diff --git a/lib/spack/spack/environment.py b/lib/spack/spack/environment.py index 2c7a0cf098..2be355df2d 100644 --- a/lib/spack/spack/environment.py +++ b/lib/spack/spack/environment.py @@ -813,7 +813,10 @@ def add(self, user_spec, list_name=user_speclist_name): raise SpackEnvironmentError( 'cannot add anonymous specs to an environment!') elif not spack.repo.path.exists(spec.name): - raise SpackEnvironmentError('no such package: %s' % spec.name) + virtuals = spack.repo.path.provider_index.providers.keys() + if spec.name not in virtuals: + msg = 'no such package: %s' % spec.name + raise SpackEnvironmentError(msg) list_to_change = self.spec_lists[list_name] existing = str(spec) in list_to_change.yaml_list diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 9b3ca6e66e..85413a7939 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -63,6 +63,27 @@ def test_add(): assert Spec('mpileaks') in e.user_specs +def test_env_add_virtual(): + env('create', 'test') + + e = ev.read('test') + e.add('mpi') + e.concretize() + + hashes = e.concretized_order + assert len(hashes) == 1 + spec = e.specs_by_hash[hashes[0]] + assert spec.satisfies('mpi') + + +def test_env_add_nonexistant_fails(): + env('create', 'test') + + e = ev.read('test') + with pytest.raises(ev.SpackEnvironmentError, match=r'no such package'): + e.add('thispackagedoesnotexist') + + def test_env_list(mutable_mock_env_path): env('create', 'foo') env('create', 'bar') @@ -1777,7 +1798,7 @@ def test_duplicate_packages_raise_when_concretizing_together(): def test_env_write_only_non_default(): - print(env('create', 'test')) + env('create', 'test') e = ev.read('test') with open(e.manifest_path, 'r') as f: