Repository navigation
🐛 fix(xonsh): preserve shell state and path literals - #3411
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/xonsh-activation-state
branch
from
October 16, 2026 14:23
57a5cd6 to
4f8cdbc
Compare
Reported by Anthropic's OSS Scanner: ANT-2026-PPZYMXEQ, ANT-2026-ZBWJ4FLK.
gaborbernat
force-pushed
the
fix/xonsh-activation-state
branch
from
October 16, 2026 14:25
4f8cdbc to
f4425b5
Compare
gaborbernat
marked this pull request as draft
October 16, 2026 23:55
Replace the quote test that still described repr() with the round trip through GBK from pypa#3432, and give quote() the same docstring, so the two PRs merge without conflict in either order. Assert the warning the generator logs when it skips an expanding destination, and run xonsh through one fixture instead of three copies of the subprocess call.
gaborbernat
marked this pull request as ready for review
October 17, 2026 09:24
6 of 9 tasks
Merged
1 task done
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 xonsh activation script exported its own "was unset" marker under the
_OLD_VIRTUAL_<name>names the other shells use for real saved values. Each process started from an activated xonsh inherited_OLD_VIRTUAL_PYTHONHOME=__virtualenv_was_not_set__. A bash started from that shell, aftersource bin/activateanddeactivate, ended withPYTHONHOMEset to that string, and any Python it then ran failed during path configuration. A destination directory whose name contains a set variable, such asenv$HOME, went onto$PATHas a different directory, sopythonresolved elsewhere while$VIRTUAL_ENVsaid the environment was active. A script read under$XONSH_ENCODING=gbkorbig5could lose the backslash in front of an escaped quote, so the rest of the prompt or path value ran as code. 🐛 After this PR the unset bookkeeping lives on the activator object and is never exported, the generator refuses a destination that xonsh would expand, and every generated literal is ASCII. The literal change is the one #3432 carries on its own, so the two merge in either order.The unset marker was exported as a saved value
_savewrote either the variable's value or the marker string into$_OLD_VIRTUAL_<name>, a design from #3125. xonsh passes its environment variables to the processes it starts (environment variables), so the marker left the shell. Within the same shelldeactivaterecognised the marker and removed the variable. The bash script does not know the marker: itsdeactivaterestoresPYTHONHOMEfrom any non-empty_OLD_VIRTUAL_PYTHONHOME, andsource bin/activaterunsdeactivate nondestructivefirst. A child bash therefore setPYTHONHOMEto the marker before it saved and unset it, and put the marker back ondeactivate. WithPYTHONHOME=__virtualenv_was_not_set__CPython stops at startup with a path configuration error. The same happened toTCL_LIBRARYandTK_LIBRARYwhen the environment set them. A user whosePYTHONHOMEwas the marker string got it unset instead of restored._savenow exports a backup only when the variable had a value. Otherwise it adds the name tounset_vars, a set on the activator object, and removes any stale backup.deactivaterestores from the backup when one is exported and otherwise removes the variables recorded as unset. Any saved value, the former marker included, is restored as a value. The record now lives on the object, so a second script sourced into the same shell must not discard the first object's record before it runs: the module-level code callsdeactivate(["nondestructive"])on the previous instance before it replaces it. Switching from an environment that setTCL_LIBRARYto one that does not now leaves the variable unset, which the switch test checks. The marker test checks that no backup is exported for an unsetPYTHONHOMEand that a real value equal to the marker comes back. Both tests run xonsh, so they skip where it is not installed, which includes CI.sequenceDiagram participant X as xonsh parent participant E as exported environment participant B as child bash rect rgb(255, 214, 214) X->>E: _OLD_VIRTUAL_PYTHONHOME = __virtualenv_was_not_set__ E-->>B: inherited at start B->>B: source bin/activate, deactivate Note over B: PYTHONHOME is the marker, python fails to start end rect rgb(216, 243, 220) X->>X: unset_vars = {"PYTHONHOME"} on the activator object E-->>B: no backup variable Note over B: PYTHONHOME stays unset endPATH entries went through variable and tilde expansion
The template puts the literal destination joined with the bin directory at the front of
$PATH. xonsh keeps$PATHas anEnvPath(environment types), and each read of an entry goes through_expandpath. With$EXPAND_ENV_VARSon, its default,expandvarsreplaces$NAMEand${'NAME'}for each variable that is set, andexpand_pathapplies~expansion to the text after the first=, around eachos.pathsep, the rule bash uses for assignments. Command lookup and child process environments see the expanded entries. An environment at/work/env$HOMEput/work/env/home/user/binon$PATH, a directory that does not exist, sopythonresolved to the next interpreter on the path. Quoting cannot prevent this: the expansion runs on the value after the literal is parsed. Theos.pathsepcase cannot arise, since #3399 rejects such destinations; a~right after=can.generatenow matches the destination against\$(?:\w+|{)|=~. On a match it logs a warning that names the destination, removes anactivate.xshleft by an earlier run, and returns no files; the other activators still run. The pattern is wider than xonsh's:${HOME}is refused although xonsh leaves it alone, and a$not followed by a word character or{passes. The skip test covers the three forms, the warning text and the removed file.flowchart LR L["literal '/work/env$HOME/bin'"] --> P["Python string parsed"] P --> S["stored as a PATH entry"] S --> R["read through _expandpath"] R --> V["expandvars: $HOME replaced"] V --> T["expand_path: ~ after = replaced"] T --> X["/work/env/home/user/bin on PATH"] G["generate: destination matches"] --> W["warning, no activate.xsh"] classDef bad fill:#ffd6d6,stroke:#c1121f,color:#5c0000; classDef ok fill:#d8f3dc,stroke:#2d6a4f,color:#1b4332; class X bad; class W ok;Source decoding merged a backslash into the character before it
quotereturnedreprof the value, which keeps printable non-ASCII characters as they are, and the file is written as UTF-8. xonsh'ssourcereads the file with$XONSH_ENCODING(source_alias_fn). In GBK and Big5 a lead byte between 0x81 and 0xFE takes the next byte in the range 0x40 to 0xFE as its trail. The last UTF-8 byte of中is 0xAD, so中\decodes as涓璡and the backslash is gone. A value with both kinds of quote is written as'中\'…', which under GBK becomes'涓璡'…': the literal ends after two characters and the parser reads the rest of the value as code.quotenow returnsasciiof the value (quote), so every byte of the file is below 0x80 and no double-byte codec pairs them; the parser turns中back into中. The quote test round-trips the literal through GBK andast.literal_eval, and the xonsh test sources a script with a non-ASCII path and prompt under GBK and Big5. This also addresses GHSA-394h-qwmc-48pf; #3432 has the full account.sequenceDiagram participant G as generator participant D as xonsh source decoder participant P as literal parser rect rgb(255, 214, 214) G->>D: repr, bytes e4 b8 ad 5c 27 for 中\' D->>P: GBK pairs ad 5c, the backslash is gone Note over P: literal closes early, the rest runs as code end rect rgb(216, 243, 220) G->>D: ascii, every byte below 0x80 D->>P: text unchanged under any ASCII-compatible codec Note over P: escapes decode back to the original value endBehavior changes
Activation no longer exports
_OLD_VIRTUAL_<name>for a variable that was unset; child processes see no backup variable. A saved value equal to__virtualenv_was_not_set__is restored as a value. A child shell started from a parent activated with an older script still inherits the marker as a backup, and the new script restores it as a value, sodeactivatein that child leavesPYTHONHOMEset to the marker until the parent environment is regenerated and re-activated. Sourcing a second environment's script deactivates the first environment before it activates the second. A destination containing$followed by a word character or{, or containing=~, gets noactivate.xsh: virtualenv logs a warning, removes an existingactivate.xsh, and generates the other activators as before. Non-ASCII characters in paths and prompts are written as escapes. Scripts generated by earlier versions keep their old literals and backup handling until the environment is regenerated.Reported by Anthropic's OSS Scanner: ANT-2026-PPZYMXEQ, ANT-2026-ZBWJ4FLK.