Fix JSONDecodeError when using compiler modules (#25624)

When using modules for compiler (and/or external package), if a
package's `setup_[dependent_]build_environment` sets `PYTHONHOME`, it
can influence the python subprocess executed to gather module
information.

The error seen was:

```
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
```

But the actual hidden error happened in the `python -c 'import
json...'` subprocess, which made it return an empty string as json:

```
ModuleNotFoundError: No module named 'encodings'
```

This fix uses `python -E` to ignore `PYTHONHOME` and
`PYTHONPATH`. Should be safe here because the python subprocess code
only use packages built-in python.

The python subprocess in `environment.py` was also patched to be safe
and consistent.
This commit is contained in:
Jordan Galby 2021-10-03 16:10:33 +02:00 committed by GitHub
parent b9e72557e8
commit 0d6a2381b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 2 deletions

View File

@ -989,7 +989,7 @@ def _source_single_file(file_and_args, environment):
python_cmd = python_cmd.name if python_cmd else sys.executable
dump_cmd = 'import os, json; print(json.dumps(dict(os.environ)))'
dump_environment = python_cmd + ' -c "{0}"'.format(dump_cmd)
dump_environment = python_cmd + ' -E -c "{0}"'.format(dump_cmd)
# Try to source the file
source_file_arguments = ' '.join([

View File

@ -47,7 +47,7 @@ def module(*args):
module_cmd += 'LD_LIBRARY_PATH="$SPACK_LD_LIBRARY_PATH" '
# Execute the python command
module_cmd += '%s -c "%s";' % (sys.executable, py_cmd)
module_cmd += '%s -E -c "%s";' % (sys.executable, py_cmd)
# If LD_LIBRARY_PATH was set after `module`, dump the old value because
# we have since corrupted it to ensure python would run.