From 9244ecacf0bfa3569515eaa64f4a278e1abad4ff Mon Sep 17 00:00:00 2001 From: Gregory Becker Date: Wed, 21 Jun 2023 11:34:16 -0700 Subject: [PATCH] bugfix: environments with unify:false can concretize abstract hash without name --- lib/spack/spack/environment/environment.py | 25 ++++++++++++++++------ lib/spack/spack/test/cmd/env.py | 15 +++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/spack/spack/environment/environment.py b/lib/spack/spack/environment/environment.py index 3091a79848e..9de65e9cc9c 100644 --- a/lib/spack/spack/environment/environment.py +++ b/lib/spack/spack/environment/environment.py @@ -2380,17 +2380,28 @@ def _concretize_from_constraints(spec_constraints, tests=False): # Accept only valid constraints from list and concretize spec # Get the named spec even if out of order root_spec = [s for s in spec_constraints if s.name] - if len(root_spec) != 1: - m = "The constraints %s are not a valid spec " % spec_constraints - m += "concretization target. all specs must have a single name " - m += "constraint for concretization." - raise InvalidSpecConstraintError(m) - spec_constraints.remove(root_spec[0]) + hash_spec = [s for s in spec_constraints if s.abstract_hash] + + error_message = "The constraints %s are not a valid spec " % spec_constraints + error_message += "concretization target. all specs must have a single name " + error_message += "constraint for concretization." + + if len(root_spec) > 1: + raise InvalidSpecConstraintError(error_message) + + if len(root_spec) < 1: + if len(hash_spec) < 1: + raise InvalidSpecConstraintError(error_message) + + if root_spec: + spec_constraints.remove(root_spec[0]) + + root_spec = root_spec[0] if root_spec else Spec() invalid_constraints = [] while True: # Attach all anonymous constraints to one named spec - s = root_spec[0].copy() + s = root_spec.copy() for c in spec_constraints: if c not in invalid_constraints: s.constrain(c) diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 5aeb93ad58d..d1fe7c0d248 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -2402,6 +2402,21 @@ def test_env_activate_default_view_root_unconditional(mutable_mock_env_path): ) +@pytest.mark.regression("") +def test_concretize_separately_abstract_hash(install_mockery, mock_fetch): + """Check that a root can have no name if it has a hash.""" + s = Spec("trivial-install-test-package").concretized() + install(str(s)) + + e = ev.create("test") + e.unify = False + + e.add(f"/{s.dag_hash()}") + e.concretize() + + assert list(e.concretized_specs()) == [(Spec(f"/{s.dag_hash()}"), s)] + + def test_concretize_user_specs_together(): e = ev.create("coconcretization") e.unify = True