Repository: conda/conda

Issue:
PATH manipulation problems with conda.bat activate on Windows
With conda 4.4.5:

```
C:\Users\builder>echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\PuTTY\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\dotnet\;C:\Users\builder\AppData\Local\Microsoft\WindowsApps;

c:/Users/builder/m64/Library/bin/conda.bat activate
'chcp' is not recognized as an internal or external command,
operable program or batch file.

(base) C:\Users\builder>(base) C:\Users\builder>echo %PATH%
c:\Users\builder\m64;c:\Users\builder\m64\Library\mingw-w64\bin;c:\Users\builder\m64\Library\usr\bin;c:\Users\builder\m64\Library\bin;c:\Users\builder\m64\Scripts;c:\Users\builder\m64\bin;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\PuTTY\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\dotnet\;C:\Users\builder\AppData\Local\Microsoft\WindowsApps;
```

Notice that `C:\WINDOWS\system32` got dropped which is why `chcp` isn't found (this folder contains lots of important programs too).


Discussion:
Is this with 4.4.5 in canary?

Sent from my iPhone

> On Dec 29, 2017, at 8:19 PM, Ray Donnelly <notifications@github.com> wrote:
> 
> C:\Users\builder>echo %PATH%
> C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\PuTTY\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\dotnet\;C:\Users\builder\AppData\Local\Microsoft\WindowsApps;
> 
> c:/Users/builder/m64/Library/bin/conda.bat activate
> 'chcp' is not recognized as an internal or external command,
> operable program or batch file.
> 
> c:\Users\builder\m64;c:\Users\builder\m64\Library\mingw-w64\bin;c:\Users\builder\m64\Library\usr\bin;c:\Users\builder\m64\Library\bin;c:\Users\builder\m64\Scripts;c:\Users\builder\m64\bin;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\PuTTY\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\dotnet\;C:\Users\builder\AppData\Local\Microsoft\WindowsApps;
> Notice that C:\WINDOWS\system32 got dropped which is why chcp isn't found (this folder contains lots of important programs too).
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub, or mute the thread.
> 

Yes and 4.4.4 too
OK, I know what's going on and it's kind of my fault (but I want to make a PR to conda to avoid this anyway).

While investigating https://github.com/ContinuumIO/anaconda/issues/1158 to check whether [this horrible hack](https://github.com/AnacondaRecipes/python-feedstock/blob/master/recipe/0005-Win32-Ensure-Library-bin-is-in-os.environ-PATH.patch) was to blame I removed it.

That then tripped up [this code](https://github.com/conda/conda/blob/master/conda/activate.py#L368-L374):

```
    def _get_starting_path_list(self):
        path = os.environ['PATH']
        if on_win:
            # on Windows, the python interpreter prepends sys.prefix\Library\bin on startup WTF
            return path.split(os.pathsep)[1:]
        else:
            return path.split(os.pathsep)
```

I will make a PR that will only elide the first element when the interpreter has actually done this (by testing it to see if the value of the first entry is indeed `sys.prefix\Library\bin`) and will add a link back to this unfortunate patch in our python recipe for reference (hopefully one day that link will point to nothing).

This will remove a dependency between future conda releasses and this hack in our Python interpreter and will also allow conda to work correctly on other builds of Python on Windows.


Resolving patch:
diff --git a/conda/activate.py b/conda/activate.py
--- a/conda/activate.py
+++ b/conda/activate.py
@@ -368,8 +368,20 @@ def build_reactivate(self):
     def _get_starting_path_list(self):
         path = os.environ['PATH']
         if on_win:
-            # on Windows, the python interpreter prepends sys.prefix\Library\bin on startup WTF
-            return path.split(os.pathsep)[1:]
+            # On Windows, the Anaconda Python interpreter prepends sys.prefix\Library\bin on
+            # startup. It's a hack that allows users to avoid using the correct activation
+            # procedure; a hack that needs to go away because it doesn't add all the paths.
+            # See: https://github.com/AnacondaRecipes/python-feedstock/blob/master/recipe/0005-Win32-Ensure-Library-bin-is-in-os.environ-PATH.patch  # NOQA
+            # But, we now detect if that has happened because:
+            #   1. In future we would like to remove this hack and require real activation.
+            #   2. We should not assume that the Anaconda Python interpreter is being used.
+            path_split = path.split(os.pathsep)
+            library_bin = r"%s\Library\bin" % (sys.prefix)
+            # ^^^ deliberately the same as: https://github.com/AnacondaRecipes/python-feedstock/blob/8e8aee4e2f4141ecfab082776a00b374c62bb6d6/recipe/0005-Win32-Ensure-Library-bin-is-in-os.environ-PATH.patch#L20  # NOQA
+            if normpath(path_split[0]) == normpath(library_bin):
+                return path_split[1:]
+            else:
+                return path_split
         else:
             return path.split(os.pathsep)
 


Test patch:
diff --git a/tests/cli/test_activate.py b/tests/cli/test_activate.py
--- a/tests/cli/test_activate.py
+++ b/tests/cli/test_activate.py
@@ -246,7 +246,6 @@ def test_activate_bad_env_keeps_existing_good_env(shell):
 
 
 @pytest.mark.installed
-@pytest.mark.xfail(on_win and datetime.now() < datetime(2018, 11, 21), reason="need to get 4.4.x merge-up finished", strict=True)
 def test_activate_deactivate(shell):
     if shell == 'bash.exe':
         pytest.skip("usage of cygpath in win_path_to_unix messes this test up")
@@ -284,7 +283,6 @@ def test_activate_deactivate(shell):
 
 
 @pytest.mark.installed
-@pytest.mark.xfail(on_win and datetime.now() < datetime(2018, 11, 21), reason="need to get 4.4.x merge-up finished", strict=True)
 def test_activate_root_simple(shell):
     if shell == 'bash.exe':
         pytest.skip("usage of cygpath in win_path_to_unix messes this test up")