Skip to content

feat(env): show message when switching version automatically#608

Draft
hyf0 wants to merge 3 commits intomainfrom
hyf_show_message
Draft

feat(env): show message when switching version automatically#608
hyf0 wants to merge 3 commits intomainfrom
hyf_show_message

Conversation

@hyf0
Copy link
Contributor

@hyf0 hyf0 commented Feb 20, 2026

Summary

Add shell cd hooks that auto-switch Node.js versions on directory change, similar to fnm/nvm.

~/project-a $ cd ../project-b
vp: Using Node.js v20.18.0 (from .node-version)
~/project-b $ cd ../project-a
vp: Using Node.js v22.22.0 (from .node-version)
~/project-a $ cd .
(silent — version unchanged)

What's new

Change Detail
cd hooks in ~/.vite-plus/env* Zsh (chpwd_functions), Bash (cd alias), Fish (--on-variable PWD), PowerShell (prompt wrapper)
Bug fix: resolve_version circular read vp env use without explicit version now calls resolve_version_from_project() which skips VITE_PLUS_NODE_VERSION env var — previously the env var was read back as the "resolved" version, making --silent-if-unchanged always match
Message format Using Node.js v{} (resolved from {})vp: Using Node.js v{} (from {})

Test plan

  • Unit tests: cd hook code generation for POSIX, Fish, PowerShell
  • Snap test: command-env-use-silent-if-unchanged — first call shows message, same version silent, different version triggers message
  • Snap test: command-env-use updated for new message format
  • Manual e2e: zsh cd between dirs with different .node-version correctly switches version

Copilot AI review requested due to automatic review settings February 20, 2026 14:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds automatic Node.js version switching when changing directories, improving the developer experience by eliminating the need to manually run vp env use when entering different projects.

Changes:

  • Added --silent-if-unchanged flag to vp env use to suppress output when the version is already active
  • Introduced resolve_version_from_project() function to resolve versions from project files only, avoiding infinite recursion when vp env use is called from cd hooks
  • Updated shell environment files (bash/zsh, fish, PowerShell) to automatically call vp env use --silent-if-unchanged on directory changes
  • Improved message format from "Using Node.js v{version} (resolved from {source})" to "vp: Using Node.js v{version} (from {source})" for clarity and consistency

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/vite_global_cli/src/commands/env/use.rs Updated to use resolve_version_from_project and improved status message format with "vp:" prefix
crates/vite_global_cli/src/commands/env/config.rs Added new resolve_version_from_project function that skips session overrides, preventing infinite recursion
crates/vite_global_cli/src/commands/env/setup.rs Added cd hooks for bash/zsh, fish, and PowerShell shells to auto-switch Node.js versions; includes comprehensive unit tests
packages/global/snap-tests/command-env-use/snap.txt Updated to reflect new message format with "vp:" prefix
packages/global/snap-tests/command-env-use-silent-if-unchanged/* New integration test suite verifying --silent-if-unchanged behavior across different scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@fengmk2
Copy link
Member

fengmk2 commented Feb 21, 2026

According to our current implementation logic, as long as the node command is executed it will run the correct version — so why do we still need to rely on cd to switch versions?

@hyf0
Copy link
Contributor Author

hyf0 commented Feb 21, 2026

According to our current implementation logic, as long as the node command is executed it will run the correct version — so why do we still need to rely on cd to switch versions?

I don't consider this as a functional feature but a dx improvement. I used fnm before it shows switched node version when entering project folder, gives you a clear hint it's working.

I don't have a strong stand on this behavior, if we don't need it by design, it's ok to just close it.

@fengmk2
Copy link
Member

fengmk2 commented Feb 24, 2026

@hyf0 If the DX experience can be improved, then let's merge.

export VITE_PLUS_NODE_VERSION=20.18.0

> VITE_PLUS_NODE_VERSION=22.0.0 vp env use --silent-if-unchanged --no-install # different version - should show message
export VITE_PLUS_NODE_VERSION=20.18.0
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this output 22.0.0?

@hyf0
Copy link
Contributor Author

hyf0 commented Feb 24, 2026

@hyf0 If the DX experience can be improved, then let's merge.

Let me do more investigation and ask for feedbacks

@hyf0 hyf0 marked this pull request as draft February 24, 2026 04:11
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.

3 participants