Skip to content

fix(cli): keep env use session-scoped on Windows#1577

Merged
fengmk2 merged 15 commits into
mainfrom
fix/windows-env-use-session-scope
May 15, 2026
Merged

fix(cli): keep env use session-scoped on Windows#1577
fengmk2 merged 15 commits into
mainfrom
fix/windows-env-use-session-scope

Conversation

@fengmk2
Copy link
Copy Markdown
Member

@fengmk2 fengmk2 commented May 14, 2026

Summary

  • prevent vp env use on Windows from writing the shared .session-node-version fallback
  • require the PowerShell eval wrapper for Windows session-scoped env switching
  • ignore legacy session files from Windows runtime resolution and surface them in vp env doctor

Verification

  • installed local dev CLI and verified direct Windows invocation no longer creates .session-node-version
  • verified separate wrapped PowerShell sessions can select Node 20.18.0 and 22.18.0 independently
image

Note

Medium Risk
Changes Windows behavior for vp env use by preventing session-file fallback outside CI and returning non-zero when the PowerShell eval wrapper isn’t loaded, which could break existing Windows workflows/scripts.

Overview
Ensures vp env use stays session-scoped on Windows by disallowing .session-node-version writes in interactive Windows shells unless the PowerShell wrapper is active (CI is still allowed to use the session file fallback). vp env use --unset now always deletes any existing session file, and new Windows-only tests cover direct invocation, CI fallback, and wrapper cleanup behavior.

Refactors env setup script generation in env setup into per-shell templates with a shared renderer, and updates PowerShell setup guidance/output (including docs and RFC) to explicitly dot-source "$env:USERPROFILE\.vite-plus\env.ps1".

Reviewed by Cursor Bugbot for commit 6e38069. Configure here.

@fengmk2 fengmk2 self-assigned this May 14, 2026
@netlify
Copy link
Copy Markdown

netlify Bot commented May 14, 2026

Deploy Preview for viteplus-preview canceled.

Name Link
🔨 Latest commit e2bb2d3
🔍 Latest deploy log https://app.netlify.com/projects/viteplus-preview/deploys/6a068768fb08bd00084bff6c

@fengmk2 fengmk2 force-pushed the fix/windows-env-use-session-scope branch from 80bfa93 to ab868d5 Compare May 14, 2026 08:29
@IHEL
Copy link
Copy Markdown

IHEL commented May 14, 2026

好的,请验证好功能,我的要求是跟fnm软件类似,在不同的PowerShell Tab窗口使用类似fnm use切换node版本,不影响其他的PowerShell Tab窗口里面的node版本,目前使用下来vp env use切换node版本其他PowerShell里面执行vp env list也跟着同步发送切换变化了,我希望跟fnm类似

@fengmk2 fengmk2 force-pushed the fix/windows-env-use-session-scope branch 3 times, most recently from dc48fe8 to 9072db7 Compare May 14, 2026 12:44
@IHEL
Copy link
Copy Markdown

IHEL commented May 14, 2026

这个功能大概什么时候发布正式?

@fengmk2 fengmk2 force-pushed the fix/windows-env-use-session-scope branch from 9072db7 to c64eeda Compare May 14, 2026 13:14
Comment thread docs/guide/env.md Outdated
@fengmk2
Copy link
Copy Markdown
Member Author

fengmk2 commented May 14, 2026

@IHEL A new version will be released every Tuesday.

Comment thread crates/vite_global_cli/src/commands/env/config.rs Outdated
Comment thread crates/vite_global_cli/src/commands/env/config.rs Outdated
Comment thread crates/vite_global_cli/src/commands/env/config.rs Outdated
Comment thread crates/vite_global_cli/src/commands/env/doctor.rs Outdated
Comment thread crates/vite_global_cli/src/commands/env/config.rs Outdated
Comment thread crates/vite_global_cli/src/shim/dispatch.rs Outdated
Comment thread crates/vite_global_cli/src/js_executor.rs Outdated
fengmk2 added 5 commits May 14, 2026 22:03
…letes

- Remove the --use-no-cd flag: it was wired into the parser but never read
  by print_shell_env, and env.ps1 has no directory-change hooks for it to
  opt out of. Update the recommended PowerShell setup line everywhere
  (cli help, print_path_instructions, wrapper-required warning, docs/RFC).
- Drop the duplicated config::delete_session_version() calls in use.rs:
  the unconditional delete at the top of each branch already covers all
  three sub-cases (eval-wrapper, session-file-allowed, Windows non-CI),
  so the inner delete inside `else if can_use_session_file()` was a
  guaranteed-NotFound syscall.
- Flatten three `else { if ... }` blocks into `else if ...` (silent-if-
  unchanged, tail write block, unset branch) and drop the redundant
  "Removed file-based ... if it existed" line that was superseded by the
  existing "Reverted to file-based Node.js version resolution" message.
- Sync the `vp env --help` example for the PowerShell setup into
  help.rs's hand-maintained mirror; the clap after_help in cli.rs is not
  rendered for `vp env --help` (the unified renderer wins), so the
  PowerShell example was previously invisible to users.
The full `vp env --shell powershell | Out-String | Invoke-Expression`
line is much longer than the other Setup examples, so leaving it inline
broke the `#`-comment column alignment. Move it into its own
`PowerShell (add to $PROFILE):` subsection (matches the existing
Setup/Manage/Inspect/Execute structure) and drop the inline comment.

