Skip to content

feat(cli): advertise VP_USER_AGENT to child processes#1958

Draft
fengmk2 wants to merge 2 commits into
mainfrom
vp-dlx-user-agent
Draft

feat(cli): advertise VP_USER_AGENT to child processes#1958
fengmk2 wants to merge 2 commits into
mainfrom
vp-dlx-user-agent

Conversation

@fengmk2

@fengmk2 fengmk2 commented Jun 26, 2026

Copy link
Copy Markdown
Member

Problem

When vp runs 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) overwrites npm_config_user_agent with 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 with vp install / vp dev / vp dlx.

  • Added the VP_USER_AGENT constant to the central env_vars registry.
  • Set it as the first statement of main, before the async runtime starts (edition 2024 set_var must run single-threaded), mirroring the vite_installer pattern. This covers all dlx paths including the npx fallback used when there is no local package.json.

Companion

create-vite reads VP_USER_AGENT: vitejs/vite#22788

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.
@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

Deploy Preview for viteplus-preview canceled.

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

@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

Deploy Preview for viteplus-preview canceled.

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

@fengmk2

fengmk2 commented Jun 26, 2026

Copy link
Copy Markdown
Member Author

@codex review

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

Copy link
Copy Markdown

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: 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".

Comment on lines +309 to +312
std::env::set_var(
vite_shared::env_vars::VP_USER_AGENT,
concat!("vp/", env!("CARGO_PKG_VERSION")),
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment on lines +309 to +312
std::env::set_var(
vite_shared::env_vars::VP_USER_AGENT,
concat!("vp/", env!("CARGO_PKG_VERSION")),
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.
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.

1 participant