spack / spack

A flexible package manager that supports multiple versions, configurations, platforms, and compilers.
https://spack.io
Other
4.21k stars 2.24k forks source link

Spec.format errors when using `{variants.<name>.name|value}` #45318

Open frankeverdij opened 1 month ago

frankeverdij commented 1 month ago

Steps to reproduce

After a discussion in (Slack) about changes in allowed characters with respect to module file generation with variants in the name, i tested this with various Slack versions.

  $ git clone https://github.com/spack/spack.git
  $ cd spack
  $ git checkout v0.22.1
  $ source share/spack/setup-env.sh
  $ spack compiler find
  $ spack config add "modules:default:lmod:projections:zlib:'{name}/{version}_{variants.pic.name}_{variants.pic.value}'"
  $ spack install zlib+pic
  $ spack module lmod refresh -y

To my surprise the module file generation breaks altogether with respect to version v0.21.2.
It should have made the following module filename (verified using v0.21.2):

  $ git checkout v0.21.2
  $ source share/spack/setup-env.sh
  $ spack config add "modules:default:lmod:projections:zlib:'{name}/{version}_{variants.pic.name}_{variants.pic.value}'"
  $ spack clean -a
  $ spack install zlib+pic
  $ spack module lmod refresh -y
  $ ls share/spack/lmod/linux-rhel8-x86_64/gcc/11.3.0/zlib
  1.3_pic_True-clzw4fh.lua

### Error message

==> Error: Attempted to format attribute variants.pic.name.Spec variants has no attribute pic

In debug mode:

$ spack -d module lmod refresh -y
==> [2024-07-19-13:06:37.016605] Reading config from file /tmp/spack/etc/spack/defaults/config.yaml ==> [2024-07-19-13:06:37.043014] Imported module from built-in commands ==> [2024-07-19-13:06:37.045784] Imported module from built-in commands
==> [2024-07-19-13:06:37.046775] DATABASE LOCK TIMEOUT: 60s
==> [2024-07-19-13:06:37.046830] PACKAGE LOCK TIMEOUT: No timeout
==> [2024-07-19-13:06:37.047645] Reading config from file /tmp/spack/etc/spack/defaults/repos.yaml ==> [2024-07-19-13:06:37.049472] Reading config from file /tmp/spack/etc/spack/defaults/packages.yaml ==> [2024-07-19-13:06:37.078849] Reading config from file /tmp/spack/etc/spack/defaults/modules.yaml
==> [2024-07-19-13:06:37.088935] Reading config from file /tmp/spack/etc/spack/defaults/linux/modules.yaml ==> [2024-07-19-13:06:37.090516] Reading config from file /home/sup-feverdij/.spack/modules.yaml ==> [2024-07-19-13:06:37.261392] SpecFormatStringError: Attempted to format attribute variants.pic.name.Spec variants has no attribute pic ==> [2024-07-19-13:06:37.261455] Error: Attempted to format attribute variants.pic.name.Spec variants has no attribute pic Traceback (most recent call last): File "/tmp/spack/lib/spack/spack/spec.py", line 4535, in format_attribute current = getattr(current, part) AttributeError: 'VariantMap' object has no attribute 'pic'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/tmp/spack/lib/spack/spack/main.py", line 1070, in main return _main(argv) File "/tmp/spack/lib/spack/spack/main.py", line 1023, in _main return finish_parse_and_run(parser, cmd_name, args, env_format_error) File "/tmp/spack/lib/spack/spack/main.py", line 1053, in finish_parse_and_run return _invoke_command(command, parser, args, unknown) File "/tmp/spack/lib/spack/spack/main.py", line 650, in _invoke_command return_val = command(parser, args) File "/tmp/spack/lib/spack/spack/cmd/module.py", line 26, in module _subcommands[args.module_command](parser, args) File "/tmp/spack/lib/spack/spack/cmd/modules/init.py", line 388, in modules_cmd callbacks[args.subparser_name](module_type, specs, args) File "/tmp/spack/lib/spack/spack/cmd/modules/init.py", line 321, in refresh file2writer[item.layout.filename].append(item) File "/tmp/spack/lib/spack/spack/modules/lmod.py", line 279, in filename f"{self.use_name}.{self.extension}", # file name File "/tmp/spack/lib/spack/spack/modules/common.py", line 588, in use_name name = self.spec.format_path(projection) File "/tmp/spack/lib/spack/spack/spec.py", line 4624, in format_path fs.polite_filename(self.format(part)) for part in input_path_components File "/tmp/spack/lib/spack/spack/spec.py", line 4624, in fs.polite_filename(self.format(part)) for part in input_path_components File "/tmp/spack/lib/spack/spack/spec.py", line 4567, in format return SPEC_FORMAT_RE.sub(format_attribute, format_string).strip() File "/tmp/spack/lib/spack/spack/spec.py", line 4540, in format_attribute raise SpecFormatStringError(m) spack.spec.SpecFormatStringError: Attempted to format attribute variants.pic.name.Spec variants has no attribute pic



### Information on your system

$ git rev-parse --short HEAD
d82c9e7f2a
$ spack debug report
* **Spack:** 0.23.0.dev0 (d82c9e7f2a2f4a59c4e80e7219f57b82dcb87b5d)
* **Python:** 3.6.8
* **Platform:** linux-rhel8-cascadelake
* **Concretizer:** clingo

### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have searched the issues of this repo and believe this is not a duplicate
- [X] I have run the failing commands in debug mode and reported the output
alalazo commented 3 weeks ago

Shorter reproducer:

Spack version 0.23.0.dev0
Python 3.11.5, Linux x86_64
>>> from spack.spec import Spec
>>> s = Spec("zlib-ng").concretized()
>>> s.format("{variants}")
'+compat+new_strategies+opt+pic+shared build_system=autotools'
>>> s.format("{variants.compat}")
Traceback (most recent call last):
  File "/home/culpo/PycharmProjects/spack/lib/spack/spack/spec.py", line 3905, in format_attribute
    current = getattr(current, part)
              ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'VariantMap' object has no attribute 'compat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/culpo/PycharmProjects/spack/lib/spack/spack/spec.py", line 3941, in format
    return SPEC_FORMAT_RE.sub(format_attribute, format_string).strip()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/culpo/PycharmProjects/spack/lib/spack/spack/spec.py", line 3910, in format_attribute
    raise SpecFormatStringError(m)
spack.spec.SpecFormatStringError: Attempted to format attribute variants.compat.Spec variants has no attribute compat

The only place I found where we explicitly mention this feature is the docstring of Spec.format:

https://github.com/spack/spack/blob/b623f58782c53bc465d14b84b44e91ffc2b872cd/lib/spack/spack/spec.py#L3798-L3803