Add hash hint to multi-spec message (#32652)

This commit is contained in:
Tamara Dahlgren 2022-09-22 00:18:43 -07:00 committed by GitHub
parent 457daf4be6
commit 54d06fca79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 25 deletions

View File

@ -291,18 +291,23 @@ def disambiguate_spec_from_hashes(spec, hashes, local=False, installed=True, fir
elif first: elif first:
return matching_specs[0] return matching_specs[0]
elif len(matching_specs) > 1: ensure_single_spec_or_die(spec, matching_specs)
return matching_specs[0]
def ensure_single_spec_or_die(spec, matching_specs):
if len(matching_specs) <= 1:
return
format_string = "{name}{@version}{%compiler}{arch=architecture}" format_string = "{name}{@version}{%compiler}{arch=architecture}"
args = ["%s matches multiple packages." % spec, "Matching packages:"] args = ["%s matches multiple packages." % spec, "Matching packages:"]
args += [ args += [
colorize(" @K{%s} " % s.dag_hash(7)) + s.cformat(format_string) colorize(" @K{%s} " % s.dag_hash(7)) + s.cformat(format_string) for s in matching_specs
for s in matching_specs
] ]
args += ["Use a more specific spec."] args += ["Use a more specific spec (e.g., prepend '/' to the hash)."]
tty.die(*args) tty.die(*args)
return matching_specs[0]
def gray_hash(spec, length): def gray_hash(spec, length):
if not length: if not length:

View File

@ -35,7 +35,6 @@
""" """
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.link_tree import MergeConflictError from llnl.util.link_tree import MergeConflictError
from llnl.util.tty.color import colorize
import spack.cmd import spack.cmd
import spack.environment as ev import spack.environment as ev
@ -66,16 +65,7 @@ def squash(matching_specs):
tty.die("Spec matches no installed packages.") tty.die("Spec matches no installed packages.")
matching_in_view = [ms for ms in matching_specs if ms in view_specs] matching_in_view = [ms for ms in matching_specs if ms in view_specs]
spack.cmd.ensure_single_spec_or_die("Spec", matching_in_view)
if len(matching_in_view) > 1:
spec_format = "{name}{@version}{%compiler}{arch=architecture}"
args = ["Spec matches multiple packages.", "Matching packages:"]
args += [
colorize(" @K{%s} " % s.dag_hash(7)) + s.cformat(spec_format)
for s in matching_in_view
]
args += ["Use a more specific spec."]
tty.die(*args)
return matching_in_view[0] if matching_in_view else matching_specs[0] return matching_in_view[0] if matching_in_view else matching_specs[0]

View File

@ -10,7 +10,7 @@
import spack.spec import spack.spec
import spack.user_environment as uenv import spack.user_environment as uenv
from spack.main import SpackCommand, SpackCommandError from spack.main import SpackCommand
load = SpackCommand("load") load = SpackCommand("load")
unload = SpackCommand("unload") unload = SpackCommand("unload")
@ -115,10 +115,12 @@ def test_load_first(install_mockery, mock_fetch, mock_archive, mock_packages):
"""Test with and without the --first option""" """Test with and without the --first option"""
install("libelf@0.8.12") install("libelf@0.8.12")
install("libelf@0.8.13") install("libelf@0.8.13")
# Now there are two versions of libelf
with pytest.raises(SpackCommandError): # Now there are two versions of libelf, which should cause an error
# This should cause an error due to multiple versions out = load("--sh", "libelf", fail_on_error=False)
load("--sh", "libelf") assert "matches multiple packages" in out
assert "Use a more specific spec" in out
# Using --first should avoid the error condition # Using --first should avoid the error condition
load("--sh", "--first", "libelf") load("--sh", "--first", "libelf")