fix(nodeenv): keep the python virtualenv's scripts under -p on Windows - #411
Merged
Merged
Conversation
`nodeenv -p` decided between appending and overwriting by whether the file had prompt-disable content, and only the posix `activate` and `activate.fish` have any. "activate.bat", "deactivate.bat" and "Activate.ps1" were therefore overwritten, so VIRTUAL_ENV and the virtualenv's own deactivation were thrown away. The decision now goes by the file: everything python's virtualenv writes is extended. Activate.ps1 defines `deactivate` too, so the appended part saves the one already in place and calls it when the user really deactivates, instead of taking the name over for good. writefile() now starts an appended part on a line of its own: a "deactivate.bat" ending with `:END` and no trailing newline would otherwise swallow the first appended line into the label. #243
The Activate.ps1 python ships on Windows is signed, and PowerShell will not parse a script with code after its signature block: the whole activation failed with "Executable script code found in signature block" instead of only losing the prompt. writefile() now inserts an appended part in front of the block. The signature no longer matches, but editing the script voids it whichever end the new part goes to. The PowerShell probe runs with $ErrorActionPreference = 'Stop' and its stderr folded into stdout, so a script that cannot be parsed fails the test that runs it instead of the next one.
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.
Fixes #243.
The problem
nodeenv -pinside a python virtualenv on Windows overwroteScripts/activate.bat,Scripts/deactivate.batandScripts/Activate.ps1, soVIRTUAL_ENVand the virtualenv's owndeactivation were gone after the run. Only the posix
Scripts/activatesurvived, because that one is appended.
install_activate()picked append over overwrite bybool(disable_prompt), andDISABLE_PROMPThas entries only foractivateandactivate.fish, never for the cmd and PowerShellscripts.
The fix
writes is extended, listed in
PYTHON_VIRTUALENV_FILES. For posixthis is the same set as before, and
shim/nodeare stilloverwritten
ACTIVATE_PS1definesdeactivateas well, and so does thevirtualenv's own
Activate.ps1it is appended to. It now saves theone already in place as
_OLD_NODE_DEACTIVATEand puts it back andcalls it on a real deactivate. The internal
deactivate -nondestructivedoes not chain, or it would undo the activation thatjust happened above it
writefile()starts an appended part on a line of its own: adeactivate.batending with:ENDand no trailing newline swallowsthe first appended line into the label
ACTIVATE_BATandDEACTIVATE_BATare unchanged. Under-pbothenvironments share one directory, so the
_OLD_VIRTUAL_PROMPT/_OLD_VIRTUAL_PATHbookkeeping the two scripts have in common composescorrectly: PATH and PROMPT after activation are the ones the virtualenv
set, and on deactivation the virtualenv's part runs first and clears
them, so the appended part leaves them alone.
Tests
tests/test_install_activate.py: the three cmd/PowerShell scriptskeep the content the virtualenv wrote, and
Activate.ps1keeps adeactivatethat is already theretests/nodeenv_test.py:writefile()append starts on a new linetests/test_activate_win.py: new, the counterpart oftest_activate_shells.pyfor cmd and PowerShell. It runs the scriptsfor real and checks
VIRTUAL_ENV,NODE_VIRTUAL_ENVand PATH afteractivation and after deactivation. Needs a Windows host, so it only
runs in the windows-latest jobs