Skip to content

Commit aeac7e0

Browse files
committed
fix: embed node version requirement when compiling
1 parent 2e2c4c6 commit aeac7e0

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

crates/vite_global_cli/src/js_executor.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use vite_js_runtime::{
1313
};
1414
use vite_path::{AbsolutePath, AbsolutePathBuf};
1515
use vite_shared::{
16-
PrependOptions, PrependResult,
16+
PackageJson, PrependOptions, PrependResult,
1717
env_vars::{self, VP_NODE_VERSION},
1818
format_path_with_prepend,
1919
};
@@ -117,13 +117,11 @@ impl JsExecutor {
117117
cmd
118118
}
119119

120-
/// Read the `engines.node` requirement from the CLI's own `package.json`.
121-
///
122-
/// Returns `None` when the file is missing, unreadable, or has no `engines.node`.
123-
async fn get_cli_engines_requirement(&self) -> Option<String> {
124-
let cli_dir = self.get_cli_package_dir().ok()?;
125-
let pkg_path = cli_dir.join("package.json");
126-
let pkg = read_package_json(&pkg_path).await.ok()??;
120+
/// Return the `engines.node` requirement from the CLI's `package.json`.
121+
/// It must be embedded at compile time. As cli package may not exist while upgrading.
122+
fn get_cli_engines_requirement() -> Option<String> {
123+
let pkg: PackageJson =
124+
serde_json::from_str(include_str!("../../../packages/cli/package.json")).ok()?;
127125
pkg.engines?.node.map(|s| s.to_string())
128126
}
129127

@@ -256,7 +254,7 @@ impl JsExecutor {
256254
source: Option<&str>,
257255
is_project_runtime: bool,
258256
) -> Result<(), Error> {
259-
let Some(requirement) = self.get_cli_engines_requirement().await else { return Ok(()) };
257+
let Some(requirement) = Self::get_cli_engines_requirement() else { return Ok(()) };
260258

261259
// System runtimes report "system" — we cannot inspect the actual version cheaply,
262260
// and the user has explicitly opted in via `vp env off`.
@@ -572,14 +570,10 @@ mod tests {
572570
use tempfile::TempDir;
573571
use vite_shared::EnvConfig;
574572

575-
// Point scripts_dir at the real packages/cli/dist so that
576-
// get_cli_engines_requirement() reads the actual engines.node from
577-
// packages/cli/package.json. The dist/ directory need not exist — only
578-
// its parent (packages/cli/) and the package.json within it are read.
579-
let scripts_dir = AbsolutePathBuf::new(
580-
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../packages/cli/dist"),
581-
)
582-
.unwrap();
573+
// `engines.node`` is now embedded at compile time
574+
// So we just need to direct to a random directory
575+
let scripts_dir =
576+
AbsolutePathBuf::new(TempDir::new().unwrap().path().to_path_buf()).unwrap();
583577

584578
// Use any existing directory as project_path; the session override
585579
// fires before any project-source lookup or network download.

0 commit comments

Comments
 (0)