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 = [
"rstest_reuse",
"serde",
"serde_json",
"similar",
"tokio",
"turbo-rcstr",
"turbo-tasks",
......
use std::fmt::Display;
use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ValueToString, Vc, trace::TraceRawVcs};
#[turbo_tasks::value(shared)]
......@@ -243,6 +244,13 @@ impl Platform {
Self::Unknown => "unknown",
}
}
pub fn path_separator(&self) -> RcStr {
match self {
Self::Win32 => rcstr!("\\"),
_ => rcstr!("/"),
}
}
}
impl Display for Platform {
......
......@@ -605,7 +605,7 @@ async fn well_known_object_member(
let new_value = match kind {
WellKnownObjectKind::GlobalObject => global_object(prop),
WellKnownObjectKind::PathModule | WellKnownObjectKind::PathModuleDefault => {
path_module_member(kind, prop)
path_module_member(kind, prop, compile_time_info).await?
}
WellKnownObjectKind::FsModule
| WellKnownObjectKind::FsModuleDefault
......@@ -685,8 +685,12 @@ fn global_object(prop: JsValue) -> JsValue {
}
}
fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
match (kind, prop.as_str()) {
async fn path_module_member(
kind: WellKnownObjectKind,
prop: JsValue,
compile_time_info: Vc<CompileTimeInfo>,
) -> Result<JsValue> {
Ok(match (kind, prop.as_str()) {
(.., Some("join")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathJoin),
(.., Some("dirname")) => JsValue::WellKnownFunction(WellKnownFunctionKind::PathDirname),
(.., Some("resolve")) => {
......@@ -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")) => {
JsValue::WellKnownObject(WellKnownObjectKind::PathModuleDefault)
}
......@@ -706,7 +717,7 @@ fn path_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
true,
"unsupported property on Node.js path module",
),
}
})
}
fn fs_module_member(kind: WellKnownObjectKind, prop: JsValue) -> JsValue {
......
......@@ -37,6 +37,7 @@ turbopack-node = { workspace = true, default-features = false, features = ["proc
criterion = { workspace = true, features = ["async_tokio"] }
difference = "2.0"
similar = "2.2.0"
rstest = { workspace = true }
rstest_reuse = "0.5.0"
tokio = { workspace = true }
......
......@@ -2,6 +2,8 @@ use std::{collections::VecDeque, fmt::Write as _};
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 {
assert!(changeset.split == "\n");
let mut result = String::from("--- DIFF ---\n- Expected\n+ Actual\n------------\n");
......
const fs = require('fs')
const { sep } = require('path')
const X = sep
......
......@@ -6,9 +6,9 @@ mod helpers;
use std::{path::PathBuf, sync::LazyLock};
use anyhow::Result;
use difference::Changeset;
use regex::Regex;
use rstest::*;
use similar::TextDiff;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_backend::TurboTasksBackend;
......@@ -36,8 +36,6 @@ use turbopack_core::{
use turbopack_ecmascript::AnalyzeMode;
use turbopack_resolve::resolve_options_context::ResolveOptionsContext;
use crate::helpers::print_changeset;
#[global_allocator]
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::non_analyzable_requires("non-analyzable-requires")]
#[case::null_destructure("null-destructure")]
// #[case::path_sep("path-sep")]
#[case::path_sep("path-sep")]
// #[case::phantomjs_prebuilt("phantomjs-prebuilt")]
// #[case::pino_transport("pino-transport")]
// #[case::pino_transport_targets("pino-transport-targets")]
......@@ -298,7 +296,18 @@ fn node_file_trace(input_path: &str) -> Result<()> {
let package_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
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"));
r.block_on(async move {
......@@ -330,9 +339,12 @@ fn node_file_trace(input_path: &str) -> Result<()> {
} else {
let reference = reference.into_iter().collect::<Vec<_>>().join("\n");
let list = list.into_iter().collect::<Vec<_>>().join("\n");
let diff = TextDiff::from_lines(&reference, &list);
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");
}
......
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