From ebb40ee0d185d6ae3aa2258f4594933976c7099b Mon Sep 17 00:00:00 2001 From: G-Ragghianti <33492707+G-Ragghianti@users.noreply.github.com> Date: Wed, 10 May 2023 06:26:29 -0400 Subject: New option "--first" for "spack location" (#36283) --- lib/spack/spack/cmd/location.py | 10 +++++++++- lib/spack/spack/test/cmd/location.py | 9 +++++++++ share/spack/spack-completion.bash | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/spack/spack/cmd/location.py b/lib/spack/spack/cmd/location.py index aba8264a28..0afac01b0c 100644 --- a/lib/spack/spack/cmd/location.py +++ b/lib/spack/spack/cmd/location.py @@ -76,6 +76,14 @@ def setup_parser(subparser): help="location of the named or current environment", ) + subparser.add_argument( + "--first", + action="store_true", + default=False, + dest="find_first", + help="use the first match if multiple packages match the spec", + ) + arguments.add_common_arguments(subparser, ["spec"]) @@ -121,7 +129,7 @@ def location(parser, args): # install_dir command matches against installed specs. if args.install_dir: env = ev.active_environment() - spec = spack.cmd.disambiguate_spec(specs[0], env) + spec = spack.cmd.disambiguate_spec(specs[0], env, first=args.find_first) print(spec.prefix) return diff --git a/lib/spack/spack/test/cmd/location.py b/lib/spack/spack/test/cmd/location.py index 38d3f45619..1e42141199 100644 --- a/lib/spack/spack/test/cmd/location.py +++ b/lib/spack/spack/test/cmd/location.py @@ -37,6 +37,15 @@ def mock_spec(): shutil.rmtree(s.package.stage.path) +def test_location_first(install_mockery, mock_fetch, mock_archive, mock_packages): + """Test with and without the --first option""" + install = SpackCommand("install") + install("libelf@0.8.12") + install("libelf@0.8.13") + # This would normally return an error without --first + assert location("--first", "--install-dir", "libelf") + + def test_location_build_dir(mock_spec): """Tests spack location --build-dir.""" spec, pkg = mock_spec diff --git a/share/spack/spack-completion.bash b/share/spack/spack-completion.bash index 320e6fab57..0db940129a 100755 --- a/share/spack/spack-completion.bash +++ b/share/spack/spack-completion.bash @@ -590,7 +590,7 @@ _spack_buildcache_rebuild_index() { _spack_cd() { if $list_options then - SPACK_COMPREPLY="-h --help -m --module-dir -r --spack-root -i --install-dir -p --package-dir -P --packages -s --stage-dir -S --stages --source-dir -b --build-dir -e --env" + SPACK_COMPREPLY="-h --help -m --module-dir -r --spack-root -i --install-dir -p --package-dir -P --packages -s --stage-dir -S --stages --source-dir -b --build-dir -e --env --first" else _all_packages fi @@ -1243,7 +1243,7 @@ _spack_load() { _spack_location() { if $list_options then - SPACK_COMPREPLY="-h --help -m --module-dir -r --spack-root -i --install-dir -p --package-dir -P --packages -s --stage-dir -S --stages --source-dir -b --build-dir -e --env" + SPACK_COMPREPLY="-h --help -m --module-dir -r --spack-root -i --install-dir -p --package-dir -P --packages -s --stage-dir -S --stages --source-dir -b --build-dir -e --env --first" else _all_packages fi -- cgit v1.2.3-60-g2f50