nox: [BUG] --no-reuse-existing-virtualenvs doesn't seem to work

Describe the bug

Adding --no-reuse-existing-virtualenvs does not seem to ignore the cache, and instead just reuses the existing environments anyway. Happened on the previous version, and then did a brew upgrade, and 2021.10.1 also has this problem. Unless I misunderstand this option, pretty sure it’s broken.

How to reproduce

@nox.session(reuse_venv=True)
def type(session):
    session.install('mypy')
    session.run('mypy')

(simplified slightly, should be unimportant)

$ nox --no-reuse-existing-virtualenvs -s type
nox > Running session type
nox > Re-using existing virtual environment at .nox/type.

Expected behavior The existing virtual environment should not have been reused. (It really was, not just visually; I was trying to remove an item from the install list, but it remained installed).

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 18 (2 by maintainers)

Most upvoted comments

Alright, I think I understand what you’re getting at:

  • Without anything passed on the command-line, sessions marked True will be reused - this is the default behavior and matches the intended current behavior.
  • Command-line --reuse (True) reuses envs for all sessions except ones marked False. This matches the intended current behavior (that isn’t working correctly).
  • Command-line --reuse=always forces every session to re-use its env, and is not a valid option for local specification.
  • Command-line --reuse=never forces every session to re-create its env, and is not a valid option for local specification.

If that’s the case, that works for me.

Hmm… Ideally we’d do the following (but it’s more work):

  1. Use the command-line option if it’s set (either way).
  2. Use the per-session setting from the noxfile, if it’s not None.
  3. Use the global nox.options setting from the noxfile, if it’s not None.
  4. Don’t reuse.

So this would split your True/False row in two cases, depending on where the global setting comes from.

Consider this noxfile:

import nox
nox.options.reuse_existing_virtualenvs = False
@nox.session(reuse_venvs=True):
def test(session):
    pass

I’d be surprised if the test session was not reused.

On the other hand, I’d expect nox --no-reuse-existing-virtualenvs --session test to disable reuse.

This behavior is definitely ambiguous if we’re just using True/False. What do y’all think using “yes”, “no”, “always” and “never”?

So:

  • Command-line --reuse=yes reuses envs for all sessions except ones marked “never”.
  • Command-line --reuse=no (or unset) only reuses sessions marked “always”.
  • Command-line --reuse=always forces every session to re-use its env.
  • Command-line --reuse=never forces every session to re-create its env.

The noxfile.py option nox.options.reuse_existing_virtualenvs should behave the same as the other noxfile.py options - it provides a default for the command-line arg if it isn’t specified.