Repository: spack/spack

Issue:
External perl breaks $PATH in build_env
While trying to reinstall an environment that used to work before my last pull (to 008fe37941d2f2071f038c4bc6465d8f80267b57), packages started to break down with errors suggesting that system stuff was leaking into `build_env`, for example:

```console
$ spack install gmp
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
>> 661    make[2]: *** [addmul_1.lo] Error 63
     663    libtool: Version mismatch error.  This is libtool 2.4.2, but the
     664    libtool: definition of this LT_INIT comes from libtool 2.4.6.
     665    libtool: You should recreate aclocal.m4 with macros from libtool 2.4.2
     666    libtool: and run autoconf again.
```

`git bisect` pointed me to 99dfff447509a68d672a82812e5abfbc463b6d26 (merged in #13578) that looks like the culprit: when a package being installed depends on `perl`, the patch in #13578 prepends the concretized `perl` path to everything else. The issue is that when `perl` is external, that machinery ends up **prepending a system directory (`/usr/bin` for instance) to `$PATH` in the `build_env`** thus making the build environment find system stuff over Spack stuff (e.g.: `libtool`).

```console
$ spack build-env gmp | grep libtool
# ...stuff...
PATH=/usr/bin:/davide/home/userinternal/fficarel/src/spack/lib/spack/env/gcc:/davide/home/userinternal/fficarel/src/spack/opt/spack/linux-centos7-power8le/gcc-4.8.5/libtool-2.4.6-eew4dtszgkcdg27jmfy6p57gk4kgq644/bin:/usr/bin
# ...stuff...
```

Notice that `/usr/bin` shows up both as first and last `$PATH` component.

### Steps to reproduce the issue

On a system with a provided `libtool < 2.4.6` (older than the default version provided by `spack` @  008fe37941d2f2071f038c4bc6465d8f80267b57), the following commands reliably reproduce the issue (any package that has an `autoreconf` phase and directly depends on `libtool` will trigger the issue in the same way tho):

```console
$ cat ~/.spack/packages.yaml
packages:
  perl:
    paths:
      perl@5.16.3-host +shared +threads: /usr
    buildable: False
$ spack install gmp
==> Executing phase: 'autoreconf'
==> Executing phase: 'configure'
==> Executing phase: 'build'
>> 661    make[2]: *** [addmul_1.lo] Error 63
     663    libtool: Version mismatch error.  This is libtool 2.4.2, but the
     664    libtool: definition of this LT_INIT comes from libtool 2.4.6.
     665    libtool: You should recreate aclocal.m4 with macros from libtool 2.4.2
     666    libtool: and run autoconf again.
```

### Information on your system

```console
$ lsb_release --all
LSB Version:	:core-4.1-noarch:core-4.1-ppc64le
Distributor ID:	CentOS
Description:	CentOS Linux release 7.5.1804 (AltArch) 
Release:	7.5.1804
Codename:	AltArch
```

```console
$ cat ~/.spack/packages.yaml
packages:
  perl:
    paths:
      perl@5.16.3-host +shared +threads: /usr
    buildable: False
```



Discussion:

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/perl/package.py b/var/spack/repos/builtin/packages/perl/package.py
--- a/var/spack/repos/builtin/packages/perl/package.py
+++ b/var/spack/repos/builtin/packages/perl/package.py
@@ -165,43 +165,24 @@ def install_cpanm(self):
                 make()
                 make('install')
 
-    def setup_dependent_build_environment(self, env, dependent_spec):
+    def _setup_dependent_env(self, env, dependent_spec, deptypes):
         """Set PATH and PERL5LIB to include the extension and
            any other perl extensions it depends on,
            assuming they were installed with INSTALL_BASE defined."""
-        perl_lib_dirs = [join_path(self.spec.prefix.lib,
-                                   str(self.spec.version))]
-        perl_bin_dirs = [self.spec.prefix.bin]
-        for d in dependent_spec.traverse(
-                deptype=('build', 'run'), deptype_query='run'):
+        perl_lib_dirs = []
+        for d in dependent_spec.traverse(deptype=deptypes):
             if d.package.extends(self.spec):
                 perl_lib_dirs.append(d.prefix.lib.perl5)
-                perl_bin_dirs.append(d.prefix.bin)
-        if perl_bin_dirs:
-            perl_bin_path = ':'.join(perl_bin_dirs)
-            env.prepend_path('PATH', perl_bin_path)
         if perl_lib_dirs:
             perl_lib_path = ':'.join(perl_lib_dirs)
             env.prepend_path('PERL5LIB', perl_lib_path)
 
+    def setup_dependent_build_environment(self, env, dependent_spec):
+        self._setup_dependent_env(env, dependent_spec,
+                                  deptypes=('build', 'run'))
+
     def setup_dependent_run_environment(self, env, dependent_spec):
-        """Set PATH and PERL5LIB to include the extension and
-           any other perl extensions it depends on,
-           assuming they were installed with INSTALL_BASE defined."""
-        perl_lib_dirs = [join_path(self.spec.prefix.lib,
-                                   str(self.spec.version))]
-        perl_bin_dirs = [self.spec.prefix.bin]
-        for d in dependent_spec.traverse(
-                deptype=('run',), deptype_query='run'):
-            if d.package.extends(self.spec):
-                perl_lib_dirs.append(d.prefix.lib.perl5)
-                perl_bin_dirs.append(d.prefix.bin)
-        if perl_bin_dirs:
-            perl_bin_path = ':'.join(perl_bin_dirs)
-            env.prepend_path('PATH', perl_bin_path)
-        if perl_lib_dirs:
-            perl_lib_path = ':'.join(perl_lib_dirs)
-            env.prepend_path('PERL5LIB', perl_lib_path)
+        self._setup_dependent_env(env, dependent_spec, deptypes=('run',))
 
     def setup_dependent_package(self, module, dependent_spec):
         """Called before perl modules' install() methods.


Test patch: