Skip to content

spack setup broken if ignore_deps is requested #12231

@tjfulle

Description

@tjfulle

spack setup -i <package>@<version> results in the following error:

Traceback (most recent call last):
  File "<spack prefix>/bin/spack", line 48, in <module>
    sys.exit(spack.main.main())
  File "<spack prefix>/lib/spack/spack/main.py", line 697, in main
    return _invoke_command(command, parser, args, unknown)
  File "<spack prefix>/lib/spack/spack/main.py", line 447, in _invoke_command
    return_val = command(parser, args)
  File "<spack prefix>/lib/spack/spack/cmd/setup.py", line 166, in setup
    inst_args = parser.parse_args(
UnboundLocalError: local variable 'parser' referenced before assignment

The local variable parser is defined inside the block:

$ cat <spack prefix>/lib/spack/spack/cmd/setup.py
...
        # Install dependencies if requested to do so
        if not args.ignore_deps:
            parser = argparse.ArgumentParser()
            install.setup_parser(parser)

But is later used outside of the scope of this if:

$ cat <spack prefix>/lib/spack/spack/cmd/setup.py
...
        # Install dependencies if requested to do so
        if not args.ignore_deps:
            parser = argparse.ArgumentParser()
            install.setup_parser(parser)
...
        inst_args = copy.deepcopy(args)
        inst_args = parser.parse_args(
            ['--only=package', '--fake'] + args.spec,
            namespace=inst_args
        )

If the user ignores dependencies, you get the error noted above. Creating a new parser object seems to fix the issue:

diff --git a/lib/spack/spack/cmd/setup.py b/lib/spack/spack/cmd/setup.py
index aa391b2..668440d 100644
--- a/lib/spack/spack/cmd/setup.py
+++ b/lib/spack/spack/cmd/setup.py
@@ -45,7 +45,7 @@ def setup_parser(subparser):
 def spack_transitive_include_path():
     return ';'.join(
         os.path.join(dep, 'include')
-        for dep in os.environ['SPACK_DEPENDENCIES'].split(os.pathsep)
+        for dep in os.environ.get('SPACK_DEPENDENCIES', '').split(os.pathsep)
     )


@@ -162,6 +162,8 @@ def setup(self, args):

         # Install this package to register it in the DB and permit
         # module file regeneration
+        parser = argparse.ArgumentParser()
+        install.setup_parser(parser)
         inst_args = copy.deepcopy(args)
         inst_args = parser.parse_args(
             ['--only=package', '--fake'] + args.spec,

This diff also include a fix for the case that SPACK_DEPENDENCIES does not exist in os.environ

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions