feat(cli): advertise VP_USER_AGENT to child processes#1958
Conversation
Set `VP_USER_AGENT=vp/<version>` once at startup so spawned tools can detect vp as the package manager. Underlying package managers overwrite `npm_config_user_agent`, so a dedicated variable is needed. Enables `vp dlx create-vite` to scaffold with vp commands.
✅ Deploy Preview for viteplus-preview canceled.
|
✅ Deploy Preview for viteplus-preview canceled.
|
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2d9a056834
ℹ️ 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".
| std::env::set_var( | ||
| vite_shared::env_vars::VP_USER_AGENT, | ||
| concat!("vp/", env!("CARGO_PKG_VERSION")), | ||
| ); |
There was a problem hiding this comment.
Avoid marking direct package-manager shims as vp
Because this is set before shim detection, any invocation through the managed npm/npx shims also exports VP_USER_AGENT before run() reaches shim::detect_shim_tool; in system-first mode this even survives when vp immediately execs the system package manager. A user running npx create-vite through Vite+'s shims is not using vp dlx, but create-vite will still see VP_USER_AGENT=vp/... and can scaffold commands for vp instead of the package manager the user actually invoked. Set this only on the normal vp command path, or clear it before dispatching shim invocations.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in deeab22. VP_USER_AGENT is now cleared in the shim branch before shim::dispatch, so when vp runs as a managed npm/npx/node shim the wrapped tool and its children no longer see it. Verified: VP_SHIM_TOOL=node vp -e ... shows undefined, while vp dlx ... node -e ... still shows vp/0.2.1.
| std::env::set_var( | ||
| vite_shared::env_vars::VP_USER_AGENT, | ||
| concat!("vp/", env!("CARGO_PKG_VERSION")), | ||
| ); |
There was a problem hiding this comment.
Set VP_USER_AGENT for local vp invocations too
This only runs in the Rust global binary, but the published vite-plus package also exposes its own vp bin and that JS entrypoint routes dlx through the NAPI package-manager path without setting this variable. In projects that invoke the local CLI directly, e.g. pnpm exec vp dlx create-vite or node_modules/.bin/vp dlx create-vite, the child create tool still won't see VP_USER_AGENT, so the new detection remains inconsistent for a supported vp entrypoint. Mirror this initialization in the local JS/NAPI path or a shared command launcher.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed this is a real consistency gap. Tracing it: user-facing vp dlx is implemented in the Rust binary (Commands::Dlx -> vite_pm_cli), which this PR now covers. The JS package's own dlx execution (getPackageRunner/formatDlxCommand in packages/cli/src/create/command.ts) spawns the native PM (pnpm dlx/npx/...) directly and is used internally by create/migration, not by user vp dlx — and it already carries a TODO to route through vp dlx instead (vite-task#27).
So I'd prefer not to duplicate the marker into the JS launcher here; the clean fix is to unify that path onto vp dlx (the existing TODO), after which it inherits this automatically. If you'd rather I set VP_USER_AGENT in the JS spawn env now (sourced from VP_GLOBAL_VERSION) as an interim, I can add that — let me know your preference.
When vp runs as a managed npm/npx/node shim, the user is invoking that tool, not vp, so clear VP_USER_AGENT before dispatching the shim. Otherwise a child like create-vite would wrongly scaffold vp commands.
Problem
When
vpruns a tool (e.g.vp dlx create-vite), the child can't tell it was launched by vp. The underlying package manager (pnpm/npm/yarn/bun) overwritesnpm_config_user_agentwith its own value, so that channel can't carry the vp signal.Fix
Set a dedicated
VP_USER_AGENT=vp/<version>env var once at startup, inherited by every child process. Tools like create-vite can read it to detect vp and scaffold withvp install/vp dev/vp dlx.VP_USER_AGENTconstant to the centralenv_varsregistry.main, before the async runtime starts (edition 2024set_varmust run single-threaded), mirroring thevite_installerpattern. This covers all dlx paths including thenpxfallback used when there is no localpackage.json.Companion
create-vite reads
VP_USER_AGENT: vitejs/vite#22788