Repository: conda/conda

Issue:
deactivate sets `CONDA_PREFIX` to the wrong value.
Deactivating an env. into which the new compilers have been installed leads to:

```
ERROR: This cross-compiler package contains no program /opt/conda/bin/x86_64-conda_cos6-linux-gnu-cc
```

This comes from [here](https://github.com/AnacondaRecipes/aggregate/blob/master/ctng-compilers-activation-feedstock/recipe/deactivate-gcc.sh#L58)

It seems that `CONDA_PREFIX` has been set to the prefix of the new environment instead of that of the old environment.

It doesn't seem to matter whether the legacy activate scripts or the conda shell function are used here, both exhibit the same problem.


Discussion:
> It seems that `CONDA_PREFIX` has been set to the prefix of the new environment instead of that of the old environment.

It's my understanding that `CONDA_PREFIX` has always been the path to the current active prefix.  At least back to the #1727 days or so.  Then again, I could be completely mis-remembering, and I've added pieces of code over time that made that assumption, because I can't find any reference to `CONDA_PREFIX` in the 4.3.x code concerning activate.
Oh, right.  I was only looking in the code, not the shell wrappers.

https://github.com/conda/conda/blob/4.3.31/shell/activate#L88-L92

Resolving patch:
diff --git a/conda/activate.py b/conda/activate.py
--- a/conda/activate.py
+++ b/conda/activate.py
@@ -187,6 +187,9 @@ class Help(CondaError):  # NOQA
         self.command = command
 
     def _yield_commands(self, cmds_dict):
+        for script in cmds_dict.get('deactivate_scripts', ()):
+            yield self.run_script_tmpl % script
+
         for key in sorted(cmds_dict.get('unset_vars', ())):
             yield self.unset_var_tmpl % key
 
@@ -196,9 +199,6 @@ def _yield_commands(self, cmds_dict):
         for key, value in sorted(iteritems(cmds_dict.get('export_vars', {}))):
             yield self.export_var_tmpl % (key, value)
 
-        for script in cmds_dict.get('deactivate_scripts', ()):
-            yield self.run_script_tmpl % script
-
         for script in cmds_dict.get('activate_scripts', ()):
             yield self.run_script_tmpl % script
 


Test patch:
diff --git a/tests/test_activate.py b/tests/test_activate.py
--- a/tests/test_activate.py
+++ b/tests/test_activate.py
@@ -446,9 +446,9 @@ def test_posix_basic(self):
             reactivate_data = c.stdout
 
             assert reactivate_data == dals("""
+            . "%(deactivate1)s"
             export CONDA_PROMPT_MODIFIER='(%(native_prefix)s) '
             export CONDA_SHLVL='1'
-            . "%(deactivate1)s"
             . "%(activate1)s"
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.sh')),
@@ -464,6 +464,7 @@ def test_posix_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            . "%(deactivate1)s"
             unset CONDA_DEFAULT_ENV
             unset CONDA_PREFIX
             unset CONDA_PROMPT_MODIFIER
@@ -471,7 +472,6 @@ def test_posix_basic(self):
             PS1='%(ps1)s'
             export CONDA_SHLVL='0'
             export PATH='%(new_path)s'
-            . "%(deactivate1)s"
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.sh')),
@@ -524,9 +524,9 @@ def test_cmd_exe_basic(self):
             rm_rf(reactivate_result)
 
             assert reactivate_data == dals("""
+            @CALL "%(deactivate1)s"
             @SET "CONDA_PROMPT_MODIFIER=(%(native_prefix)s) "
             @SET "CONDA_SHLVL=1"
-            @CALL "%(deactivate1)s"
             @CALL "%(activate1)s"
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.bat')),
@@ -545,13 +545,13 @@ def test_cmd_exe_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            @CALL "%(deactivate1)s"
             @SET CONDA_DEFAULT_ENV=
             @SET CONDA_PREFIX=
             @SET CONDA_PROMPT_MODIFIER=
             @SET CONDA_PYTHON_EXE=
             @SET "CONDA_SHLVL=0"
             @SET "PATH=%(new_path)s"
-            @CALL "%(deactivate1)s"
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.bat')),
@@ -598,9 +598,9 @@ def test_csh_basic(self):
             reactivate_data = c.stdout
 
             assert reactivate_data == dals("""
+            source "%(deactivate1)s";
             setenv CONDA_PROMPT_MODIFIER "(%(native_prefix)s) ";
             setenv CONDA_SHLVL "1";
-            source "%(deactivate1)s";
             source "%(activate1)s";
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.csh')),
@@ -616,6 +616,7 @@ def test_csh_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            source "%(deactivate1)s";
             unset CONDA_DEFAULT_ENV;
             unset CONDA_PREFIX;
             unset CONDA_PROMPT_MODIFIER;
@@ -623,7 +624,6 @@ def test_csh_basic(self):
             set prompt='%(prompt)s';
             setenv CONDA_SHLVL "0";
             setenv PATH "%(new_path)s";
-            source "%(deactivate1)s";
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.csh')),
@@ -676,9 +676,9 @@ def test_xonsh_basic(self):
             rm_rf(reactivate_result)
 
             assert reactivate_data == dals("""
+            source "%(deactivate1)s"
             $CONDA_PROMPT_MODIFIER = '(%(native_prefix)s) '
             $CONDA_SHLVL = '1'
-            source "%(deactivate1)s"
             source "%(activate1)s"
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.xsh')),
@@ -697,13 +697,13 @@ def test_xonsh_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            source "%(deactivate1)s"
             del $CONDA_DEFAULT_ENV
             del $CONDA_PREFIX
             del $CONDA_PROMPT_MODIFIER
             del $CONDA_PYTHON_EXE
             $CONDA_SHLVL = '0'
             $PATH = '%(new_path)s'
-            source "%(deactivate1)s"
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.xsh')),
@@ -748,9 +748,9 @@ def test_fish_basic(self):
             reactivate_data = c.stdout
 
             assert reactivate_data == dals("""
+            source "%(deactivate1)s";
             set -gx CONDA_PROMPT_MODIFIER "(%(native_prefix)s) ";
             set -gx CONDA_SHLVL "1";
-            source "%(deactivate1)s";
             source "%(activate1)s";
             """) % {
                 'activate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.fish')),
@@ -766,13 +766,13 @@ def test_fish_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            source "%(deactivate1)s";
             set -e CONDA_DEFAULT_ENV;
             set -e CONDA_PREFIX;
             set -e CONDA_PROMPT_MODIFIER;
             set -e CONDA_PYTHON_EXE;
             set -gx CONDA_SHLVL "0";
             set -gx PATH "%(new_path)s";
-            source "%(deactivate1)s";
             """) % {
                 'new_path': new_path,
                 'deactivate1': activator.path_conversion(join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.fish')),
@@ -817,9 +817,9 @@ def test_powershell_basic(self):
             reactivate_data = c.stdout
 
             assert reactivate_data == dals("""
+            . "%(deactivate1)s"
             $env:CONDA_PROMPT_MODIFIER = "(%(prefix)s) "
             $env:CONDA_SHLVL = "1"
-            . "%(deactivate1)s"
             . "%(activate1)s"
             """) % {
                 'activate1': join(self.prefix, 'etc', 'conda', 'activate.d', 'activate1.ps1'),
@@ -835,13 +835,13 @@ def test_powershell_basic(self):
 
             new_path = activator.pathsep_join(activator._remove_prefix_from_path(self.prefix))
             assert deactivate_data == dals("""
+            . "%(deactivate1)s"
             Remove-Variable CONDA_DEFAULT_ENV
             Remove-Variable CONDA_PREFIX
             Remove-Variable CONDA_PROMPT_MODIFIER
             Remove-Variable CONDA_PYTHON_EXE
             $env:CONDA_SHLVL = "0"
             $env:PATH = "%(new_path)s"
-            . "%(deactivate1)s"
             """) % {
                 'new_path': new_path,
                 'deactivate1': join(self.prefix, 'etc', 'conda', 'deactivate.d', 'deactivate1.ps1'),