Skip to content

Conversation

cjolowicz
Copy link
Collaborator

@cjolowicz cjolowicz commented May 24, 2021

Follow-up to #418 and #425

This PR contains two changes to the staleness check when reusing environments:

1. Fix environments not being reused due to wrong Python lookup

When determining the Python base installation of an existing environment, allow nox.command.run to find the Python executable in the virtualenv, by passing VirtualEnv.bin_paths as the paths argument.

The original PR (#418) passed os.path.join(self.location, "bin", "python"). This does not work for two reasons:

  • The directory is named "Scripts" on Windows.
  • The executable is named "python.exe" on Windows.

The follow-up PR (#425) passed bare "python". This does not work, because nox.command.run searches PATH by default. It needs to be told to search the virtualenv instead.

This fix exposed portability flaws in the test suite on Windows. Some tests messed with the pyvenv.cfg files, which confused the Windows Python launcher. We solve this by skipping one non-essential test on Windows, and by preserving the original pyvenv.cfg keys in another test.

2. Don't log interpreter checks when reusing environments

This gets rid of some clutter in the Nox output. Here's an example for such clutter:

nox > Running session test
nox > /usr/local/bin/python -c import sys; print(getattr(sys, 'real_prefix', sys.base_prefix))
nox > python -c import sys; print(getattr(sys, 'real_prefix', sys.base_prefix))
nox > Re-using existing virtual environment at .nox/test.
nox > Session test was successful.

@cjolowicz cjolowicz marked this pull request as draft May 24, 2021 15:23
cjolowicz added 3 commits May 24, 2021 18:13
When determining the Python base installation of an existing environment, allow
`nox.command.run` to find the Python executable in the virtualenv, by passing
`VirtualEnv.bin_paths` as the `paths` argument.

The original PR passed `os.path.join(self.location, "bin", "python")`. This does
not work for two reasons:

- The directory is named "Scripts" on Windows.
- The executable is named "python.exe" on Windows.

The follow-up PR passed bare "python". This does not work, because
`nox.command.run` searches PATH by default. It needs to be told to search the
virtualenv instead.
This test results in an error 'No pyvenv.cfg file' on the Windows 2019 runner:

    [...]
    File "D:\a\nox\nox\tests\test_virtualenv.py", line 438, in test_create_reuse_oldstyle_virtualenv_environment
      reused = not venv.create()
    File "d:\a\nox\nox\nox\virtualenv.py", line 420, in create
      if not self._clean_location():
    File "d:\a\nox\nox\nox\virtualenv.py", line 313, in _clean_location
      and self._check_reused_environment_interpreter()
    File "d:\a\nox\nox\nox\virtualenv.py", line 342, in _check_reused_environment_interpreter
      ["python", "-c", program], silent=True, log=False, paths=self.bin_paths
    File "d:\a\nox\nox\nox\command.py", line 118, in run
      full_cmd, return_code, ":" if silent else ""
  Message: "Command python -c import sys; print(getattr(sys, 'real_prefix', sys.base_prefix)) failed with exit code 106:"
  Arguments: ()
  No pyvenv.cfg file

  ------------------------------ Captured log call ------------------------------
  INFO     nox:virtualenv.py:440 Creating virtual environment (virtualenv) using python.exe in D:\a\nox\nox\.nox\tests-3-7\tmp\pytest-of-runneradmin\pytest-0\test_create_reuse_oldstyle_vir0\venv
  ERROR    nox:command.py:118 Command python -c import sys; print(getattr(sys, 'real_prefix', sys.base_prefix)) failed with exit code 106:

The error message appears to come from the Windows Python launcher (see
cpython/PC/launcher.c).
The test places a spurious occurrence of "virtualenv" in the pyvenv.cfg file, to
check for robust parsing. This was done by modifying the `home` key to point to
a fictitious installation at /opt/virtualenv/bin.

Unfortunately, this leads to the following error on Windows when determining the
Python base installation:

    No Python at '/opt/virtualenv/bin\python.exe'

Avoid this error by using an entirely bogus key, which should be ignored by
Python and other tools:

    bogus = virtualenv
@cjolowicz cjolowicz marked this pull request as ready for review May 24, 2021 16:35
@cjolowicz cjolowicz requested a review from theacodes May 24, 2021 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants