Repository: conda/conda

Issue:
csh prompt handling & deactivate broken
<!--
Hi!

This is an issue tracker for conda -- the package manager.  File feature requests
for conda here, as well as bug reports about something conda has messed up.

If your issue is a bug report or feature request for:
  * a specific conda package from Anaconda ('defaults' channel):
      ==> file at https://github.com/ContinuumIO/anaconda-issues
  * a specific conda package from conda-forge:
      ==> file at the corresponding feedstock under https://github.com/conda-forge
  * repo.continuum.io access and service:
      ==> file at https://github.com/ContinuumIO/anaconda-issues
  * anaconda.org access and service:
      ==> file at https://anaconda.org/contact/report
  * commands under 'conda build':
      ==> file at https://github.com/conda/conda-build
  * commands under 'conda env':
      ==> please file it here!
  * all other conda commands that start with 'conda':
      ==> please file it here!

-->

**I'm submitting a...**
  - [x] bug report
  - [ ] feature request


## Current Behavior
<!-- What actually happens?
     If you want to include console output, please use "Steps to Reproduce" below. -->

In a csh shell, after sourcing the '~/miniconda3/etc/profile.d/conda.csh'

When activating the environment the original prompt is not a part of the new prompt.

Neither is it reverted to the original prompt when deactivating.

Additionally, when deactivating the 'CONDA' specific env vars are not removed from the environment.



### Steps to Reproduce
<!-- If the current behavior is a bug, please provide specific, minimal steps to independently reproduce.
     Include the exact conda commands that reproduce the issue and their output between the ticks below. -->
```
$> echo "'$prompt'"
'$> '
$> conda activate test_env
(test_env) echo "'$prompt'"
'(test_env) '
(test_env) conda deactivate
env | grep CONDA
_CONDA_EXE=/home/jrevans/miniconda3/bin/conda
_CONDA_ROOT=/home/jrevans/miniconda3
CONDA_SHLVL=0
CONDA_DEFAULT_ENV=test_env
CONDA_PREFIX=/home/jrevans/miniconda3/envs/test_env
CONDA_PROMPT_MODIFIER=(test_env) 
CONDA_PYTHON_EXE=/home/jrevans/miniconda3/bin/python

```


## Expected Behavior
<!-- What do you think should happen? -->

The prompt needs to be preserved throughout and the conda env vars need to be removed from the environment.

I have attached a version of the '.../etc/profile.d/conda.csh' script that fixes the prompt problem.

The env var problem can be fixed by changing the results of 'conda shell.csh deactivate' to use 'unsetenv' instead of 'unset'

[conda.csh.txt](https://github.com/conda/conda/files/1748844/conda.csh.txt)



Discussion:

Resolving patch:
diff --git a/conda/activate.py b/conda/activate.py
--- a/conda/activate.py
+++ b/conda/activate.py
@@ -70,7 +70,7 @@ def __init__(self, shell, arguments=None):
             self.shift_args = 0
             self.command_join = ';\n'
 
-            self.unset_var_tmpl = 'unset %s'
+            self.unset_var_tmpl = 'unsetenv %s'
             self.export_var_tmpl = 'setenv %s "%s"'
             self.set_var_tmpl = "set %s='%s'"
             self.run_script_tmpl = 'source "%s"'
@@ -227,7 +227,7 @@ def build_activate(self, env_name_or_prefix):
         old_conda_prefix = self.environ.get('CONDA_PREFIX')
         max_shlvl = context.max_shlvl
 
-        if old_conda_prefix == prefix:
+        if old_conda_prefix == prefix and old_conda_shlvl > 0:
             return self.build_reactivate()
         if self.environ.get('CONDA_PREFIX_%s' % (old_conda_shlvl-1)) == prefix:
             # in this case, user is attempting to activate the previous environment,
@@ -291,9 +291,10 @@ def build_activate(self, env_name_or_prefix):
 
     def build_deactivate(self):
         # query environment
+        old_conda_prefix = self.environ.get('CONDA_PREFIX')
         old_conda_shlvl = int(self.environ.get('CONDA_SHLVL', 0))
-        old_conda_prefix = self.environ.get('CONDA_PREFIX', None)
-        if old_conda_shlvl <= 0 or old_conda_prefix is None:
+        if not old_conda_prefix or old_conda_shlvl < 1:
+            # no active environment, so cannot deactivate; do nothing
             return {
                 'unset_vars': (),
                 'set_vars': {},
@@ -351,7 +352,7 @@ def build_deactivate(self):
 
     def build_reactivate(self):
         conda_prefix = self.environ.get('CONDA_PREFIX')
-        conda_shlvl = int(self.environ.get('CONDA_SHLVL', -1))
+        conda_shlvl = int(self.environ.get('CONDA_SHLVL', 0))
         if not conda_prefix or conda_shlvl < 1:
             # no active environment, so cannot reactivate; do nothing
             return {


Test patch:
diff --git a/tests/test_activate.py b/tests/test_activate.py
--- a/tests/test_activate.py
+++ b/tests/test_activate.py
@@ -687,11 +687,11 @@ 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_EXE;
-            unset CONDA_PREFIX;
-            unset CONDA_PROMPT_MODIFIER;
-            unset CONDA_PYTHON_EXE;
+            unsetenv CONDA_DEFAULT_ENV;
+            unsetenv CONDA_EXE;
+            unsetenv CONDA_PREFIX;
+            unsetenv CONDA_PROMPT_MODIFIER;
+            unsetenv CONDA_PYTHON_EXE;
             set prompt='%(prompt)s';
             setenv CONDA_SHLVL "0";
             setenv PATH "%(new_path)s";
@@ -1146,7 +1146,7 @@ def basic_csh(self, shell):
         shell.assert_env_var('CONDA_SHLVL', '0')
 
     @pytest.mark.skipif(not which('csh'), reason='csh not installed')
-    @pytest.mark.xfail(reason="csh needs work apparently; but at least tcsh works")
+    @pytest.mark.xfail(reason="pure csh doesn't support argument passing to sourced scripts")
     def test_csh_basic_integration(self):
         with InteractiveShell('csh') as shell:
             self.basic_csh(shell)