Skip to content

🐛 fix(activation): honor .pth import precedence - #3653

Merged
gaborbernat merged 4 commits into
pypa:mainfrom
gaborbernat:fix/adko-pth-precedence
Nov 20, 2026
Merged

gaborbernat merged 4 commits into
pypa:mainfrom
gaborbernat:fix/adko-pth-precedence

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The old activate_this.py processed environment .pth files before moving environment libraries ahead of host paths. A hook could load a same-name host module for users activating inside a running Python process. The script now puts environment libraries ahead of host paths for those imports while keeping standard library paths first. Fixes #3623.

.pth imports saw host paths first

site.addsitedir() processes executable .pth lines during the call. The old path rotation happened afterward, so the hook could resolve a module from the host working directory or site-packages before the environment. The new _add_libraries() orders interpreter library paths, environment libraries, then host paths while the hooks run. Interpreter paths include GraalPy's implementation modules. The script records .pth path insertions at any position and then restores the original host entries behind the added paths.

sequenceDiagram
    participant P as host Python
    participant A as activate_this.py
    participant S as site.addsitedir
    participant I as import machinery
    P->>+A: runpy.run_path
    rect rgb(255, 214, 214)
    Note over A,I: Before: host path precedes environment
    A->>+S: process environment .pth
    S->>+I: import hook module
    I-->>-S: host copy wins
    S-->>-A: return
    end
    rect rgb(216, 243, 220)
    Note over A,I: Now: interpreter libraries, environment, host
    A->>+S: process environment .pth
    S->>+I: import hook module
    I-->>-S: environment or interpreter copy wins
    S-->>-A: return
    end
    A-->>-P: restore post-activation sys.path
Loading

Behavior changes

An environment .pth hook now finds an environment module before a same-name module on the host path. Standard library and GraalPy implementation modules retain precedence during .pth processing. A .pth hook that inserts a new path at the front keeps that path after activation. The change does not reload modules already present in sys.modules. Host path entries keep their relative order after activation.

Reported by @oss-scanner: ANT-2026-ADKO6NBD.

@gaborbernat
gaborbernat marked this pull request as draft November 20, 2026 06:33
@gaborbernat
gaborbernat force-pushed the fix/adko-pth-precedence branch 3 times, most recently from 31478da to 9b2cdc9 Compare November 20, 2026 08:25
activate_this.py ran .pth hooks before moving environment paths ahead of
host paths. A same-name host module could win during activation.

Put standard library paths first, then environment libraries, then host
paths while the hooks run. Restore the existing post-activation order.

Fixes pypa#3623
The temporary lookup order grouped environment libraries ahead of paths
added by earlier .pth files in the final sys.path. Record additions per
library to retain the prior order when lib directories differ.

Refs pypa#3623
GraalPy keeps implementation modules outside its standard library
directory. The initial lookup list missed them and let environment
site-packages shadow runtime modules.

Executable .pth lines can also insert paths before existing entries.
Record additions regardless of insertion position when restoring the
post-activation path.

Refs pypa#3623
The base-prefix filter could promote an unrelated host path ahead of
the environment if that path was under the interpreter prefix. Match
the known standard library locations and GraalPy's implementation
module directory instead.

Use a dict to count path additions so activation imports no extra
module before its temporary search order is installed.

Refs pypa#3623
@gaborbernat
gaborbernat force-pushed the fix/adko-pth-precedence branch from 9b2cdc9 to 501d391 Compare November 20, 2026 09:09
@gaborbernat
gaborbernat marked this pull request as ready for review November 20, 2026 10:09
@gaborbernat
gaborbernat merged commit f96f3b0 into pypa:main Nov 20, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

activate_this.py can resolve .pth imports from host first

1 participant