Skip to content

Commit 16f71ad

Browse files
committed
fix: pass global CLI version via env var to prevent local version misreport
When `vp --version` delegated to a local node_modules copy, the JS `getGlobalVersion()` read the local package.json via `import.meta.dirname`, showing the local version as the global version. Now the Rust binary sets `VITE_PLUS_GLOBAL_VERSION` from `CARGO_PKG_VERSION` before delegation, and the JS side reads this env var instead.
1 parent 6c76830 commit 16f71ad

6 files changed

Lines changed: 24 additions & 5 deletions

File tree

crates/vite_global_cli/src/commands/version.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ use crate::error::Error;
88

99
/// Execute the `--version` command by delegating to local or global vite-plus.
1010
pub async fn execute(cwd: AbsolutePathBuf) -> Result<ExitStatus, Error> {
11+
// Pass the Rust binary's version to JS so it can display the correct global version,
12+
// even when delegation resolves to a local node_modules copy.
13+
unsafe {
14+
std::env::set_var(
15+
vite_shared::env_vars::VITE_PLUS_GLOBAL_VERSION,
16+
env!("CARGO_PKG_VERSION"),
17+
);
18+
}
1119
super::delegate::execute(cwd, "--version", &[]).await
1220
}
1321

crates/vite_shared/src/env_vars.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ pub const VITE_PLUS_SHIM_TOOL: &str = "VITE_PLUS_SHIM_TOOL";
6363

6464
/// Path to the vp binary, passed to JS scripts so they can invoke CLI commands.
6565
pub const VITE_PLUS_CLI_BIN: &str = "VITE_PLUS_CLI_BIN";
66+
67+
/// Global CLI version, passed from Rust binary to JS for --version display.
68+
pub const VITE_PLUS_GLOBAL_VERSION: &str = "VITE_PLUS_GLOBAL_VERSION";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
> vp --version
2+
VITE+ - The Unified Toolchain for the Web
3+
4+
PACKAGE VERSIONS:
5+
Global vite-plus: Not found
6+
Local vite-plus: Not found
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"commands": ["vp --version"]
3+
}

packages/cli/src/version.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { styleText } from 'node:util';
55

66
import { VITE_PLUS_NAME } from './utils/constants.js';
77
import { detectPackageMetadata } from './utils/package.js';
8-
import { pkgRoot } from './utils/path.js';
98
import { getVitePlusHeader, headline, log } from './utils/terminal.js';
109

1110
const require = createRequire(import.meta.url);
@@ -29,9 +28,8 @@ interface ToolVersionSpec {
2928
fallbackPackageJson?: string;
3029
}
3130

32-
function getGlobalVersion(): string {
33-
const pkg: PackageJson = require(path.join(pkgRoot, 'package.json'));
34-
return pkg.version;
31+
function getGlobalVersion(): string | null {
32+
return process.env.VITE_PLUS_GLOBAL_VERSION ?? null;
3533
}
3634

3735
function getLocalMetadata(cwd: string): LocalPackageMetadata | null {
@@ -112,7 +110,7 @@ export async function printVersion(cwd: string) {
112110
log((await getVitePlusHeader()) + '\n');
113111
log(headline('Package Versions:'));
114112
log(
115-
` ${styleText('bold', 'Global vite-plus:')}${' '.repeat(getColumnWidth('Global vite-plus:'))}v${globalVersion}`,
113+
` ${styleText('bold', 'Global vite-plus:')}${' '.repeat(getColumnWidth('Global vite-plus:'))}${globalVersion ? `v${globalVersion}` : 'Not found'}`,
116114
);
117115
if (localVersion) {
118116
log(

0 commit comments

Comments
 (0)