pytest-dev / pytest-dev/pytest

Change how nodeids are created in relation to rootdir

Open
#11,245 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

This issue is about the path part of the nodeid (the part before the ::).

Problem

Generally, the path part of a nodeid is created by taking the node's path relative to the rootdir.
But, if the node's path is not below the rootdir ("out of tree"), then we create its nodeid relative to the initial paths (= paths given in the command line), specifically the first one which contains the path. This was added in 14b6380e5f8c2e26aa518de8a499978eb9601848 as a fix for an issue with out-of-tree --pyargs (2775) which would result in an empty nodeid path.

This change broke an important property of the nodeid, that it is always relative to the rootdir, or said differently, that the nodeid cannot be relied upon to be the root of all nodeids. This causes various issues that crop up when such invariants are broken:

  • ambiguous nodeids
  • duplicate nodeids
  • incorrect printing of nodeids

See #11186, #6605, #3714.

The issues mostly stem from the fact that a nodeid does not carry around the context of which path it it relative to, so the context is lost and becomes ambiguous.

Proposed solution

Perhaps the simplest fix is to change back to always using the rootdir, but now allowing .. (parent directory) segments in the nodeid path. This is kind of ugly, but running tests outside the rootdir is discouraged anyway, so I'm not too worried about it, in fact it might encourage settings the rootdir correctly...

I have an initial implementation here: https://github.com/bluetech/pytest/commits/rootdir-rel

Other solutions?

Another solution might be to change nodeids to be structured and maintain the context. However, users still interact with nodeids as strings so I don't think this can go very far.

And another solution can be that out-of-tree nodeids just always use absolute paths e.g. /my/out/of/tree/test.py::test_it instead of ../../of/tree/test.py::test_it. Maybe it's better?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the nodeid and rootdir behavior described here and the initial implementation on the rootdir-rel branch, then compare the alternatives against #11186, #6605, and #3714. Done means an agreed change removes ambiguous, duplicate, and incorrectly printed nodeids for out-of-tree paths while preserving the relevant --pyargs behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.