Repository: spack/spack

Issue:
activating already active extension causes an error (with potential fix)
activating already active extension causes an error and returns non zero error code

### Steps to reproduce the issue 

in `spack/ubuntu-bionic` container:

```console
spack install py-sphinx
spack activate py-sphinx
spack activate py-sphinx
```

### Error Message

Second activate fails with a message that its already activated. 

```console
==> Error: py-sphinx@3.2.0%gcc@7.5.0 arch=linux-ubuntu18.04-zen/mzyx34y is already installed in python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
```

I expect it to simply succeed if already installed and not in conflict. 

Also note, if you try to install a dependent extension and activate that will always fail with a dependent is already installed error:



```console
spack install py-sphinx-rtd-theme

spack -d activate  py-sphinx-rtd-theme
==> [2021-03-25-00:34:33.922544] Imported activate from built-in commands
==> [2021-03-25-00:34:33.923059] Imported activate from built-in commands
==> [2021-03-25-00:34:33.923969] Reading config file /opt/spack/etc/spack/defaults/config.yaml
==> [2021-03-25-00:34:33.939249] DATABASE LOCK TIMEOUT: 3s
==> [2021-03-25-00:34:33.939296] PACKAGE LOCK TIMEOUT: No timeout
==> [2021-03-25-00:34:33.942902] Reading config file /opt/spack/etc/spack/defaults/repos.yaml
==> [2021-03-25-00:34:38.207189] Activating extension py-sphinx-rtd-theme@0.5.1%gcc@7.5.0 arch=linux-ubuntu18.04-zen/hbsujrb for python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
==> [2021-03-25-00:34:38.212585] Activating extension py-sphinx@3.2.0%gcc@7.5.0 arch=linux-ubuntu18.04-zen/mzyx34y for python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
==> [2021-03-25-00:34:38.223965] ExtensionAlreadyInstalledError: py-sphinx@3.2.0%gcc@7.5.0 arch=linux-ubuntu18.04-zen/mzyx34y is already installed in python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
==> [2021-03-25-00:34:38.224016] Error: py-sphinx@3.2.0%gcc@7.5.0 arch=linux-ubuntu18.04-zen/mzyx34y is already installed in python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
Traceback (most recent call last):
  File "/opt/spack/lib/spack/spack/main.py", line 768, in main
    return _invoke_command(command, parser, args, unknown)
  File "/opt/spack/lib/spack/spack/main.py", line 496, in _invoke_command
    return_val = command(parser, args)
  File "/opt/spack/lib/spack/spack/cmd/activate.py", line 51, in activate
    spec.package.do_activate(view, with_dependencies=not args.force)
  File "/opt/spack/lib/spack/spack/package.py", line 2331, in do_activate
    verbose=verbose)
  File "/opt/spack/lib/spack/spack/package.py", line 2323, in do_activate
    self.extendee_spec, self.spec)
  File "/opt/spack/lib/spack/spack/directory_layout.py", line 434, in check_extension_conflict
    raise ExtensionAlreadyInstalledError(spec, ext_spec)
spack.directory_layout.ExtensionAlreadyInstalledError: py-sphinx@3.2.0%gcc@7.5.0 arch=linux-ubuntu18.04-zen/mzyx34y is already installed in python@3.8.8%gcc@7.5.0+bz2+ctypes+dbm~debug+libxml2+lzma~nis~optimizations+pic+pyexpat+pythoncmd+readline+shared+sqlite3+ssl~tix~tkinter~ucs4+uuid+zlib patches=0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 arch=linux-ubuntu18.04-zen/6sotmzu
```

This change fixes the issue for me:
```
diff --git a/lib/spack/spack/package.py b/lib/spack/spack/package.py
index 9dfd8bf..6d8c75c 100644
--- a/lib/spack/spack/package.py
+++ b/lib/spack/spack/package.py
@@ -2319,8 +2319,13 @@ def do_activate(self, view=None, with_dependencies=True, verbose=True):
 
         extensions_layout = view.extensions_layout
 
-        extensions_layout.check_extension_conflict(
-            self.extendee_spec, self.spec)
+        try:
+            extensions_layout.check_extension_conflict(
+                    self.extendee_spec, self.spec)
+        except spack.directory_layout.ExtensionAlreadyInstalledError as e:
+            # already installed, let caller know
+            tty.msg(e.message)
+            return
 
         # Activate any package dependencies that are also extensions.
         if with_dependencies:
```

Any advice on tweaks to this or another approach would be great!

### Information on your system

* **Spack:** 0.16.1
* **Python:** 3.6.9
* **Platform:** linux-ubuntu18.04-zen3
* **Concretizer:** original

### Additional information

<!-- These boxes can be checked by replacing [ ] with [x] or by clicking them after submitting the issue. -->
- [x] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [x] I have searched the issues of this repo and believe this is not a duplicate
- [x] I have run the failing commands in debug mode and reported the output



Discussion:
@becker33 here are details and a possible fix for the issue I mentioned on slack.

Resolving patch:
diff --git a/lib/spack/spack/package.py b/lib/spack/spack/package.py
--- a/lib/spack/spack/package.py
+++ b/lib/spack/spack/package.py
@@ -2319,8 +2319,13 @@ def do_activate(self, view=None, with_dependencies=True, verbose=True):
 
         extensions_layout = view.extensions_layout
 
-        extensions_layout.check_extension_conflict(
-            self.extendee_spec, self.spec)
+        try:
+            extensions_layout.check_extension_conflict(
+                self.extendee_spec, self.spec)
+        except spack.directory_layout.ExtensionAlreadyInstalledError as e:
+            # already installed, let caller know
+            tty.msg(e.message)
+            return
 
         # Activate any package dependencies that are also extensions.
         if with_dependencies:


Test patch: