[FR][API] Allow resetting env configs in plugins #3507
-
So I'm trying to use Also, it doesn't with non-list settings like Apparently, these dynamic envs inherit cc @gaborbernat any ideas? examples? pointers? I'd love to be able to define envs that don't inherit anything and just populate them myself, instead of having side effects coming from whatever end-users would define in their |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 9 replies
-
You should not be editing those configurations. Instead you should insert a memory loader for the envs too, e.g. tox/tests/plugin/test_plugin.py Line 189 in 5a67ae1 |
Beta Was this translation helpful? Give feedback.
-
Thanks! I was try to do something like that yesterday but it didn't seem to work. That's why I've been experimenting with other ways... I'll try again with your example. |
Beta Was this translation helpful? Give feedback.
-
@gaborbernat |
Beta Was this translation helpful? Give feedback.
-
Do you have a link to the repository for me to take a look? |
Beta Was this translation helpful? Give feedback.
-
That's still local. But I think I figured it out. Tox calls the hook before declaring the |
Beta Was this translation helpful? Give feedback.
-
@gaborbernat here's roughly what I have. I've added comment markers to places that demonstrate my discovery: from tox.config.loader.memory import MemoryLoader
@impl
def tox_add_env_config(env_conf, state):
if env_conf.env_name not in {'pip-compile', 'pip-compile-build-lock', 'pip-compile-tox-env-lock'}:
return
loader = MemoryLoader(deps=['pip-tools'], package='skip')
env_conf.loaders = [loader]
loader.raw['package'] = 'skip' # overrides memloader's args, not needed really; just testing what explodes and what doesn't
env_conf['deps'] # this works
# env_conf.add_constant('package', value='skip', desc='') # <- This makes the following line work but then exploads within tox because it apparently does `add_config()` some time after invoking the `tox_add_env_config()` hook
env_conf['package'] # <- 💣 THIS EXPLODES with KeyError 💣 I kinda expected that everything tox declares natively present in the docs would also be accessible at this hook point. But evidently, it doesn't. I don't really need to access I think, this highlights an issue of possibly incomplete documentation or misleading guarantees. The docs say
and I suppose that could be interpreted as "once the env config is fully initialized" or "some time around the config init". I was reading it as "the config is fully pre-populated by tox at this point". So discovering that it's partially came as a surprise and confused me. I don't know if tox's own config population can be moved before calling the hook but this should probably be acknowledged in the docs. @gaborbernat another thing I'm struggling with is populating commands. I'd like to inject commands referencing the python executable of the venv. I normally do that via pip-compile: commands[0]> '{envpython}' -Im piptools compile --help
pip-compile: Exception running subprocess [Errno 2] No such file or directory: '{envpython}'
pip-compile: exit 2 (0.00 seconds) /full/project/path/to/cwd> '{envpython}' -Im piptools compile --help This also suggests that |
Beta Was this translation helpful? Give feedback.
-
Looks like I'll need to spread the command construction logic across |
Beta Was this translation helpful? Give feedback.
-
Yeah, probably more accurate to say
Yeah, that's not going to work. The substitution is a INI config file feature, not a core tox one. On one end there's no need to use |
Beta Was this translation helpful? Give feedback.
-
The core config should always contain the posargs. |
Beta Was this translation helpful? Give feedback.
-
Do you have an example? I think I saw unparsed configs somewhere but not just the part after |
Beta Was this translation helpful? Give feedback.
-
Not sue I understand what you're asking for. |
Beta Was this translation helpful? Give feedback.
-
I can access Python (replacing @impl
def tox_before_run_commands(tox_env: ToxEnv) -> None:
"""Inject SOURCE_DATE_EPOCH env var into build-dists."""
print(f'tox_before_run_commands: {tox_env=} {tox_env.env_python()=} {tox_env.session.interpreter.executable=}') And then, @impl
def tox_add_env_config(env_conf, state):
print(f'tox_add_env_config: {env_conf=}, {state=}, {state.args=}, {state.conf.pos_args(to_path=None)=}') So this effectively means that I need to avoid populating |
Beta Was this translation helpful? Give feedback.
-
You don't need
Yeah, that's true but you should not be modifying configuration in |
Beta Was this translation helpful? Give feedback.
I don't want ini-ic features in the core layer. E.g. if you look at tox.toml https://tox.wiki/en/4.25.0/config.html#toml-only you see there's plenty things that does differently that ini. Having to do these random string replacement has caused enough bugs that I do not want to go down that path. You're free to fork the project and do whatever you like, but as someone who knows well the design philosophy of tox 4, I'm not interested to go down that path here.