Update the cli-helper-message snapshot to match.
Promote the previous top-level `--shell <shell>` flag on `vp env` into a
proper `Profile { shell: Option<EnvShell> }` subcommand so the new
capability fits the existing noun-verb subcommand pattern (current,
print, default, on, off, setup, doctor, which, pin, unpin, list, ...)
instead of being the only top-level flag on the command. The recommended
PowerShell setup invocation becomes:

  vp env profile --shell powershell | Out-String | Invoke-Expression

Side-effect-free implementation:

- Lift the four shell env-file templates (POSIX/Fish/Nushell/PowerShell)
  and the cmd.exe wrapper to module-scope constants in setup.rs.
- Add `setup::render_env_content(shell, vite_plus_home) -> String` as a
  pure helper that performs the placeholder substitution for one shell.
- `create_env_files` now loops over the four shells calling the helper,
  removing the inline-template duplication.
- `vp env profile` builds the content directly via `render_env_content`
  and prints it. No call to `create_env_files`, no read-back of a file
  we just wrote — important because the recommended `$PROFILE` line runs
  on every PowerShell startup, which previously rewrote all five env
  files (~20KB of disk churn) on every shell launch.

Also reverts `create_env_files` from `pub(crate)` back to private now
that the profile path no longer needs it, syncs help.rs subcommand list
+ Examples block, and updates the use.rs warning text plus docs/RFC.
- Add `EnvShell::env_file_name() -> &'static str` so the per-shell file
  name (env, env.fish, env.nu, env.ps1) lives on the enum instead of the
  inline tuple in `create_env_files`. Removes one source of drift; the
  rest of the codebase (notably `commands/shell.rs::ALL_SHELL_PROFILES`)
  can migrate to this method in a follow-up.
- Disambiguate the `print` subcommand description against the new
  `profile` subcommand in both clap (`vp env print --help`) and the
  unified renderer (`vp env --help`). The contrast between
  "current session" and "$PROFILE-style setup" is now explicit.
@fengmk2
Copy link
Copy Markdown
Member Author

fengmk2 commented May 14, 2026

@cursor review

Comment thread crates/vite_global_cli/src/commands/env/config.rs
Comment thread crates/vite_global_cli/src/commands/env/use.rs
`vp env setup` already writes `~/.vite-plus/env.ps1` to disk, so the new
`vp env profile` subcommand was redundant: the recommended PowerShell
`$PROFILE` line just needs to dot-source the existing file. That avoids
spawning a `vp` subprocess on every shell startup, removes the
chicken-and-egg requirement that `vp` already be on PATH, and matches
the pattern used by rustup/mise/fnm/sdkman.

  . "$env:USERPROFILE\.vite-plus\env.ps1"

Changes:
- Remove `EnvSubcommands::Profile` and the `print_profile` handler.
- Remove the `clap::ValueEnum` derive and `#[value(alias = ...)]`
  attributes on `EnvShell` — it is no longer exposed via the CLI, only
  used internally by `setup::create_env_files` / `render_env_content`.
- Restore the original `print` subcommand description (the `profile`
  cross-reference is no longer needed).
- Update the recommended PowerShell setup line everywhere (cli help
  examples, help.rs Examples block, print_path_instructions, the
  `print_windows_eval_wrapper_required` warning, docs/guide/env.md,
  rfcs/env-command.md).
- Make `render_env_content` private again now that the `vp env profile`
  caller is gone; keep the lift-templates-to-consts cleanup since
  `create_env_files` still benefits.
Comment thread docs/guide/env.md
fengmk2 added 2 commits May 15, 2026 09:23
…ed hint

- Relocate `EnvShell` enum and `env_file_name()` from `cli.rs` into
  `commands/env/setup.rs`. With `vp env profile` gone, the type is no
  longer CLI surface — only `create_env_files` and `render_env_content`
  use it. Drop `pub`/`pub(crate)` since callers are now same-module.
- Reword `print_windows_eval_wrapper_required`: the previous "Then start
  a new PowerShell session" hint left the user's current PowerShell
  still wrapper-less. Updated to "dot-source it now (or open a new
  PowerShell session)" so the user can re-run `vp env use` without
  restarting the shell.
The PowerShell setup line is documented in docs/guide/env.md and the
RFC; including it inline in the `vp env --help` Examples is noise for
the common (POSIX) audience and was the only Windows-specific example
in the block.
@fengmk2
Copy link
Copy Markdown
Member Author

fengmk2 commented May 15, 2026

@cursor review

@fengmk2
Copy link
Copy Markdown
Member Author

fengmk2 commented May 15, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6e38069500

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/vite_global_cli/src/commands/env/setup.rs Outdated
Comment thread crates/vite_global_cli/src/commands/env/use.rs
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 6e38069. Configure here.

`vp env setup` writes `env.ps1` under the actual `vite_plus_home`, but
the printed PowerShell `$PROFILE` instruction was hardcoded to
`$env:USERPROFILE\.vite-plus\env.ps1`. With a custom `VP_HOME` the file
written and the file the user is told to dot-source did not match, so
subsequent `vp env use` calls kept hitting the wrapper-required error
even though setup succeeded. Use the same `home_path` already computed
for the POSIX/Fish/Nushell instructions.
@fengmk2 fengmk2 marked this pull request as ready for review May 15, 2026 02:14
@fengmk2 fengmk2 requested review from cpojer, naokihaba and nekomoyi May 15, 2026 02:15
@fengmk2 fengmk2 merged commit e561d78 into main May 15, 2026
80 of 84 checks passed
@fengmk2 fengmk2 deleted the fix/windows-env-use-session-scope branch May 15, 2026 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants