Unverified Commit bd90bc42 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: statically evaluate path.sep (#92150)

For more `@vercel/nft` parity
parent 76df801a
...@@ -10461,6 +10461,7 @@ dependencies = [ ...@@ -10461,6 +10461,7 @@ dependencies = [
"rstest_reuse", "rstest_reuse",
"serde", "serde",
"serde_json", "serde_json",
"similar",
"tokio", "tokio",
"turbo-rcstr", "turbo-rcstr",
"turbo-tasks", "turbo-tasks",
......
use std::fmt::Display; use std::fmt::Display;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ValueToString, Vc, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ValueToString, Vc, trace::TraceRawVcs};
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
...@@ -243,6 +244,13 @@ impl Platform { ...@@ -243,6 +244,13 @@ impl Platform {
Self::Unknown => "unknown", Self::Unknown => "unknown",
} }
} }
pub fn path_separator(&self) -> RcStr {
match self {
Self::Win32 => rcstr!("\\"),
_ => rcstr!("/"),
}
}
} }
impl Display for Platform { impl Display for Platform {
......
...@@ -605,7 +605,7 @@ async fn well_known_object_member( ...@@ -605,7 +605,7 @@ async fn well_known_object_member(
let new_value = match kind { let new_value = match kind {
WellKnownObjectKind::GlobalObject => global_object(prop), WellKnownObjectKind::GlobalObject => global_object(prop),
WellKnownObjectKind::PathModule | WellKnownObjectKind::PathModuleDefault => { WellKnownObjectKind::PathModule | WellKnownObjectKind::PathModuleDefault => {
path_module_member(kind, prop) path_module_member(kind, prop, compile_time_info).await?
} }
WellKnownObjectKind::FsModule WellKnownObjectKind::FsModule
| WellKnownObjectKind::FsModuleDefault | WellKnownObjectKind::FsModuleDefault
...@@ -685,8 +685,12 @@ fn global_object(prop: JsValue) -> JsValue { ...@@ -685,8 +685,12 @@ fn global_object(prop: JsValue) -> JsValue {
} }
} }
fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue { async fn path_module_member(
match (kind, prop.as_str()) { kind: WellKnownObjectKind,
prop: JsValue,
compile_time_info: Vc<CompileTimeInfo>,
) -> Result<JsValue> {
Ok(match (kind, prop.as_str()) {
(.., Some("join")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathJoin), (.., Some("join")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathJoin),
(.., Some("dirname")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathDirname), (.., Some("dirname")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathDirname),
(.., Some("resolve")) => { (.., Some("resolve")) => {
...@@ -695,6 +699,13 @@ fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue { ...@@ -695,6 +699,13 @@ fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
"", "",
)))) ))))
} }
(.., Some("sep")) => compile_time_info
.environment()
.compile_target()
.await?
.platform
.path_separator()
.into(),
(WellKnownObjectKind::PathModule, Some("default")) => { (WellKnownObjectKind::PathModule, Some("default")) => {
JsValue::WellKnownObject(WellKnownObjectKind::PathModuleDefault) JsValue::WellKnownObject(WellKnownObjectKind::PathModuleDefault)
} }
...@@ -706,7 +717,7 @@ fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue { ...@@ -706,7 +717,7 @@ fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
true, true,
"unsupported property on Node.js path module", "unsupported property on Node.js path module",
), ),
} })
} }
fn fs_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue { fn fs_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
......
...@@ -37,6 +37,7 @@ turbopack-node = { workspace = true, default-features = false, features = ["proc ...@@ -37,6 +37,7 @@ turbopack-node = { workspace = true, default-features = false, features = ["proc
criterion = { workspace = true, features = ["async_tokio"] } criterion = { workspace = true, features = ["async_tokio"] }
difference = "2.0" difference = "2.0"
similar = "2.2.0"
rstest = { workspace = true } rstest = { workspace = true }
rstest_reuse = "0.5.0" rstest_reuse = "0.5.0"
tokio = { workspace = true } tokio = { workspace = true }
......
...@@ -2,6 +2,8 @@ use std::{collections::VecDeque, fmt::Write as _}; ...@@ -2,6 +2,8 @@ use std::{collections::VecDeque, fmt::Write as _};
use difference::{Changeset, Difference}; use difference::{Changeset, Difference};
// Still used by turbopack/crates/turbopack-tracing/tests/node-file-trace.rs
#[allow(dead_code)]
pub fn print_changeset(changeset: &Changeset) -> String { pub fn print_changeset(changeset: &Changeset) -> String {
assert!(changeset.split == "\n"); assert!(changeset.split == "\n");
let mut result = String::from("--- DIFF ---\n- Expected\n+ Actual\n------------\n"); let mut result = String::from("--- DIFF ---\n- Expected\n+ Actual\n------------\n");
......
const fs = require('fs')
const { sep } = require('path') const { sep } = require('path')
const X = sep const X = sep
......
...@@ -6,9 +6,9 @@ mod helpers; ...@@ -6,9 +6,9 @@ mod helpers;
use std::{path::PathBuf, sync::LazyLock}; use std::{path::PathBuf, sync::LazyLock};
use anyhow::Result; use anyhow::Result;
use difference::Changeset;
use regex::Regex; use regex::Regex;
use rstest::*; use rstest::*;
use similar::TextDiff;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, TurboTasks, Vc}; use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_backend::TurboTasksBackend; use turbo_tasks_backend::TurboTasksBackend;
...@@ -36,8 +36,6 @@ use turbopack_core::{ ...@@ -36,8 +36,6 @@ use turbopack_core::{
use turbopack_ecmascript::AnalyzeMode; use turbopack_ecmascript::AnalyzeMode;
use turbopack_resolve::resolve_options_context::ResolveOptionsContext; use turbopack_resolve::resolve_options_context::ResolveOptionsContext;
use crate::helpers::print_changeset;
#[global_allocator] #[global_allocator]
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc; static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
...@@ -136,7 +134,7 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc; ...@@ -136,7 +134,7 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
#[case::node_modules_filter("node-modules-filter")] #[case::node_modules_filter("node-modules-filter")]
// #[case::non_analyzable_requires("non-analyzable-requires")] // #[case::non_analyzable_requires("non-analyzable-requires")]
#[case::null_destructure("null-destructure")] #[case::null_destructure("null-destructure")]
// #[case::path_sep("path-sep")] #[case::path_sep("path-sep")]
// #[case::phantomjs_prebuilt("phantomjs-prebuilt")] // #[case::phantomjs_prebuilt("phantomjs-prebuilt")]
// #[case::pino_transport("pino-transport")] // #[case::pino_transport("pino-transport")]
// #[case::pino_transport_targets("pino-transport-targets")] // #[case::pino_transport_targets("pino-transport-targets")]
...@@ -298,7 +296,18 @@ fn node_file_trace(input_path: &str) -> Result<()> { ...@@ -298,7 +296,18 @@ fn node_file_trace(input_path: &str) -> Result<()> {
let package_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let package_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let package_root = package_root.join("tests/node-file-trace"); let package_root = package_root.join("tests/node-file-trace");
let input: RcStr = format!("test/unit/{input_path}/input.js").into(); let entry_name = match input_path {
"jsx-input" => "input.jsx",
"tsx-input" => "input.tsx",
"ts-input-esm" => "input.ts",
"module-create-require-no-mixed"
| "module-create-require-named-require"
| "module-create-require-named-import"
| "module-create-require-ignore-other"
| "module-create-require-destructure" => "input.mjs",
_ => "input.js",
};
let input: RcStr = format!("test/unit/{input_path}/{entry_name}").into();
let reference = package_root.join(format!("test/unit/{input_path}/output.js")); let reference = package_root.join(format!("test/unit/{input_path}/output.js"));
r.block_on(async move { r.block_on(async move {
...@@ -330,9 +339,12 @@ fn node_file_trace(input_path: &str) -> Result<()> { ...@@ -330,9 +339,12 @@ fn node_file_trace(input_path: &str) -> Result<()> {
} else { } else {
let reference = reference.into_iter().collect::<Vec<_>>().join("\n"); let reference = reference.into_iter().collect::<Vec<_>>().join("\n");
let list = list.into_iter().collect::<Vec<_>>().join("\n"); let list = list.into_iter().collect::<Vec<_>>().join("\n");
let diff = TextDiff::from_lines(&reference, &list);
println!( println!(
"{}", "{}",
print_changeset(&Changeset::new(reference.trim(), list.trim(), "\n")) diff.unified_diff()
.context_radius(3)
.header("expected", "actual")
); );
anyhow::bail!("file trace does not match reference"); anyhow::bail!("file trace does not match reference");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment