determine_setup: use invocation_dir as rootdir when -c is given#14454
determine_setup: use invocation_dir as rootdir when -c is given#14454EternalRights wants to merge 3 commits intopytest-dev:mainfrom
Conversation
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
Im wondering if the common root ought to be used in case of sub/siblings
c7d396b to
55123ac
Compare
|
Good point. Let me think through the scenarios:
My reasoning for sticking with That said, I am open to switching to |
|
thanks you for the elaboration but args outside the tree is also the case im concerned in a valid case - like working in a subdir, using the config dir thats now a siebling it may be a better solution to warn about using invocation dir as rootdir when the intent is not clear and recommending explict rootdir to disambiguate good intentions have been used to pave this road |
The warning approach makes sense. I've updated the PR with a warning "rootdir was set to <invocation_dir> because -c was given without --rootdir. This keeps the common case silent while giving users a clear action for edge cases. |
Found this while testing a project with pytest -c config/pytest.ini. rootdir ended up pointing to config/ instead of the project root, which broke conftest discovery since conftests are resolved relative to rootdir. Turned out `rootdir = inipath_.parent` was the culprit - it makes rootdir jump to wherever the config file lives, ignoring invocation_dir. Simple fix: use invocation_dir instead.
78f4aad to
b399b23
Compare
for more information, see https://pre-commit.ci
|
One more thing — the RTD build seems to be failing on a pre-existing Sphinx toctree warning (unrelated to these changes). Just a heads-up. |
|
|
||
| if ns.inifilename and not ns.rootdir: | ||
| if inipath is not None and inipath.parent != self.invocation_params.dir: | ||
| log.warning( |
There was a problem hiding this comment.
pytest doesnt use logging for ux we might want to find a differnt place to output this
@nicoddemus @bluetech any opinion on this - personally i lean towards the pre collection summary header that shows pytest/plugin versions + config paths already but something about that feels icky as well
|
@RonnyPfannschmidt Fair point, logging was a bad call. I should have thought about pytest's UX conventions before reaching for that. If the consensus is the pre-collection summary header, happy to move it there. Another option could be printing it via terminal.write_line alongside the config path info that already shows up. But I'll wait to hear what nicoddemus and bluetech think. |
Ran into this while playing around with
pytest -c config/pytest.ini- rootdir was jumping to config/ instead of staying at the project root. Broke conftest discovery in a pretty confusing way.Turned out the root cause is one line in determine_setup:
rootdir = inipath_.parent. When-cpoints to a file in a subdirectory, that line makes rootdir follow the config file, ignoring where you actually invoked pytest from.The fix is to use
invocation_dirinstead (which is what everything else in determine_setup already does when no-cis given). Ronny confirmed this is a regression on the issue, so no design discussion needed here.Also cleaned up
test_with_specific_inifilewhich was usingPath.cwd()as invocation_dir (non-deterministic) - changed it to tmp_path like every other test in the class. Added a new test specifically for the subdir-config scenario.Would appreciate a review!