pw_env_setup: Reduce PW_ENVIRONMENT_ROOT errors

PW_ENVIRONMENT_ROOT is now only set by developers and not by
bootstrap.sh. bootstrap.sh now sets _PW_ACTUAL_ENVIRONMENT_ROOT and
that's what the rest of Pigweed uses. This means that if
PW_ENVIRONMENT_ROOT is set it was always set directly by a user.

Change-Id: I782b4e8dd5168facdf0ad8c333f2c0cf8ec18468
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/14880
Reviewed-by: Keir Mierle <keir@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/bootstrap.bat b/bootstrap.bat
index 124c1ec..9532775 100644
--- a/bootstrap.bat
+++ b/bootstrap.bat
@@ -56,11 +56,18 @@
   )
 )
 
-:: Not prefixing environment with "." since that doesn't hide it anyway.
+:: PW_ENVIRONMENT_ROOT allows developers to specify where the environment should
+:: be installed. _PW_ACTUAL_ENVIRONMENT_ROOT is where Pigweed keeps that 
+:: information. This separation allows Pigweed to assume PW_ENVIRONMENT_ROOT 
+:: came from the developer and not from a previous bootstrap possibly from 
+:: another workspace.
 if "%PW_ENVIRONMENT_ROOT%"=="" (
-   set "PW_ENVIRONMENT_ROOT=%PW_ROOT%\environment"
+   :: Not prefixing environment with "." since that doesn't hide it anyway.
+   set "_PW_ACTUAL_ENVIRONMENT_ROOT=%PW_ROOT%\environment"
+) else (
+   set "_PW_ACTUAL_ENVIRONMENT_ROOT=%PW_ENVIRONMENT_ROOT%"
 )
-set "shell_file=%PW_ENVIRONMENT_ROOT%\activate.bat"
+set "shell_file=%_PW_ACTUAL_ENVIRONMENT_ROOT%\activate.bat"
 
 set _PW_OLD_CIPD_PACKAGE_FILES=%PW_CIPD_PACKAGE_FILES%
 set _PW_OLD_VIRTUALENV_REQUIREMENTS=%PW_VIRTUALENV_REQUIREMENTS%
@@ -82,7 +89,7 @@
   call "%python%" "%PW_ROOT%\pw_env_setup\py\pw_env_setup\env_setup.py" ^
       --pw-root "%PW_ROOT%/" ^
       --shell-file "%shell_file%" ^
-      --install-dir "%PW_ENVIRONMENT_ROOT%"
+      --install-dir "%_PW_ACTUAL_ENVIRONMENT_ROOT%"
 ) else (
   if exist "%shell_file%" (
     call "%python%" "%_pw_start_script%"
diff --git a/bootstrap.sh b/bootstrap.sh
index cf56121..11e44e1 100644
--- a/bootstrap.sh
+++ b/bootstrap.sh
@@ -165,11 +165,19 @@
 
 export PW_ROOT
 
+# PW_ENVIRONMENT_ROOT allows developers to specify where the environment should
+# be installed. _PW_ACTUAL_ENVIRONMENT_ROOT is where Pigweed keeps that 
+# information. This separation allows Pigweed to assume PW_ENVIRONMENT_ROOT 
+# came from the developer and not from a previous bootstrap possibly from 
+# another workspace.
 if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
-  PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
-  export PW_ENVIRONMENT_ROOT
+  _PW_ACTUAL_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
+  export _PW_ACTUAL_ENVIRONMENT_ROOT
+else
+  _PW_ACTUAL_ENVIRONMENT_ROOT="$PW_ENVIRONMENT_ROOT"
+  export _PW_ACTUAL_ENVIRONMENT_ROOT
 fi
-SETUP_SH="$PW_ENVIRONMENT_ROOT/activate.sh"
+SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
 
 if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
   _pw_green "\n  WELCOME TO...\n"
@@ -223,9 +231,9 @@
   fi
 
   if [ -n "$_PW_ENV_SETUP" ]; then
-    "$_PW_ENV_SETUP" --shell-file "$SETUP_SH" --install-dir "$PW_ENVIRONMENT_ROOT"
+    "$_PW_ENV_SETUP" --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
   else
-    "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" --install-dir "$PW_ENVIRONMENT_ROOT"
+    "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
   fi
 
   PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES"
diff --git a/pw_doctor/py/pw_doctor/doctor.py b/pw_doctor/py/pw_doctor/doctor.py
index 42113be..d203df8 100755
--- a/pw_doctor/py/pw_doctor/doctor.py
+++ b/pw_doctor/py/pw_doctor/doctor.py
@@ -191,8 +191,8 @@
         return
 
     var = 'PW_ROOT'
-    if 'PW_ENVIRONMENT_ROOT' in os.environ:
-        var = 'PW_ENVIRONMENT_ROOT'
+    if '_PW_ACTUAL_ENVIRONMENT_ROOT' in os.environ:
+        var = '_PW_ACTUAL_ENVIRONMENT_ROOT'
     root = pathlib.Path(os.environ[var]).resolve()
 
     if root not in venv_path.parents:
diff --git a/pw_env_setup/py/pw_env_setup/env_setup.py b/pw_env_setup/py/pw_env_setup/env_setup.py
index e916da8..a95e77e 100755
--- a/pw_env_setup/py/pw_env_setup/env_setup.py
+++ b/pw_env_setup/py/pw_env_setup/env_setup.py
@@ -189,9 +189,10 @@
         if isinstance(self._pw_root, bytes) and bytes != str:
             self._pw_root = self._pw_root.decode()
 
-        # No need to set PW_ROOT or PW_ENVIRONMENT_ROOT, that will be done by
-        # bootstrap.sh and bootstrap.bat for both bootstrap and activate.
-        self._env.add_replacement('PW_ENVIRONMENT_ROOT', install_dir)
+        # No need to set PW_ROOT or _PW_ACTUAL_ENVIRONMENT_ROOT, that will be
+        # done by bootstrap.sh and bootstrap.bat for both bootstrap and
+        # activate.
+        self._env.add_replacement('_PW_ACTUAL_ENVIRONMENT_ROOT', install_dir)
         self._env.add_replacement('PW_ROOT', pw_root)
 
     def _log(self, *args, **kwargs):