Repository navigation
馃悰 fix(activate): stop when relocation cannot resolve - #3409
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/activation-relocation
branch
from
October 16, 2026 14:22
800d683 to
2d43ecc
Compare
Reported by Anthropic's OSS Scanner: ANT-2026-WUTWEG5X.
gaborbernat
force-pushed
the
fix/activation-relocation
branch
from
October 16, 2026 14:25
2d43ecc to
289ffc8
Compare
The relocation check compared the sourced file with bin/activate, so a moved environment under Git Bash on Windows, whose scripts live in Scripts, was refused. Compare against the generated bin name instead, drop the bash check the outer guard already makes, and have the failure tests assert PATH and VIRTUAL_ENV stay untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bash
activatescript carries the absolute path of its environment. When that directory is gone, because the environment was moved after creation, the script locates the environment from the file being sourced. That lookup could fail and the script carried on with whatever came out of it. Witheval "$(cat bin/activate)", or the script fed to bash on standard input,BASH_SOURCEis empty:realpath ''failed,dirnameof the empty result gave., andVIRTUAL_ENVbecame the parent of the working directory with<parent>/binat the front ofPATH. Withsource <(cat bin/activate)the source is a pipe under/dev/fd, the lastrealpathfailed, andPATHgained/binfrom an emptyVIRTUAL_ENV. WithoutrealpathonPATH, which macOS lacked before 13, all three lookups printedcommand not foundand the same/binentry was added. Every case ended with status 0. 馃悰 Relocation now stops with status 1, and leavesPATH,PS1andVIRTUAL_ENVas they were, unless bash sourced the file by its own path, every lookup succeeded, the file is theactivateof the resolved directory's bin directory, and that directory holds apyvenv.cfg(the marker file every virtual environment carries).A failed lookup fed the next step
The fallback ran
realpath,dirnameandrealpathin a row and checked none of them. #2996 added it reading$0, and #3091 switched it toBASH_SOURCE, the array bash fills with the file names of sourced scripts. The variable is empty when the text reaches bash throughevalor standard input, and other shells do not set it. Each lookup now ends with|| return 1. Thereturnbuiltin stops a sourced file, so the tool's own error message is the last output and nothing after that line runs. Before any lookup, a guard checks that the environment directory is missing, thatBASH_VERSIONis set and thatBASH_SOURCEis not empty. When the guard fails it printsRelocating this activation script requires sourcing it from bash., andfalseleaves status 1; the rest of the script sits in theelsebranch, so no variable changes. With<(cat bin/activate)the file name is/dev/fd/<n>, whose/proc/<pid>/fdentry points at a pipe, and therealpathof its parent directory fails.flowchart LR S["BASH_SOURCE[0]"] --> G{bash and non-empty?} G -- no --> M[message, status 1] G -- yes --> R1[realpath] R1 -- fails --> X1[return 1] R1 --> D[dirname] D --> R2["realpath dir/.."] R2 -- fails --> X2[return 1] R2 --> C{same file as env/bin/activate and pyvenv.cfg?} C -- no --> X3["Cannot identify relocated environment, return 1"] C -- yes --> E[export VIRTUAL_ENV, prepend PATH] S -. before: empty name .-> R1 R1 -. before: empty result .-> D D -. before: dot .-> P[parent of the working directory] P --> E0[export VIRTUAL_ENV, prepend PATH] classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000; classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332; class P,E0 bad; class M,X1,X2,X3,E ok;Any directory above the script counted as the environment
The old fallback took the parent of the sourced file's directory as the environment, whatever that file was: a copy of
activatedropped into some otherbin, or a wrapper script thatevals the real one. The script now compares the sourced file with<env>/<bin>/activatethrough-ef, which is true when both names have the same device and inode numbers, and requirespyvenv.cfgin that directory. The bin directory name is the one the generator wrote into the script fromcreator.bin_dir, soScriptsunder Git Bash on Windows passes likebinelsewhere. A symlink to the realactivatepasses too, sincerealpathresolved it. Anything else printsCannot identify relocated environment: <dir>and returns 1.stateDiagram-v2 [*] --> Resolved: realpath and dirname succeed Resolved --> Guessed: before, parent taken as is Guessed --> Activated Resolved --> Checked: now, file -ef env/bin/activate Checked --> Refused: other file, or no pyvenv.cfg Checked --> Activated: same inode and pyvenv.cfg present Refused --> [*]: status 1, nothing exported classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000 classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332 class Guessed bad class Checked,Refused okBehavior changes
Activating an environment at its original path is unchanged. Activating a moved environment requires bash with
realpathanddirnameonPATH, and the script sourced by its own path.eval "$(cat bin/activate)", the script on standard input, and shells other than bash printRelocating this activation script requires sourcing it from bash.and stop with status 1;PATH,PS1andVIRTUAL_ENVare left as they were.source <(cat bin/activate)stops with arealpatherror. A missingrealpathordirnamestops with that tool'scommand not found. A moved environment withoutpyvenv.cfg, a copy ofactivateoutside its bin directory, and a wrapper thatevals the script are refused withCannot identify relocated environment. A moved environment sourced from Git Bash on Windows, whose bin directory isScripts, activates.Reported by Anthropic's OSS Scanner: ANT-2026-WUTWEG5X.