Repository: spack/spack

Issue:
Silent Automagic Environment Activation
I have never used `spack env activate` in my life.  I don't ever want environments activate.  And yet, Spack automagically activates the environment in `$CWD` if I'm working from within that environment's directory.  As a result, commands that would otherwise be environment-free end up adversely affecting the environment I'm working on.  That non-intuitive behavior has repeatedly interrupted my workflow and confused me.  As a result, I have now routinely learned to write `spack --no-env` whenever I want a command to run without an environment.  (And if I want it to run with an environment, I use `spack -e`).  Activating the environment in `$CWD` is an anti-feature that should be removed.

**The *only* time Spack should activate an environment is when you use *spack env activate*!!**

### Steps to reproduce the issue

Create (but don't activate) an environment.  Then go to that environment's home directory and run `spack install`.  Notice how Spack has changed your environment's `spack.yaml` file.  For example:

```
$ spack env status
==> Using spack.yaml in current directory: /import/unsupported/TWOWAY/spack/var/spack/environments/tw-chinook
$ spack install libxt
do_install <spack.pkg.builtin.libxt.Libxt object at 0x255add0> None install_deps=True
==> libxt is already installed in /import/unsupported/TWOWAY/spack/opt/spack/linux-centos6-x86_64/intel-18.5.274/libxt-1.1.5-gycpi5krvp57nqeynkzviwxogqfpsuk3
==> libxt@1.1.5 : marking the package explicit
chinook00:~/spack/environments/tw-chinook$ less spack.yaml 
chinook00:~/spack/environments/tw-chinook$ git diff spack.yaml
diff --git a/var/spack/environments/tw-chinook/spack.yaml b/var/spack/environments/tw-chinook/spack.yaml
index 977c85c3c..6521d6021 100644
--- a/var/spack/environments/tw-chinook/spack.yaml
+++ b/var/spack/environments/tw-chinook/spack.yaml
@@ -79,6 +79,8 @@ spack:
     spec: chrpath
   - setup: []
     spec: patchelf
+  - setup: []
+    spec: libxt
   mirrors: {}
   modules:
     enable: []
$ cd ..
chinook00:~/spack/environments$ spack env status
==> No active environment
```



Discussion:
This is how environments work.  I'm inclined not to change it, as this is fundamental to allowing workflows where environments are versioned in repos...
Can you explain more?  I have environments versioned in repos, and this behavior is just an anti-feature for me.

I brought this up in Slack and we're getting some good discussion there -- people seem to be leaning towards explicit activation, so once again, @citibeth has caused me to reconsider something 😄.
This hasn't bitten me yet, per se, because I don't work yet work heavily with environments. But where I do currently work with them, I have multiple ones scattered across a repo where I'll be working and making different changes to several `spack.yaml` files at a time from different `$CWD`s. At some point in the relatively near future, the OLCF will be using only environments to define the software stacks on each machine (and probably several environments per machine) so I suspect @citibeth's concern will bite me at some point. And, while I can probably develop a workflow where implicit activation is not a concern, I tend to prefer explicit actions over implicit ones. Also, I'm not sure there's enough remaining screen real estate to add an *active spack env* entry to my shell prompt on top of the git status, path, host, venv, vi-mode, etc. tokens already there....
Uff, bites me as well. I though I add a `spack.yaml` to the root of my repo for developers but this now interferes badly with my Spack-powered CI if I sit in the wrong dir.
https://github.com/openPMD/openPMD-api/pull/737

Resolving patch:
diff --git a/lib/spack/spack/ci.py b/lib/spack/spack/ci.py
--- a/lib/spack/spack/ci.py
+++ b/lib/spack/spack/ci.py
@@ -612,7 +612,10 @@ def generate_gitlab_ci_yaml(env, print_summary, output_file,
                 if 'enable-debug-messages' in gitlab_ci:
                     debug_flag = '-d '
 
-                job_scripts = ['spack {0}ci rebuild'.format(debug_flag)]
+                job_scripts = [
+                    'spack env activate .',
+                    'spack {0}ci rebuild'.format(debug_flag),
+                ]
 
                 compiler_action = 'NONE'
                 if len(phases) > 1:
diff --git a/lib/spack/spack/environment.py b/lib/spack/spack/environment.py
--- a/lib/spack/spack/environment.py
+++ b/lib/spack/spack/environment.py
@@ -247,18 +247,13 @@ def find_environment(args):
         # at env_dir (env and env_dir are mutually exclusive)
         env = getattr(args, 'env_dir', None)
 
-        # if no argument, look for a manifest file
+        # if no argument, look for the environment variable
         if not env:
-            if os.path.exists(manifest_name):
-                env = os.getcwd()
+            env = os.environ.get(spack_env_var)
 
-            # if no env, env_dir, or manifest try the environment
+            # nothing was set; there's no active environment
             if not env:
-                env = os.environ.get(spack_env_var)
-
-                # nothing was set; there's no active environment
-                if not env:
-                    return None
+                return None
 
     # if we get here, env isn't the name of a spack environment; it has
     # to be a path to an environment, or there is something wrong.


Test patch: