Skip to content

Commit 1944f16

Browse files
committed
fix(shim): set VITE_PLUS_HOME in all Windows .cmd wrappers
All Windows .cmd wrappers now set VITE_PLUS_HOME=%~dp0.. before calling vp.exe. This ensures the vp binary knows its home directory regardless of how it's invoked. Changes: - install.ps1: Set VITE_PLUS_HOME in bin/vp.cmd - setup.rs: Set VITE_PLUS_HOME in vp.cmd, node.cmd, npm.cmd, npx.cmd - global_install.rs: Set VITE_PLUS_HOME in package binary shims - install-global-cli.ts: Simplified - no longer rewrites .cmd files, just renames vp.cmd to vp-dev.cmd when needed
1 parent a23e584 commit 1944f16

5 files changed

Lines changed: 26 additions & 75 deletions

File tree

.github/workflows/test-standalone-install.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114
ubuntu:20.04 bash -c "
115115
ls -al ~/
116116
apt-get update && apt-get install -y curl ca-certificates
117-
cat /workspace/packages/global/install.sh | sh
117+
cat /workspace/packages/global/install.sh | bash
118118
if [ -f ~/.profile ]; then
119119
source ~/.profile
120120
elif [ -f ~/.bashrc ]; then

crates/vite_global_cli/src/commands/env/global_install.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,10 @@ async fn create_package_shim(
333333
}
334334

335335
// Create .cmd wrapper that calls vp env run <bin_name>
336+
// Set VITE_PLUS_HOME using %~dp0.. which resolves to the parent of bin/
337+
// This ensures the vp binary knows its home directory
336338
let wrapper_content = format!(
337-
"@echo off\r\n\"%~dp0..\\current\\bin\\vp.exe\" env run {} %*\r\nexit /b %ERRORLEVEL%\r\n",
339+
"@echo off\r\nset VITE_PLUS_HOME=%~dp0..\r\n\"%VITE_PLUS_HOME%\\current\\bin\\vp.exe\" env run {} %*\r\nexit /b %ERRORLEVEL%\r\n",
338340
bin_name
339341
);
340342
tokio::fs::write(&shim_path, wrapper_content).await?;

crates/vite_global_cli/src/commands/env/setup.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ async fn setup_vp_wrapper(bin_dir: &vite_path::AbsolutePath, refresh: bool) -> R
108108
refresh || !tokio::fs::try_exists(&bin_vp_cmd).await.unwrap_or(false);
109109

110110
if should_create_wrapper {
111-
let cmd_content = r#"@echo off
112-
"%~dp0..\current\bin\vp.exe" %*
113-
exit /b %ERRORLEVEL%
114-
"#;
111+
// Set VITE_PLUS_HOME using %~dp0.. which resolves to the parent of bin/
112+
// This ensures the vp binary knows its home directory
113+
let cmd_content = "@echo off\r\nset VITE_PLUS_HOME=%~dp0..\r\n\"%VITE_PLUS_HOME%\\current\\bin\\vp.exe\" %*\r\nexit /b %ERRORLEVEL%\r\n";
115114
tokio::fs::write(&bin_vp_cmd, cmd_content).await?;
116115
tracing::debug!("Created wrapper script {:?}", bin_vp_cmd);
117116
}
@@ -206,11 +205,11 @@ async fn create_windows_shim(
206205
let cmd_path = bin_dir.join(format!("{tool}.cmd"));
207206

208207
// Create .cmd wrapper that calls vp env run <tool>
208+
// Set VITE_PLUS_HOME using %~dp0.. which resolves to the parent of bin/
209+
// This ensures the vp binary knows its home directory
209210
let cmd_content = format!(
210-
r#"@echo off
211-
"%~dp0..\current\bin\vp.exe" env run {tool} %*
212-
exit /b %ERRORLEVEL%
213-
"#
211+
"@echo off\r\nset VITE_PLUS_HOME=%~dp0..\r\n\"%VITE_PLUS_HOME%\\current\\bin\\vp.exe\" env run {} %*\r\nexit /b %ERRORLEVEL%\r\n",
212+
tool
214213
);
215214

216215
tokio::fs::write(&cmd_path, cmd_content).await?;

packages/global/install.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,12 @@ function Main {
421421
cmd /c mklink /J "$CurrentLink" "$VersionDir" | Out-Null
422422

423423
# Create bin directory and vp.cmd wrapper (always done)
424+
# Set VITE_PLUS_HOME so the vp binary knows its home directory
424425
New-Item -ItemType Directory -Force -Path "$InstallDir\bin" | Out-Null
425426
$wrapperContent = @"
426427
@echo off
427-
"%~dp0..\current\bin\vp.exe" %*
428+
set VITE_PLUS_HOME=%~dp0..
429+
"%VITE_PLUS_HOME%\current\bin\vp.exe" %*
428430
exit /b %ERRORLEVEL%
429431
"@
430432
Set-Content -Path "$InstallDir\bin\vp.cmd" -Value $wrapperContent -NoNewline

packages/tools/src/install-global-cli.ts

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -108,74 +108,22 @@ export function installGlobalCli() {
108108
const binDir = path.join(installDir, 'bin');
109109
const currentBinDir = path.join(installDir, 'current', 'bin');
110110

111-
// Rename the actual vp binary to vp-raw, then create a wrapper at vp
112-
// This ensures VITE_PLUS_HOME is always set when vp is invoked (including via shims)
113-
// The wrapper uses `exec -a "$0"` to preserve argv[0] for shim detection
111+
// Create wrapper scripts to ensure VITE_PLUS_HOME is always set
114112
if (isWindows) {
115-
const vpExe = path.join(currentBinDir, 'vp.exe');
116-
const vpRawExe = path.join(currentBinDir, 'vp-raw.exe');
117-
118-
// Rename vp.exe -> vp-raw.exe
119-
if (existsSync(vpExe) && !existsSync(vpRawExe)) {
120-
renameSync(vpExe, vpRawExe);
121-
console.log(`Renamed ${vpExe} -> ${vpRawExe}`);
122-
}
123-
124-
// Create vp.cmd wrapper in current/bin/ that sets VITE_PLUS_HOME and calls vp-raw.exe
125-
const vpWrapperPath = path.join(currentBinDir, 'vp.cmd');
126-
const vpWrapperContent = `@echo off\r
127-
set VITE_PLUS_HOME=${installDir}\r
128-
"%~dp0vp-raw.exe" %*\r
129-
exit /b %ERRORLEVEL%\r
130-
`;
131-
writeFileSync(vpWrapperPath, vpWrapperContent);
132-
console.log(`Created wrapper: ${vpWrapperPath}`);
133-
134-
// On Windows, create bash script wrappers for Git Bash compatibility
135-
// (Git Bash doesn't execute .cmd files automatically)
113+
// On Windows, install.ps1 already creates bin/vp.cmd with VITE_PLUS_HOME set.
114+
// For 'vp-dev', we need to rename it to vp-dev.cmd.
136115
if (binName === 'vp-dev') {
137-
// Remove the vp.cmd in bin/ to avoid confusion
138-
rmSync(path.join(binDir, 'vp.cmd'), { force: true });
139-
140-
// Create vp-dev.cmd for cmd.exe/PowerShell
141-
const cmdPath = path.join(binDir, 'vp-dev.cmd');
142-
const cmdContent = `@echo off\r
143-
set VITE_PLUS_HOME=${installDir}\r
144-
"%VITE_PLUS_HOME%\\current\\bin\\vp.cmd" %*\r
145-
exit /b %ERRORLEVEL%\r
146-
`;
147-
writeFileSync(cmdPath, cmdContent);
148-
149-
// Create vp-dev bash script for Git Bash
150-
const bashPath = path.join(binDir, 'vp-dev');
151-
const bashContent = `#!/bin/bash
152-
export VITE_PLUS_HOME="${installDir}"
153-
exec "$VITE_PLUS_HOME/current/bin/vp.cmd" "$@"
154-
`;
155-
writeFileSync(bashPath, bashContent);
156-
console.log(`\nCreated wrapper scripts: ${cmdPath}, ${bashPath}`);
157-
} else {
158-
// For 'vp', update bin/vp.cmd to call vp.cmd instead of vp.exe
159-
// (install.ps1 creates it pointing to vp.exe, but we renamed that to vp-raw.exe)
160-
const cmdPath = path.join(binDir, 'vp.cmd');
161-
const cmdContent = `@echo off\r
162-
set VITE_PLUS_HOME=${installDir}\r
163-
"%VITE_PLUS_HOME%\\current\\bin\\vp.cmd" %*\r
164-
exit /b %ERRORLEVEL%\r
165-
`;
166-
writeFileSync(cmdPath, cmdContent);
167-
168-
// Also create bash script wrapper for Git Bash
169-
const bashPath = path.join(binDir, 'vp');
170-
const bashContent = `#!/bin/bash
171-
export VITE_PLUS_HOME="${installDir}"
172-
exec "$VITE_PLUS_HOME/current/bin/vp.cmd" "$@"
173-
`;
174-
writeFileSync(bashPath, bashContent);
175-
console.log(`\nCreated wrapper scripts: ${cmdPath}, ${bashPath}`);
116+
const vpCmd = path.join(binDir, 'vp.cmd');
117+
const vpDevCmd = path.join(binDir, 'vp-dev.cmd');
118+
if (existsSync(vpCmd)) {
119+
renameSync(vpCmd, vpDevCmd);
120+
console.log(`\nRenamed ${vpCmd} -> ${vpDevCmd}`);
121+
}
176122
}
123+
// For 'vp', bin/vp.cmd is already correct from install.ps1
177124
} else {
178-
// Unix: Rename vp -> vp-raw, create wrapper
125+
// Unix: Rename vp -> vp-raw, then create a wrapper at vp
126+
// The wrapper sets VITE_PLUS_HOME and uses `exec -a "$0"` to preserve argv[0] for shim detection
179127
const vpBinary = path.join(currentBinDir, 'vp');
180128
const vpRawBinary = path.join(currentBinDir, 'vp-raw');
181129

0 commit comments

Comments
 (0)