You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(utils): add Windows nodeenv migration helper - #452
In an existing project with both Python and Node.js, I wanted activating the
Python virtual environment to also select the project's already installed
Node/npm. The runtimes had been set up separately, so consolidating them meant
manually moving files without replacing the Python activation scripts and then
editing activation and deactivation for each Windows shell.
This adds an optional, repeatable utility for that migration: simply add an
existing Node installation to an existing Python environment, reuse the installed
files, and use one activation entry point for both runtimes.
For a new setup, nodeenv -p is strongly recommended. Create and activate the
Python venv first, then run nodeenv -p. The new utility is for consolidating
environments that already exist, without downloading Node/npm again.
Changes
Add utils/integrate_nodeenv.py with --source, --venv and --dry-run.
Defaults are env and .venv in the project's current working directory.
Move missing files and directories while preserving existing destination
entries and retaining their source copies. Reject overlapping roots,
incompatible file/directory collisions and unsafe activation links.
Validate all four activation files before moving anything, then integrate NODE_PATH save/restore into CMD, PowerShell and Git Bash activation and
deactivation. Repeated runs preserve existing recognized patches and mtimes.
Keep the source directory until the user removes it. Report that it can be
deleted only after migration and patching both succeed.
Document the motivation, recommended nodeenv -p workflow and migration steps
in utils/README.md; link it from the main README and add a changelog entry.
Cover the helper with offline tests and include utils in flake8 checks.
The utility is Windows-only, runs on Python 3.10+, and accepts recognized CPython
activation layouts. It preserves UTF-8 BOMs and line endings. An edited Activate.ps1 loses its original signature; execution policy is left unchanged.
Validation
Tested locally on Windows with Python 3.12.10 and Git Bash available on PATH:
The 40 new tests cover migration/no overwrite, dry runs, preflight failures,
repeated runs, working-directory defaults, junction handling, platform checks,
line endings, BOMs and PowerShell signatures. Download-based integration tests
were excluded from the local run.
Tests cover the change.
Tests and flake8 pass using the direct commands from CONTRIBUTING.md.
CHANGES has an entry under Version [unreleased].
README.rst and utils/README.md describe the workflow.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch! Three things before merge.
The anchors match only the newest CPython venv templates. activate.bat has the quoted set "PATH=%VIRTUAL_ENV%\Scripts;%PATH%" in 3.12.10, 3.13.5 and later, but 3.10, 3.11, 3.12.0-3.12.7 and 3.13.0 write it without quotes. The bash activate has # Call hash to forget past locations. in the same versions, while older ones say # Call hash to forget past commands. or # This should detect bash and zsh. For a venv made by any of those, the utility stops with expected one insertion point, though the README says Python 3.10+. I ran patch_activation on the templates from the CPython tags: 3.10.11, 3.11.9, 3.12.0, 3.12.7 and 3.13.0 fail, 3.12.10, 3.13.5, 3.13.9 and 3.14.0 pass. The test fixtures are hand-written excerpts, so they don't catch it.
The inserted blocks also differ from what nodeenv puts into these scripts. They never set npm_config_prefix, so #309 comes back in a migrated venv: npx exports a prefix and Scripts\npm.cmd then runs the npm found under it. In Git Bash they clear NODE_PATH, while nodeenv's activate sets it to Scripts/node_modules.
nodeenv can already do this part. Since 1.11.0, nodeenv -p .venv --node=<version> skips the download when that node version is already in the venv (#403), and then appends its own blocks to activate, activate.bat, deactivate.bat and Activate.ps1, including the PowerShell signature handling. So the utility could move the files and then run that command with the installed version (or the README could tell the user to run it), and PATCHES and patch_activation with their tests can go. I checked it on macOS with a venv that already held node 26.13.0: nothing was downloaded, and activate got the NODE_PATH and npm_config_prefix blocks. I haven't tried it on Windows.
If a rename fails halfway, for example because an editor or a dev server holds node.exe or a file under node_modules, the entries moved before it stay in .venv, the activation scripts are not changed, and neither environment works. Please move them back on failure, or at least list what was moved in the error. Also, "You can now delete the source directory" is printed when skipped > 0 too. The files kept in env are not compared with those in .venv, so the user can be told to delete the only copy of a different node.exe. I'd print it only when nothing was skipped, and list the skipped entries otherwise.
README.rst is also the PyPI description (setup.py reads it), and the relative link utils/README.md doesn't resolve on pypi.org. A full GitHub URL works in both places.
Thanks for the review.
I switched the helper to native nodeenv -p, added rollback for failed tasks, and made list for skipped entries.
The cleanup message now appears only when no entries were skipped.
I fixed the README link and rebased on current master.
I've verified work on Windows 11 (und 10), on some of my existed projects.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Active issues can leave Python activation unvalidated, mix or duplicate CRLF activation patches, and leave Node environment variables set after normal Python deactivation.
Adds an optional Windows migration helper for consolidating an existing Node environment into a Python venv, complementing the recommended nodeenv -p workflow for new setups.
Changes:
Adds migration planning, collision checks, dry-run output, move rollback, and activation through nodeenv -p.
Adds offline tests for migration behavior and failure handling.
Documents the workflow and includes utils in lint checks.
File
Description
utils/README.md
Documents migration usage, prerequisites, and limitations.
utils/integrate_nodeenv.py
Implements the Windows migration helper.
tox.ini
Adds utilities to tox lint checks.
tests/test_integrate_nodeenv.py
Tests migration planning, safety checks, and failures.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Migration can break absolute symlinks or junctions targeting other moved entries, including nested links, without recording a skip and may then recommend deleting the source.
Detect or rebase links to source entries during migration
utils/integrate_nodeenv.py:88
Moving an absolute symlink or junction that targets another source entry leaves it pointing at the old location after that target moves. For example, env/linked-package -> env/packages/package becomes broken in .venv, yet migration records no skip and can recommend deleting the source. During preflight, detect these links—including those inside directories moved wholesale—and either reject them or rebase their targets to the destination.
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
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.
Motivation
In an existing project with both Python and Node.js, I wanted activating the
Python virtual environment to also select the project's already installed
Node/npm. The runtimes had been set up separately, so consolidating them meant
manually moving files without replacing the Python activation scripts and then
editing activation and deactivation for each Windows shell.
This adds an optional, repeatable utility for that migration: simply add an
existing Node installation to an existing Python environment, reuse the installed
files, and use one activation entry point for both runtimes.
For a new setup,
nodeenv -pis strongly recommended. Create and activate thePython venv first, then run
nodeenv -p. The new utility is for consolidatingenvironments that already exist, without downloading Node/npm again.
Changes
utils/integrate_nodeenv.pywith--source,--venvand--dry-run.Defaults are
envand.venvin the project's current working directory.entries and retaining their source copies. Reject overlapping roots,
incompatible file/directory collisions and unsafe activation links.
NODE_PATHsave/restore into CMD, PowerShell and Git Bash activation anddeactivation. Repeated runs preserve existing recognized patches and mtimes.
deleted only after migration and patching both succeed.
nodeenv -pworkflow and migration stepsin
utils/README.md; link it from the main README and add a changelog entry.utilsin flake8 checks.The utility is Windows-only, runs on Python 3.10+, and accepts recognized CPython
activation layouts. It preserves UTF-8 BOMs and line endings. An edited
Activate.ps1loses its original signature; execution policy is left unchanged.Validation
Tested locally on Windows with Python 3.12.10 and Git Bash available on PATH:
The 40 new tests cover migration/no overwrite, dry runs, preflight failures,
repeated runs, working-directory defaults, junction handling, platform checks,
line endings, BOMs and PowerShell signatures. Download-based integration tests
were excluded from the local run.
type(scope): subject.