From 72deb53832477792b1eff8bd9b6289e84ca85ead Mon Sep 17 00:00:00 2001 From: Peter Scheibel Date: Fri, 24 May 2024 15:00:50 -0700 Subject: [PATCH] Make `spack clean` env-aware (#44227) `spack clean ` will now resolve specs based on the active environment if one is active. If an env is active but no matching spec is found, this will fall back on fully concretizing. --- lib/spack/spack/cmd/clean.py | 3 ++- lib/spack/spack/test/cmd/clean.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/spack/spack/cmd/clean.py b/lib/spack/spack/cmd/clean.py index 3a9a7f32abc..9dd3efa4599 100644 --- a/lib/spack/spack/cmd/clean.py +++ b/lib/spack/spack/cmd/clean.py @@ -106,7 +106,8 @@ def clean(parser, args): # Then do the cleaning falling through the cases if args.specs: - specs = spack.cmd.parse_specs(args.specs, concretize=True) + specs = spack.cmd.parse_specs(args.specs, concretize=False) + specs = list(spack.cmd.matching_spec_from_env(x) for x in specs) for spec in specs: msg = "Cleaning build stage [{0}]" tty.msg(msg.format(spec.short_spec)) diff --git a/lib/spack/spack/test/cmd/clean.py b/lib/spack/spack/test/cmd/clean.py index 43441c487c3..4c358683339 100644 --- a/lib/spack/spack/test/cmd/clean.py +++ b/lib/spack/spack/test/cmd/clean.py @@ -11,6 +11,7 @@ import spack.caches import spack.cmd.clean +import spack.environment as ev import spack.main import spack.package_base import spack.stage @@ -68,6 +69,20 @@ def test_function_calls(command_line, effects, mock_calls_for_clean): assert mock_calls_for_clean[name] == (1 if name in effects else 0) +def test_env_aware_clean(mock_stage, install_mockery, mutable_mock_env_path, monkeypatch): + e = ev.create("test", with_view=False) + e.add("mpileaks") + e.concretize() + + def fail(*args, **kwargs): + raise Exception("This should not have been called") + + monkeypatch.setattr(spack.spec.Spec, "concretize", fail) + + with e: + clean("mpileaks") + + def test_remove_python_cache(tmpdir, monkeypatch): cache_files = ["file1.pyo", "file2.pyc"] source_file = "file1.py"