Unverified Commit 469a7c55 authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Add symlinks and additional roots to NFT metadata (#98469)

Implements the following extension:

```typescript
export interface NftFileList {
  // File paths relative to the directory containing the `.nft.json` file, or
  // the current root (if inside of `NftAdditionalRoot`).
  //
  // When using webpack, these paths may exist outside of the tracing root. The
  // [`@vercel/next` package ignores these paths][vc-next].
  //
  // When using Turbopack, these paths are all guaranteed to exist within the
  // `turbopack.root` specified or inferred from the `next.config.js` file.
  //
  // [vc-next]: https://github.com/vercel/vercel/blob/%40vercel/next%404.20.5/packages/next/src/server-build.ts#L1022-L1026
  files: string[]
  // Turbopack extension: A parallel array to `files` (same indices and length)
  // with file content hashes. For symlinks, the hash of the path of the target
  // is stored.
  fileHashes?: string[]
  // Turbopack extension: Explicit symlink mapping information.
  //
  // If included, it's safe to assume that if a file is not in `symlinks` that
  // it is not a symlink. If this is an empty array, there are no symlinks.
  //
  // If omitted, the processor of the nft file must call `read_link` on every
  // file to determine if it is a symlink and determine the target path.
  //
  // This field is always included if `NftJson` includes `additionalRoots`, and
  // it is always included on `NftAdditionalRoot`.
  symlinks?: NftSymlink[]
}

export interface NftJson extends NftFileList {
  version: 1
  // Turbopack extension: A hash of the entrypoint that refers to these traced
  // files. This hash only depends on the content of the entrypoint file, and
  // not all of its traced dependencies.
  entryHash?: string
  // Turbopack extension: Paths stored with different base paths, typically
  // outside of the tracing root.
  additionalRoots?: NftAdditionalRoot[]
}

// Turbopack extension: A collection of paths stored with a different base path.
export interface NftAdditionalRoot extends NftFileList {
  // Stable unique identifier provided in the `next.config.js`. This can be used
  // to generate the output path where these files are copied to (e.g.
  // `.additionalRoots/$[name}`).
  //
  // This is guaranteed to use a character set that is valid on most
  // filesystems, and the identifiers are guaranteed to not have overlaps on
  // case-insensitive filesystems.
  name: string
  // A source path on the build machine that the paths in `files` are relative
  // to. The final build output directory should not depend on this path.
  absolutePath: string
  // Always specified on NftAdditionalRoot.
  symlinks: NftSymlink[]
}

// Turbopack extension: Information on a symlink, including which additional
// root it maps to. Symlinks that do not cross root boundaries (the common case)
// omit the index into `additionalRoots`.
//
// It is often complicated to transform raw symlink targets to root-relative
// paths, and including this information here ensures that the NFT reader gets
// the same result that Turbopack's tracing system expects.
//
// Because the link target type is unspecified, on Windows the reader needs to
// call `stat` to determine if a link target is a directory or file.
export type NftSymlink =
  | [
      // Index of `files` that refers to a symlink.
      number,
      // The target path of the link. In `NftJson`, this path is relative to the
      // directory containing the `.nft.json` file. In `NftAdditionalRoot`, this
      // is relative to the current root.
      string,
    ]
  | [
      // Index of `files` that refers to a symlink.
      number,
      // The target path of the link relative to the specified root.
      string,
      // An index into `additionalRoots`, -1 if the target path is relative to
      // the `.nft.json` file's directory,
      //
      // If the symlink target is relative to the same root as the symlink
      // itself (the "current root"), this field is omitted.
      number,
    ]
```

Nothing currently populates `additionalRoots`.
https://github.com/vercel/next.js/pull/98003 will do it.
parent d7a8d015
...@@ -9312,6 +9312,7 @@ dependencies = [ ...@@ -9312,6 +9312,7 @@ dependencies = [
"notify", "notify",
"omnipath", "omnipath",
"parking_lot", "parking_lot",
"pathdiff",
"rand 0.10.1", "rand 0.10.1",
"regex", "regex",
"rstest", "rstest",
......
...@@ -18,6 +18,7 @@ mod module_graph; ...@@ -18,6 +18,7 @@ mod module_graph;
pub mod next_server_nft; pub mod next_server_nft;
mod nft; mod nft;
mod nft_json; mod nft_json;
mod nft_json_builder;
pub mod operation; pub mod operation;
mod pages; mod pages;
mod path_utils; mod path_utils;
......
...@@ -4,7 +4,6 @@ use anyhow::{Context, Result, bail}; ...@@ -4,7 +4,6 @@ use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use either::Either; use either::Either;
use next_core::{get_next_package, next_server::get_tracing_compile_time_info}; use next_core::{get_next_package, next_server::get_tracing_compile_time_info};
use serde_json::json;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, trace::TraceRawVcs}; use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -16,6 +15,7 @@ use turbopack::externals_tracing_module_context; ...@@ -16,6 +15,7 @@ use turbopack::externals_tracing_module_context;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
context::AssetContext, context::AssetContext,
file_source::FileSource,
module::{Module, Modules}, module::{Module, Modules},
module_graph::{GraphEntries, ModuleGraph, SingleModuleGraph}, module_graph::{GraphEntries, ModuleGraph, SingleModuleGraph},
output::{OutputAsset, OutputAssets, OutputAssetsReference}, output::{OutputAsset, OutputAssets, OutputAssetsReference},
...@@ -24,7 +24,7 @@ use turbopack_core::{ ...@@ -24,7 +24,7 @@ use turbopack_core::{
}; };
use turbopack_resolve::ecmascript::cjs_resolve; use turbopack_resolve::ecmascript::cjs_resolve;
use crate::{nft::traced_modules_for_entries, project::Project}; use crate::{nft::traced_modules_for_entries, nft_json_builder::NftJsonBuilder, project::Project};
/// The modules `next/dist/server/require-hook` resolves its aliased requests to at runtime /// The modules `next/dist/server/require-hook` resolves its aliased requests to at runtime
/// (currently all of styled-jsx), so that the Pages Router renderer and user code share a single /// (currently all of styled-jsx), so that the Pages Router renderer and user code share a single
...@@ -187,11 +187,8 @@ impl Asset for ServerNftJsonAsset { ...@@ -187,11 +187,8 @@ impl Asset for ServerNftJsonAsset {
let this = self.await?; let this = self.await?;
// Example: [project]/apps/my-website/.next/ // Example: [project]/apps/my-website/.next/
let base_dir = this let nft_path = self.path().owned().await?;
.project let mut nft_json = NftJsonBuilder::new(this.project, &nft_path).await?;
.project_root_path()
.await?
.join(&this.project.node_root().await?.path)?;
let module_graph = ModuleGraph::from_graphs( let module_graph = ModuleGraph::from_graphs(
vec![SingleModuleGraph::new_with_entries( vec![SingleModuleGraph::new_with_entries(
...@@ -205,7 +202,7 @@ impl Asset for ServerNftJsonAsset { ...@@ -205,7 +202,7 @@ impl Asset for ServerNftJsonAsset {
let hash_salt = this.project.next_config().output_hash_salt(); let hash_salt = this.project.next_config().output_hash_salt();
let mut server_output_assets = traced_modules_for_entries( let server_output_assets = traced_modules_for_entries(
module_graph, module_graph,
Modules::empty(), Modules::empty(),
self.entries(), self.entries(),
...@@ -215,33 +212,36 @@ impl Asset for ServerNftJsonAsset { ...@@ -215,33 +212,36 @@ impl Asset for ServerNftJsonAsset {
.await? .await?
.iter() .iter()
.map(async |m| { .map(async |m| {
let path = m.ident().await?.path.clone();
let source = m.source().await?.context("NFT module has no content")?;
let content = source.content();
Ok(( Ok((
base_dir path,
.get_relative_path_to(&m.ident().await?.path) content
.context("failed to compute relative path for server NFT JSON")?,
m.source()
.await?
.context("NFT module has no content")?
.content()
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex) .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.owned()
.await?, .await?,
content.await?,
)) ))
}) })
.try_join() .try_join()
.await?; .await?;
for (path, hash, content) in server_output_assets {
nft_json.add(path, hash, &content)?;
}
let next_dir = get_next_package(this.project.project_path().owned().await?).await?; let next_dir = get_next_package(this.project.project_path().owned().await?).await?;
for ty in ["app-page", "pages"] { for ty in ["app-page", "pages"] {
let dir = next_dir.join(&format!("dist/server/route-modules/{ty}"))?; let dir = next_dir.join(&format!("dist/server/route-modules/{ty}"))?;
let module_path = dir.join("module.compiled.js")?; let module_path = dir.join("module.compiled.js")?;
server_output_assets.push(( let content = FileSource::new(module_path.clone()).content();
base_dir let hash = content
.get_relative_path_to(&module_path) .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.context("failed to compute relative path for server NFT JSON")?, .owned()
module_path .await?;
.hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex) let content = content.await?;
.await?, nft_json.add(module_path, hash, &content)?;
));
let contexts_dir = dir.join("vendored/contexts")?; let contexts_dir = dir.join("vendored/contexts")?;
let DirectoryContent::Entries(contexts_files) = &*contexts_dir.read_dir().await? else { let DirectoryContent::Entries(contexts_files) = &*contexts_dir.read_dir().await? else {
...@@ -255,32 +255,21 @@ impl Asset for ServerNftJsonAsset { ...@@ -255,32 +255,21 @@ impl Asset for ServerNftJsonAsset {
continue; continue;
}; };
if file.extension() == Some("js") { if file.extension() == Some("js") {
server_output_assets.push(( let content = FileSource::new(file.clone()).content();
base_dir let hash = content
.get_relative_path_to(file) .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.context("failed to compute relative path for server NFT JSON")?, .owned()
file.hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex) .await?;
.await?, let content = content.await?;
)) nft_json.add(file.clone(), hash, &content)?;
} }
} }
} }
server_output_assets.sort_unstable(); let json = serde_json::to_string(&nft_json.into_json(None))?;
// Dedupe as some entries may be duplicates: a file might be referenced multiple times,
// e.g. as a RawModule (from an FS operation) and as an EcmascriptModuleAsset because it
// was required.
server_output_assets.dedup();
let (files, file_hashes): (Vec<_>, Vec<_>) = server_output_assets.into_iter().unzip();
let json = json!({
"version": 1,
"files": files,
"fileHashes": file_hashes
});
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from(json.to_string())).cell(), FileContent::Content(File::from(json)).cell(),
)) ))
} }
} }
......
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result};
use either::Either;
use serde_json::json;
use tracing::{Instrument, Level, Span}; use tracing::{Instrument, Level, Span};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
graph::{AdjacencyMap, GraphTraversal, Visit}, graph::{AdjacencyMap, GraphTraversal, Visit},
turbofmt,
}; };
use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath, glob::Glob}; use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath, glob::Glob};
use turbo_tasks_hash::HashAlgorithm; use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
file_source::FileSource,
module::Module, module::Module,
output::{OutputAsset, OutputAssets, OutputAssetsReference}, output::{OutputAsset, OutputAssets, OutputAssetsReference},
}; };
use crate::{ use crate::{
nft::{EndpointTraceResult, tracing_exclude_glob}, nft::{EndpointTraceResult, tracing_exclude_glob},
nft_json_builder::NftJsonBuilder,
project::Project, project::Project,
}; };
...@@ -82,28 +81,6 @@ impl OutputAsset for NftJsonAsset { ...@@ -82,28 +81,6 @@ impl OutputAsset for NftJsonAsset {
} }
} }
fn get_output_specifier(
path_ref: &FileSystemPath,
ident_folder: &FileSystemPath,
ident_folder_in_project_fs: &FileSystemPath,
output_root: &FileSystemPath,
project_root: &FileSystemPath,
) -> Result<RcStr> {
// include assets in the outputs such as referenced chunks
if path_ref.is_inside_ref(output_root) {
return Ok(ident_folder.get_relative_path_to(path_ref).unwrap());
}
// include assets in the project root such as images and traced references (externals)
if path_ref.is_inside_ref(project_root) {
return Ok(ident_folder_in_project_fs
.get_relative_path_to(path_ref)
.unwrap());
}
// This should effectively be unreachable
bail!("NftJsonAsset: cannot handle filepath '{path_ref}'");
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Asset for NftJsonAsset { impl Asset for NftJsonAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -116,20 +93,14 @@ impl Asset for NftJsonAsset { ...@@ -116,20 +93,14 @@ impl Asset for NftJsonAsset {
async move { async move {
let project_path = this.project.project_path().owned().await?; let project_path = this.project.project_path().owned().await?;
let output_root_ref = this.project.output_fs().root().await?;
let project_root_ref = this.project.project_fs().root().await?;
let next_config = this.project.next_config(); let next_config = this.project.next_config();
let hash_salt = next_config.output_hash_salt(); let hash_salt = next_config.output_hash_salt();
let client_root = this.project.client_fs().root(); let client_root = this.project.client_fs().root();
let client_root = client_root.owned().await?; let client_root = client_root.owned().await?;
// [project]/ let nft_path = self.path().owned().await?;
let project_root_path = this.project.project_root_path().owned().await?; let mut nft_json = NftJsonBuilder::new(this.project, &nft_path).await?;
// Example: [output]/apps/my-website/.next/server/app -- without the `page.js.nft.json`
let ident_folder = self.path().await?.parent();
// Example: [project]/apps/my-website/.next/server/app -- without the `page.js.nft.json`
let ident_folder_in_project_fs = project_root_path.join(&ident_folder.path)?;
let chunk = this.chunk; let chunk = this.chunk;
let entries = this let entries = this
...@@ -158,7 +129,7 @@ impl Asset for NftJsonAsset { ...@@ -158,7 +129,7 @@ impl Asset for NftJsonAsset {
let traced_files = this.traced_files.await?; let traced_files = this.traced_files.await?;
let module_data = traced_files.module_data.await?; let module_data = traced_files.module_data.await?;
let mut result: Vec<(RcStr, _)> = all_assets let result = all_assets
.iter() .iter()
.filter(|a| **a != chunk) .filter(|a| **a != chunk)
.copied() .copied()
...@@ -171,15 +142,18 @@ impl Asset for NftJsonAsset { ...@@ -171,15 +142,18 @@ impl Asset for NftJsonAsset {
.map(AssetOrModule::Module), .map(AssetOrModule::Module),
) )
.map(async |referenced| { .map(async |referenced| {
let (referenced_chunk_path, hash) = match referenced { let (referenced_chunk_path, hash, content) = match referenced {
AssetOrModule::Asset(v) => ( AssetOrModule::Asset(v) => {
Either::Left(v.path().await?), let content = v.content().to_resolved().await?;
Either::Left( (
v.content() v.path().owned().await?,
content
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex) .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.owned()
.await?, .await?,
), content.await?,
), )
}
AssetOrModule::Module(v) => { AssetOrModule::Module(v) => {
let ident = module_data let ident = module_data
.idents .idents
...@@ -191,82 +165,37 @@ impl Asset for NftJsonAsset { ...@@ -191,82 +165,37 @@ impl Asset for NftJsonAsset {
.get(&v) .get(&v)
.await? .await?
.context("missing hash for module")?; .context("missing hash for module")?;
(Either::Right(ident.path.clone()), Either::Right(hash)) let source = v.source().await?.context("NFT module has no content")?;
(
ident.path.clone(),
(**hash).clone(),
source.content().await?,
)
} }
}; };
let referenced_chunk_path = match &referenced_chunk_path {
Either::Left(p) => &**p,
Either::Right(p) => p,
};
if referenced_chunk_path.has_extension(".map") { if referenced_chunk_path.has_extension(".map") {
return Ok(None); return Ok(None);
} }
let specifier = match get_output_specifier( Ok(Some((referenced_chunk_path, hash, content)))
referenced_chunk_path,
&ident_folder,
&ident_folder_in_project_fs,
&output_root_ref,
&project_root_ref,
) {
Ok(specifier) => specifier,
Err(err) => {
// ast-grep-ignore: no-context-turbofmt
return Err(err.context(
turbofmt!(
"NftJsonAsset: cannot handle filepath \
'{referenced_chunk_path}', it is not under the output_root: \
'{output_root_ref}' or the project_root: '{project_root_ref}'",
)
.await?,
));
}
};
Ok(Some((specifier, hash)))
}) })
.try_flat_join() .try_flat_join()
.await?; .await?;
result.extend( for (path, hash, content) in result {
traced_files nft_json.add(path, hash, &content)?;
.includes }
.iter()
.map(async |file_path| {
let relative_path = ident_folder_in_project_fs
.get_relative_path_to(file_path)
.unwrap();
Ok((
relative_path,
Either::Left(
file_path
.hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.await?,
),
))
})
.try_join()
.await?,
);
// Some of the output assets may have been included multiple times (in multiple chunking
// contexts), or asset contexts.
result.sort_unstable();
result.dedup();
let (files, file_hashes): (Vec<_>, Vec<_>) = result for file_path in &traced_files.includes {
.iter() let content = FileSource::new(file_path.clone()).content();
.map(|(name, hash)| { let hash = content
( .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
name, .owned()
match hash { .await?;
Either::Left(v) => &**v, let content = content.await?;
Either::Right(v) => &**v, nft_json.add(file_path.clone(), hash, &content)?;
}, }
)
})
.unzip();
// We can't just add this into "files" because Next.js sometimes decides to delete // We can't just add this into "files" because Next.js sometimes decides to delete
// output files such as `.next/server/pages/index.js` if that page was prerendered and // output files such as `.next/server/pages/index.js` if that page was prerendered and
// is fully static. An alternative would be to postprocess the nft file so that // is fully static. An alternative would be to postprocess the nft file so that
...@@ -276,16 +205,12 @@ impl Asset for NftJsonAsset { ...@@ -276,16 +205,12 @@ impl Asset for NftJsonAsset {
let entry_hash = chunk let entry_hash = chunk
.content() .content()
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex) .hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.owned()
.await?; .await?;
let json = json!({ let json = serde_json::to_string(&nft_json.into_json(Some(entry_hash)))?;
"version": 1,
"files": files,
"fileHashes": file_hashes,
"entryHash": entry_hash,
});
Ok(AssetContent::file( Ok(AssetContent::file(
FileContent::Content(File::from(json.to_string())).cell(), FileContent::Content(File::from(json)).cell(),
)) ))
} }
.instrument(span) .instrument(span)
......
use anyhow::{Result, bail};
use rustc_hash::FxHashMap;
use serde::{Serialize, Serializer, ser::SerializeTuple};
use turbo_rcstr::RcStr;
use turbo_tasks::ResolvedVc;
use turbo_tasks_fs::{FileSystem, FileSystemPath};
use turbo_unix_path::get_relative_path_to;
use turbopack_core::asset::AssetContent;
use crate::project::Project;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum AssetLocation {
Base { path: RcStr },
AdditionalRoot { root_index: usize, path: RcStr },
}
impl AssetLocation {
fn parts(&self) -> (Option<usize>, &RcStr) {
match self {
Self::Base { path } => (None, path),
Self::AdditionalRoot { root_index, path } => (Some(*root_index), path),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct AssetReference {
location: AssetLocation,
hash: RcStr,
/// Present for symlinks.
symlink_target: Option<AssetLocation>,
}
struct AdditionalRootConfig {
name: RcStr,
path: RcStr,
}
struct RootConfig {
base: RcStr,
additional_root_index: Option<usize>,
}
struct NftSymlink {
file_index: usize,
target: RcStr,
root: Option<isize>,
}
impl Serialize for NftSymlink {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut tuple = serializer.serialize_tuple(if self.root.is_some() { 3 } else { 2 })?;
tuple.serialize_element(&self.file_index)?;
tuple.serialize_element(&self.target)?;
if let Some(root) = self.root {
tuple.serialize_element(&root)?;
}
tuple.end()
}
}
#[derive(Default, Serialize)]
#[serde(rename_all = "camelCase")]
struct NftFileList {
files: Vec<RcStr>,
file_hashes: Vec<RcStr>,
symlinks: Vec<NftSymlink>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct NftAdditionalRoot {
#[serde(flatten)]
file_list: NftFileList,
name: RcStr,
path: RcStr,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct NftJson {
#[serde(flatten)]
file_list: NftFileList,
version: u8,
#[serde(skip_serializing_if = "Option::is_none")]
entry_hash: Option<RcStr>,
#[serde(skip_serializing_if = "Vec::is_empty")]
additional_roots: Vec<NftAdditionalRoot>,
}
pub(crate) struct NftJsonBuilder {
root_configs: FxHashMap<ResolvedVc<Box<dyn FileSystem>>, RootConfig>,
additional_roots: Vec<AdditionalRootConfig>,
/// These eventually get converted to `NftFileList` in `into_json`, but we store them in this
/// intermediate format because it's easier to sort and dedupe.
asset_refs: Vec<AssetReference>,
}
impl NftJsonBuilder {
pub async fn new(project: ResolvedVc<Project>, nft_path: &FileSystemPath) -> Result<Self> {
let mut root_configs = FxHashMap::default();
// Files not listed under `additionalRoots` have paths relative to the nft.json file, which
// lives in the output filesystem. The project and output filesystems share a root path, so
// their paths can be compared directly even when the output is outside the project.
let project_root = project.project_fs().root().owned().await?;
let output_base = nft_path.parent();
root_configs.insert(
project_root.fs,
RootConfig {
base: output_base.path.clone(),
additional_root_index: None,
},
);
// The NFT file includes references to bundled JS, which exists in the output filesystem. We
// treat these the same as files in the project filesystem, but use an output-relative base
// path.
root_configs.insert(
output_base.fs,
RootConfig {
base: output_base.path,
additional_root_index: None,
},
);
Ok(Self {
root_configs,
additional_roots: Vec::new(),
asset_refs: Vec::new(),
})
}
fn location_for_path(&self, path: &FileSystemPath) -> Result<AssetLocation> {
let Some(root) = self.root_configs.get(&path.fs) else {
bail!("NFT cannot handle filepath '{path}' because it is outside every accepted root")
};
let relative_path = RcStr::from(get_relative_path_to(&root.base, &path.path));
if let Some(root_index) = root.additional_root_index {
Ok(AssetLocation::AdditionalRoot {
root_index,
path: relative_path,
})
} else {
Ok(AssetLocation::Base {
path: relative_path,
})
}
}
pub fn add(&mut self, path: FileSystemPath, hash: RcStr, content: &AssetContent) -> Result<()> {
let location = self.location_for_path(&path)?;
let symlink_target = match content {
AssetContent::File(_) => None,
AssetContent::Redirect(content) => Some(self.location_for_path(&content.target)?),
};
self.asset_refs.push(AssetReference {
location,
hash,
symlink_target,
});
Ok(())
}
pub fn into_json(mut self, entry_hash: Option<RcStr>) -> NftJson {
self.asset_refs
.sort_unstable_by(|a, b| a.location.cmp(&b.location));
self.asset_refs.dedup_by(|a, b| a.location == b.location);
let mut base = NftFileList::default();
let mut roots = (0..self.additional_roots.len())
.map(|_| NftFileList::default())
.collect::<Vec<_>>();
for asset in self.asset_refs {
let (source_root, path) = asset.location.parts();
let list = match source_root {
None => &mut base,
Some(index) => &mut roots[index],
};
let file_index = list.files.len();
list.files.push(path.clone());
list.file_hashes.push(asset.hash);
if let Some(target) = asset.symlink_target {
let (target_root, target_path) = target.parts();
let root = if source_root == target_root {
None
} else {
Some(target_root.map(|index| index as isize).unwrap_or(-1))
};
list.symlinks.push(NftSymlink {
file_index,
target: target_path.clone(),
root,
});
}
}
let additional_roots = self
.additional_roots
.into_iter()
.zip(roots)
.map(|(root, list)| NftAdditionalRoot {
file_list: list,
name: root.name,
path: root.path,
})
.collect();
NftJson {
file_list: base,
version: 1,
entry_hash,
additional_roots,
}
}
}
...@@ -45,8 +45,8 @@ use turbo_tasks::{ ...@@ -45,8 +45,8 @@ use turbo_tasks::{
}; };
use turbo_tasks_env::{EnvMap, ProcessEnv}; use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DiskFileSystem, DiskWatcherConfig, FileContent, FileSystem, FileSystemPath, VirtualFileSystem, DiskFileSystem, DiskFileSystemMap, DiskWatcherConfig, FileContent, FileSystem, FileSystemPath,
canonicalize_to_rcstr, invalidation, VirtualFileSystem, canonicalize_to_rcstr, invalidation,
}; };
use turbo_unix_path::join_path; use turbo_unix_path::join_path;
use turbopack::{ use turbopack::{
...@@ -1071,6 +1071,7 @@ impl Project { ...@@ -1071,6 +1071,7 @@ impl Project {
)), )),
..Default::default() ..Default::default()
}, },
DiskFileSystemMap::empty(),
)) ))
} }
......
...@@ -62,6 +62,7 @@ import { Bundler } from '../../lib/bundler' ...@@ -62,6 +62,7 @@ import { Bundler } from '../../lib/bundler'
import { resolveCacheHandlerPathToFilesystem } from '../../lib/format-dynamic-import-path' import { resolveCacheHandlerPathToFilesystem } from '../../lib/format-dynamic-import-path'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import type { __ApiPreviewProps } from '../../server/api-utils' import type { __ApiPreviewProps } from '../../server/api-utils'
import { mapNftFileEntries, type NftJson } from '../nft'
interface SharedRouteFields { interface SharedRouteFields {
/** /**
...@@ -2765,26 +2766,17 @@ async function loadNFT( ...@@ -2765,26 +2766,17 @@ async function loadNFT(
repoRoot: string, repoRoot: string,
traceFilePath: string traceFilePath: string
): Promise<{ entryHash?: string }> { ): Promise<{ entryHash?: string }> {
const { files, fileHashes, entryHash } = (await JSON.parse( const nft = JSON.parse(await fs.readFile(traceFilePath, 'utf8')) as NftJson
await fs.readFile(traceFilePath, 'utf8')
)) as { // This call site only records source locations and hashes, so it does not need
files: string[] // the mapped symlink targets.
fileHashes?: string[] for (const entry of mapNftFileEntries(nft, traceFilePath, repoRoot)) {
entryHash?: string assets[entry.destination] = entry.source
} if (entry.hash) {
assetsHashes[entry.destination] = entry.hash
const traceFileDir = path.dirname(traceFilePath)
for (let i = 0; i < files.length; i++) {
const relativeFile = files[i]
const contentHash = fileHashes?.[i]
const tracedFilePath = path.join(traceFileDir, relativeFile)
const fileOutputPath = path.relative(repoRoot, tracedFilePath)
assets[fileOutputPath] = tracedFilePath
if (contentHash) {
assetsHashes[fileOutputPath] = contentHash
} }
} }
return { entryHash } return { entryHash: nft.entryHash }
} }
async function hashFile(salt: string, filePath: string): Promise<string> { async function hashFile(salt: string, filePath: string): Promise<string> {
......
import path from 'path'
/** Serialized file-list fields shared by an NFT and each additional root. */
export interface NftFileList {
/**
* File paths relative to the directory containing the `.nft.json` file, or
* the current root when inside `NftAdditionalRoot`.
*
* When using webpack, these paths may exist outside the tracing root. The
* [`@vercel/next` package](https://github.com/vercel/vercel/blob/%40vercel/next%404.20.5/packages/next/src/server-build.ts#L1022-L1026)
* ignores these paths.
*
* When using Turbopack, these paths are guaranteed to exist within the
* `turbopack.root` specified or inferred from `next.config.js`.
*/
files: string[]
/**
* Turbopack extension: A parallel array to `files` with the same indices and
* length containing file content hashes. For symlinks, this stores the hash
* of the target path.
*/
fileHashes?: string[]
/**
* Turbopack extension: Explicit symlink mapping information.
*
* When present, files not listed in `symlinks` are not symlinks. An empty
* array means there are no symlinks. When omitted, the NFT consumer must call
* `readlink` on every file to identify symlinks and their targets. Entries
* are sorted by file index.
*
* This field is always included when `NftJson` includes `additionalRoots`,
* and on every `NftAdditionalRoot`.
*/
symlinks?: NftSymlink[]
}
/** Serialized contents of a `.nft.json` trace file. */
export interface NftJson extends NftFileList {
version: 1
/**
* Turbopack extension: A hash of the entrypoint that refers to these traced
* files. This hash depends only on the entrypoint's contents, not on its
* traced dependencies.
*/
entryHash?: string
/**
* Turbopack extension: Paths stored with different base paths, typically
* outside the tracing root.
*/
additionalRoots?: NftAdditionalRoot[]
}
/** Turbopack extension: Paths stored with a different base path. */
export interface NftAdditionalRoot extends NftFileList {
/**
* Stable unique identifier provided in `next.config.js`. This can be used to
* generate the output path where these files are copied, such as
* `nextAdditionalRoots/${name}`.
*
* This uses a character set that is valid on most filesystems, and identifiers
* are guaranteed not to overlap on case-insensitive filesystems.
*/
name: string
/**
* Source root that the paths in `files` are relative to, represented relative
* to the directory containing the `.nft.json` file.
*/
path: string
/** Always specified on `NftAdditionalRoot`. */
symlinks: NftSymlink[]
}
/**
* Turbopack extension: Information about a symlink, including which additional
* root it maps to. Symlinks that do not cross root boundaries (the common case)
* omit the index into `additionalRoots`.
*
* Transforming raw symlink targets into root-relative paths can be complicated;
* including this information ensures that the NFT consumer gets the same result
* expected by Turbopack's tracing system.
*
* Because the link target type is unspecified, on Windows the consumer must call
* `stat` to determine whether a link target is a directory or file.
*/
export type NftSymlink =
| [
/** Index in `files` that refers to a symlink. */
number,
/**
* Link target path. In `NftJson`, this is relative to the directory
* containing the `.nft.json` file. In `NftAdditionalRoot`, this is
* relative to the current root.
*/
string,
]
| [
/** Index in `files` that refers to a symlink. */
number,
/** Link target path relative to the specified root. */
string,
/**
* Index into `additionalRoots`, or `-1` when the target path is relative
* to the `.nft.json` file's directory.
*
* This element is omitted when the target is relative to the same root as
* the symlink itself.
*/
number,
]
export interface MappedNftFileEntry {
source: string
destination: string
hash?: string
/**
* If the `NftJson` has a top-level `symlink` field, it can be assumed that an
* `undefined` value here means that the file is not a symlink.
*/
symlinkTarget?: string
}
function invalid(message: string): never {
throw new Error(`Invalid NFT metadata: ${message}`)
}
function isRelativePathInside(relative: string): boolean {
return (
relative === '' ||
(!path.isAbsolute(relative) &&
relative !== '..' &&
!relative.startsWith(`..${path.sep}`))
)
}
function relativePathIfInside(
root: string,
candidate: string
): string | undefined {
const relative = path.relative(root, candidate)
return isRelativePathInside(relative) ? relative : undefined
}
function mapBasePath(
traceFileDirectory: string,
baseRoot: string,
relativePath: string
): { source: string; destination: string } {
const source = path.resolve(traceFileDirectory, relativePath)
return { source, destination: path.relative(baseRoot, source) }
}
function mapBasePathInsideRoot(
traceFileDirectory: string,
baseRoot: string,
relativePath: string
): { source: string; destination: string } {
const mapped = mapBasePath(traceFileDirectory, baseRoot, relativePath)
if (!isRelativePathInside(mapped.destination)) {
invalid(`path ${JSON.stringify(relativePath)} escapes the base root`)
}
return mapped
}
function mapAdditionalRootPath(
traceFileDirectory: string,
root: NftAdditionalRoot,
relativePath: string
): { source: string; destination: string } {
const rootPath = path.resolve(traceFileDirectory, root.path)
const source = path.resolve(rootPath, relativePath)
if (relativePathIfInside(rootPath, source) === undefined) {
invalid(
`path ${JSON.stringify(relativePath)} escapes additional root ${root.name}`
)
}
return {
source,
destination: path.join('nextAdditionalRoots', root.name, relativePath),
}
}
export function mapNftFileEntries(
nft: NftJson,
traceFilePath: string,
baseRoot: string,
options?: {
skipBaseRootEscapes?: boolean
onBaseRootEscape?: (source: string) => void
}
): MappedNftFileEntry[] {
const traceFileDirectory = path.dirname(traceFilePath)
const roots = nft.additionalRoots ?? []
const result: MappedNftFileEntry[] = []
const mapList = (list: NftFileList, currentRootIndex: number) => {
// The list of symlinks is always in sorted order (by file index)
const { files, fileHashes } = list
const symlinks = list.symlinks ?? []
let symlinkCursor = 0
let nextSymlink = symlinks[symlinkCursor]
for (let fileIndex = 0; fileIndex < files.length; fileIndex++) {
const file = files[fileIndex]
let symlink: NftSymlink | undefined
if (nextSymlink?.[0] === fileIndex) {
symlink = nextSymlink
nextSymlink = symlinks[++symlinkCursor]
}
// a currentRootIndex of -1 denotes a path relative to the *.nft.json file
// (i.e. not an additional root)
const mapped =
currentRootIndex === -1
? mapBasePath(traceFileDirectory, baseRoot, file)
: mapAdditionalRootPath(
traceFileDirectory,
roots[currentRootIndex],
file
)
if (
currentRootIndex === -1 &&
options?.skipBaseRootEscapes &&
!isRelativePathInside(mapped.destination)
) {
options.onBaseRootEscape?.(mapped.source)
continue
}
let symlinkTarget: string | undefined
if (symlink !== undefined) {
const [, target, rootIndex] = symlink
const targetRootIndex = rootIndex ?? currentRootIndex
symlinkTarget =
targetRootIndex === -1
? (options?.skipBaseRootEscapes
? mapBasePathInsideRoot(traceFileDirectory, baseRoot, target)
: mapBasePath(traceFileDirectory, baseRoot, target)
).destination
: mapAdditionalRootPath(
traceFileDirectory,
roots[targetRootIndex],
target
).destination
}
result.push({
...mapped,
hash: fileHashes?.[fileIndex],
symlinkTarget,
})
}
}
mapList(nft, -1)
for (let rootIndex = 0; rootIndex < roots.length; rootIndex++) {
mapList(roots[rootIndex], rootIndex)
}
return result
}
export function resolveNftOutputPath(
outputRoot: string,
destination: string
): string {
const outputPath = path.resolve(outputRoot, destination)
if (relativePathIfInside(outputRoot, outputPath) === undefined) {
invalid(
`output path ${JSON.stringify(destination)} escapes the deployment root`
)
}
return outputPath
}
...@@ -85,6 +85,7 @@ import { parseNormalizedAppRoute } from '../shared/lib/router/routes/app' ...@@ -85,6 +85,7 @@ import { parseNormalizedAppRoute } from '../shared/lib/router/routes/app'
import { getStaticMetadataPrerenderPathname } from '../lib/metadata/get-metadata-route' import { getStaticMetadataPrerenderPathname } from '../lib/metadata/get-metadata-route'
import { isStaticMetadataFile } from '../lib/metadata/is-metadata-route' import { isStaticMetadataFile } from '../lib/metadata/is-metadata-route'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { mapNftFileEntries, type NftJson, resolveNftOutputPath } from './nft'
/** /**
* Get the display path for build output. For static metadata files under * Get the display path for build output. For static metadata files under
...@@ -1269,59 +1270,102 @@ export async function copyTracedFiles( ...@@ -1269,59 +1270,102 @@ export async function copyTracedFiles(
await fs.writeFile(packageJsonOutputPath, packageJsonContent) await fs.writeFile(packageJsonOutputPath, packageJsonContent)
} catch {} } catch {}
const copiedFiles = new Set() const copiedFiles = new Set()
const skippedTraceFiles = new Set<string>()
async function createTracedSymlink(
target: string,
linkPath: string,
sourcePath: string
) {
let isDirectory = false
if (process.platform === 'win32') {
// Windows requires the target type when creating a symlink. Files are
// copied in an arbitrary order, so the target might not exist in the
// output yet. Inspect the original target through the source symlink
// instead.
try {
isDirectory = (await fs.stat(sourcePath)).isDirectory()
} catch (err: any) {
if (err.code !== 'ENOENT' && err.code !== 'ELOOP') {
throw err
}
}
}
try {
// the target type argument is ignored on non-windows platforms
await fs.symlink(target, linkPath, isDirectory ? 'dir' : 'file')
} catch (err: any) {
// Windows doesn't support creating symlinks without elevated privileges,
// unless "Developer Mode" is turned on. If we failed to create a symlink
// due to EPERM, try creating a junction point instead.
//
// Ideally we'd just preserve the input file type (junction point or
// symlink), but there's no API in node.js to differentiate between a
// junction point and a symlink, so we just try making a symlink first.
// Symlinks are preferred because they support relative paths and
// non-directory (file) targets.
//
// Note: Junction targets are stored as absolute paths, so this fallback
// is not relocatable even when the preferred symlink above is relative,
// but it's the best we can do.
if (process.platform === 'win32' && err.code === 'EPERM' && isDirectory) {
try {
await fs.symlink(
path.resolve(path.dirname(linkPath), target),
linkPath,
'junction'
)
} catch (junctionErr: any) {
if (junctionErr.code !== 'EEXIST') {
throw junctionErr
}
}
} else if (err.code !== 'EEXIST') {
throw err
}
}
}
async function handleTraceFiles(traceFilePath: string) { async function handleTraceFiles(traceFilePath: string) {
const traceData = JSON.parse( const traceData = JSON.parse(
await fs.readFile(/* turbopackIgnore: true */ traceFilePath, 'utf8') await fs.readFile(/* turbopackIgnore: true */ traceFilePath, 'utf8')
) as { ) as NftJson
files: string[] const entries = mapNftFileEntries(traceData, traceFilePath, tracingRoot, {
} skipBaseRootEscapes: true,
const copySema = new Sema(10, { capacity: traceData.files.length }) onBaseRootEscape: (source) => skippedTraceFiles.add(source),
const traceFileDir = path.dirname(traceFilePath) })
const copySema = new Sema(10, { capacity: entries.length })
await Promise.all( await Promise.all(
traceData.files.map(async (relativeFile) => { entries.map(async (entry) => {
await copySema.acquire() await copySema.acquire()
const tracedFilePath = path.join(traceFileDir, relativeFile) const tracedFilePath = entry.source
const fileOutputPath = path.join( const fileOutputPath = resolveNftOutputPath(
outputPath, outputPath,
path.relative(tracingRoot, tracedFilePath) entry.destination
) )
if (!copiedFiles.has(fileOutputPath)) { if (!copiedFiles.has(fileOutputPath)) {
copiedFiles.add(fileOutputPath) copiedFiles.add(fileOutputPath)
await fs.mkdir(path.dirname(fileOutputPath), { recursive: true }) await fs.mkdir(path.dirname(fileOutputPath), { recursive: true })
const symlink = await fs.readlink(tracedFilePath).catch(() => null) if (entry.symlinkTarget !== undefined) {
const targetOutputPath = resolveNftOutputPath(
if (symlink) { outputPath,
try { entry.symlinkTarget
await fs.symlink(symlink, fileOutputPath) )
} catch (err: any) { const target =
// Windows doesn't support creating symlinks without elevated privileges, unless path.relative(path.dirname(fileOutputPath), targetOutputPath) ||
// "Developer Mode" is turned on. If we failed to create a symlink due to EPERM, try '.'
// creating a junction point instead. await createTracedSymlink(target, fileOutputPath, tracedFilePath)
// } else if (traceData.symlinks === undefined) {
// Ideally we'd just preserve the input file type (junction point or symlink), but const target = await fs.readlink(tracedFilePath).catch(() => null)
// there's no API in node.js to differentiate between a junction point and a symlink, if (target) {
// so we just try making a symlink first. Symlinks are preferred because they support await createTracedSymlink(target, fileOutputPath, tracedFilePath)
// relative paths and non-directory (file) targets. } else {
if ( await fs.copyFile(tracedFilePath, fileOutputPath)
process.platform === 'win32' &&
err.code === 'EPERM' &&
path.isAbsolute(symlink)
) {
try {
await fs.symlink(symlink, fileOutputPath, 'junction')
} catch (junctionErr: any) {
if (junctionErr.code !== 'EEXIST') {
throw junctionErr
}
}
} else if (err.code !== 'EEXIST') {
throw err
}
} }
} else { } else {
await fs.copyFile(tracedFilePath, fileOutputPath) await fs.copyFile(tracedFilePath, fileOutputPath)
...@@ -1473,6 +1517,23 @@ startServer({ ...@@ -1473,6 +1517,23 @@ startServer({
process.exit(1); process.exit(1);
});` });`
) )
if (skippedTraceFiles.size > 0) {
const count = skippedTraceFiles.size
const skippedFilesOutput = [...skippedTraceFiles]
.slice(0, 100)
.map((file) => ` - ${path.relative(tracingRoot, file)}`)
.join('\n')
const warning = [
`${count} traced files were not included in the standalone output`,
'because their paths are outside of `outputFileTracingRoot`.',
'First 100 skipped files:',
skippedFilesOutput,
'Set `outputFileTracingRoot` to a common parent directory',
'to include these files.',
].join('\n')
Log.warn(warning)
}
} }
export function isReservedPage(page: string) { export function isReservedPage(page: string) {
......
...@@ -19,6 +19,7 @@ import fs from 'fs' ...@@ -19,6 +19,7 @@ import fs from 'fs'
import path from 'path' import path from 'path'
import loadConfig from '../../server/config' import loadConfig from '../../server/config'
import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants' import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants'
import { mapNftFileEntries, type NftJson } from '../../build/nft'
export interface StaticRoutesInfoOptions { export interface StaticRoutesInfoOptions {
json?: boolean json?: boolean
...@@ -347,32 +348,30 @@ function collectServerEntryFiles( ...@@ -347,32 +348,30 @@ function collectServerEntryFiles(
sets: FileSets sets: FileSets
): void { ): void {
const entryRel = path.join('server', serverEntry) // e.g. server/app/page.js const entryRel = path.join('server', serverEntry) // e.g. server/app/page.js
const entryDirRel = path.dirname(entryRel) // e.g. server/app
const entryDirAbs = path.join(distDir, entryDirRel)
// The entry .js is always part of the bundle, even if no nft.json exists. // The entry .js is always part of the bundle, even if no nft.json exists.
sets.serverBundled.add(entryRel) sets.serverBundled.add(entryRel)
const nft = readJsonFile<{ files: string[] }>( const nftPath = path.join(distDir, entryRel + '.nft.json')
path.join(distDir, entryRel + '.nft.json') const nft = readJsonFile<NftJson>(nftPath)
) if (!nft) return
if (!nft?.files) return const entries = mapNftFileEntries(nft, nftPath, path.parse(nftPath).root)
for (const relPath of nft.files) { for (const entry of entries) {
// Resolve relative to the entry's dir. If the normalized result stays // NFT paths are relative to the entry's directory. If the mapped source
// inside distDir it's a server chunk; if it leaves distDir it's an // stays inside distDir it's a server chunk; if it leaves distDir it's an
// unbundled trace dep (e.g. ../../../node_modules/...). // unbundled trace dependency (e.g. ../../../node_modules/...).
const inDistDirPath = path.normalize(path.join(entryDirRel, relPath)) const inDistDirPath = path.relative(distDir, entry.source)
const outsideDistDir = inDistDirPath.startsWith('..') const outsideDistDir =
path.isAbsolute(inDistDirPath) ||
inDistDirPath === '..' ||
inDistDirPath.startsWith(`..${path.sep}`)
const isMap = inDistDirPath.endsWith('.map') const isMap = inDistDirPath.endsWith('.map')
if (isMap) { if (isMap) {
// Source maps go into the maps category whether they're in or outside // Source maps go into the maps category whether they're in or outside
// distDir, so they don't double-count under serverUnbundled. // distDir, so they don't double-count under serverUnbundled.
sets.serverMaps.add( sets.serverMaps.add(outsideDistDir ? entry.source : inDistDirPath)
outsideDistDir ? path.resolve(entryDirAbs, relPath) : inDistDirPath
)
} else if (outsideDistDir) { } else if (outsideDistDir) {
sets.serverUnbundled.add(path.resolve(entryDirAbs, relPath)) sets.serverUnbundled.add(entry.source)
} else if ( } else if (
inDistDirPath.endsWith('.js') && inDistDirPath.endsWith('.js') &&
!inDistDirPath.endsWith('_client-reference-manifest.js') !inDistDirPath.endsWith('_client-reference-manifest.js')
......
import path from 'path' import path from 'path'
import { nextTestSetup } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
describe('build trace with extra entries in monorepo', () => { describe('build trace with extra entries in monorepo', () => {
describe('production mode', () => { describe('production mode', () => {
...@@ -26,4 +26,36 @@ describe('build trace with extra entries in monorepo', () => { ...@@ -26,4 +26,36 @@ describe('build trace with extra entries in monorepo', () => {
) )
}) })
}) })
// @force-gate webpack
describe('standalone output outside outputFileTracingRoot', () => {
const { next, skipped } = nextTestSetup({
files: {
app: new FileRef(path.join(__dirname, 'app/app')),
'../other': new FileRef(path.join(__dirname, 'other')),
},
subDir: 'app',
nextConfig: {
output: 'standalone',
outputFileTracingRoot: '.',
outputFileTracingIncludes: {
'/route1': ['../other/included.txt'],
},
},
skipStart: true,
skipDeployment: true,
})
if (skipped) return
it('warns and completes the build', async () => {
const { exitCode, cliOutput } = await next.runCommand(['build'])
expect(exitCode).toBe(0)
expect(cliOutput).toMatch(
/\d+ traced files were not included in the standalone output/
)
expect(cliOutput).toContain('First 100 skipped files:')
expect(cliOutput).toContain('outputFileTracingRoot')
})
})
}) })
...@@ -145,6 +145,7 @@ async function readNormalizedNFT(next, name) { ...@@ -145,6 +145,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/build/get-supported-browsers.js", "/node_modules/next/dist/build/get-supported-browsers.js",
"/node_modules/next/dist/build/next-config-ts/require-hook.js", "/node_modules/next/dist/build/next-config-ts/require-hook.js",
"/node_modules/next/dist/build/next-config-ts/transpile-config.js", "/node_modules/next/dist/build/next-config-ts/transpile-config.js",
"/node_modules/next/dist/build/nft.js",
"/node_modules/next/dist/build/output/format.js", "/node_modules/next/dist/build/output/format.js",
"/node_modules/next/dist/build/output/log.js", "/node_modules/next/dist/build/output/log.js",
"/node_modules/next/dist/build/segment-config/app/app-segment-config.js", "/node_modules/next/dist/build/segment-config/app/app-segment-config.js",
......
...@@ -39,6 +39,7 @@ jsonc-parser = { version = "0.26.3", features = ["serde"] } ...@@ -39,6 +39,7 @@ jsonc-parser = { version = "0.26.3", features = ["serde"] }
mime = { workspace = true } mime = { workspace = true }
notify = { workspace = true } notify = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
pathdiff = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true, features = ["rc"] } serde = { workspace = true, features = ["rc"] }
......
...@@ -8,19 +8,19 @@ use std::{ ...@@ -8,19 +8,19 @@ use std::{
path::Path, path::Path,
}; };
use anyhow::{Result, bail}; use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use jsonc_parser::{ParseOptions, parse_to_serde_value}; use jsonc_parser::{ParseOptions, parse_to_serde_value};
use mime::Mime; use mime::Mime;
use serde_json::Value; use serde_json::Value;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{NonLocalValue, ReadRef, ValueToString, Vc, trace::TraceRawVcs}; use turbo_tasks::{NonLocalValue, ReadRef, ResolvedVc, ValueToString, Vc, trace::TraceRawVcs};
use turbo_tasks_hash::{ use turbo_tasks_hash::{
DeterministicHash, DeterministicHasher, HashAlgorithm, deterministic_hash, hash_xxh3_hash64, DeterministicHash, DeterministicHasher, HashAlgorithm, deterministic_hash, hash_xxh3_hash64,
}; };
use crate::{ use crate::{
FileSystemEntryType, FileSystemPath, RealPathErrorType, DiskFileSystem, FileSystemEntryType, FileSystemPath, RealPathErrorType,
json::UnparsableJson, json::UnparsableJson,
retry::retry_blocking, retry::retry_blocking,
rope::{Rope, RopeReader}, rope::{Rope, RopeReader},
...@@ -171,7 +171,10 @@ pub(crate) enum FileComparison { ...@@ -171,7 +171,10 @@ pub(crate) enum FileComparison {
#[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, NonLocalValue, Encode, Decode)] #[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, NonLocalValue, Encode, Decode)]
pub enum LinkTarget { pub enum LinkTarget {
/// The link is an absolute path on disk. /// The link is an absolute path on disk.
Absolute { resolved: FileSystemPath }, Absolute {
raw: RcStr,
resolved: FileSystemPath,
},
Relative { Relative {
/// The value read from the link. The path is lexically converted to a [unix-style /// The value read from the link. The path is lexically converted to a [unix-style
/// path][turbo_unix_path::sys_to_unix], but it may contain `..` relative to the *directory /// path][turbo_unix_path::sys_to_unix], but it may contain `..` relative to the *directory
...@@ -186,7 +189,9 @@ impl LinkTarget { ...@@ -186,7 +189,9 @@ impl LinkTarget {
/// The path this link points at. /// The path this link points at.
pub fn file_system_path(&self) -> &FileSystemPath { pub fn file_system_path(&self) -> &FileSystemPath {
match self { match self {
LinkTarget::Absolute { resolved } | LinkTarget::Relative { resolved, .. } => resolved, LinkTarget::Absolute { resolved, .. } | LinkTarget::Relative { resolved, .. } => {
resolved
}
} }
} }
...@@ -243,52 +248,6 @@ pub enum LinkContent { ...@@ -243,52 +248,6 @@ pub enum LinkContent {
Invalid { reason: RcStr }, Invalid { reason: RcStr },
} }
#[turbo_tasks::value_impl]
impl LinkContent {
/// Hashes the link itself (its target and type), not the content of whatever the link points
/// at. This mirrors [`FileContent::hash`] and is the right content hash for consumers that
/// re-create a symlink as a symlink instead of copying the resolved file.
#[turbo_tasks::function]
pub async fn hash(&self, salt: Vc<RcStr>, algorithm: HashAlgorithm) -> Result<Vc<RcStr>> {
#[derive(DeterministicHash)]
enum SimplifiedLinkContent<'a> {
Absolute(&'a RcStr),
Relative(&'a RcStr),
NotFound,
Invalid, // the actual error message doesn't matter for this API
}
let simplified = match self {
LinkContent::Link { target } => match target {
LinkTarget::Absolute { resolved } => {
SimplifiedLinkContent::Absolute(&resolved.path)
}
LinkTarget::Relative { raw, resolved: _ } => SimplifiedLinkContent::Relative(raw),
},
LinkContent::NotFound => SimplifiedLinkContent::NotFound,
LinkContent::Invalid { reason: _ } => SimplifiedLinkContent::Invalid,
};
Ok(Vc::cell(RcStr::from(deterministic_hash(
&salt.await?,
simplified,
algorithm,
))))
}
}
/// The target of a symbolic link to create, used by [`WriteLinkContent`].
///
/// Unlike [`LinkTarget`] this carries only the raw path: the write side never needs the target
/// resolved, and the link being created may not even exist yet.
#[derive(
Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, NonLocalValue, DeterministicHash, Encode, Decode,
)]
pub enum WriteLinkTarget {
/// Normalized and relative to the *filesystem root*.
Absolute(RcStr),
/// Written verbatim, relative to the *directory containing the link*.
Relative(RcStr),
}
/// The file type of the target of a newly written link. This value is only used on Windows. /// The file type of the target of a newly written link. This value is only used on Windows.
#[derive( #[derive(
Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, NonLocalValue, DeterministicHash, Encode, Decode, Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, NonLocalValue, DeterministicHash, Encode, Decode,
...@@ -308,12 +267,32 @@ pub enum WriteLinkTargetType { ...@@ -308,12 +267,32 @@ pub enum WriteLinkTargetType {
/// directories, because symlink creation may fail if "developer mode" is not enabled and we're /// directories, because symlink creation may fail if "developer mode" is not enabled and we're
/// running in an unprivileged environment. /// running in an unprivileged environment.
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, Debug, DeterministicHash)] #[derive(Clone, Debug)]
pub struct WriteLinkContent { pub struct WriteLinkContent {
pub target: WriteLinkTarget, pub target: FileSystemPath,
pub target_type: WriteLinkTargetType, pub target_type: WriteLinkTargetType,
} }
impl WriteLinkContent {
/// Hashes the link target and target type, not the target's contents. Returns a hash that is
/// stable across builds.
pub async fn hash(&self, salt: &RcStr, algorithm: HashAlgorithm) -> Result<RcStr> {
// convert the fs Vc (which is not stable across cold builds therefore cannot implement
// DeterministicHash) to the configured name, which should be globally unique and stable
// across cold builds.
let target_fs = ResolvedVc::try_downcast_type::<DiskFileSystem>(self.target.fs)
.context("link target must use a disk filesystem")?
.await?;
let target_fs_name = target_fs.name();
Ok(RcStr::from(deterministic_hash(
salt,
(target_fs_name, &self.target.path, &self.target_type),
algorithm,
)))
}
}
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Clone, DeterministicHash, PartialOrd, Ord)] #[derive(Clone, DeterministicHash, PartialOrd, Ord)]
pub struct File { pub struct File {
......
This diff is collapsed.
use std::{
collections::BTreeMap,
ops::Bound,
path::{Path, PathBuf},
};
use turbo_rcstr::RcStr;
use turbo_tasks::{OperationVc, ResolvedVc, Vc};
use turbo_unix_path::sys_to_unix;
use crate::{DiskFileSystem, FileSystemPath};
/// An ordered set of canonical system roots and their owning filesystems.
///
/// The roots must not overlap: no root may be an ancestor of another root. [`Self::lookup`]
/// relies on this invariant when selecting the nearest preceding root in path order.
#[turbo_tasks::value(shared)]
pub struct DiskFileSystemMap(BTreeMap<PathBuf, ResolvedVc<DiskFileSystem>>);
impl FromIterator<(PathBuf, ResolvedVc<DiskFileSystem>)> for DiskFileSystemMap {
fn from_iter<T: IntoIterator<Item = (PathBuf, ResolvedVc<DiskFileSystem>)>>(iter: T) -> Self {
let filesystems = BTreeMap::from_iter(iter);
let mut map = DiskFileSystemMap(BTreeMap::new());
for (root, fs) in filesystems {
assert!(
map.lookup(&root).is_none(),
"filesystem root {} overlaps another filesystem root",
root.display()
);
map.0.insert(root, fs);
}
map
}
}
impl DiskFileSystemMap {
pub fn has_file_system_other_than(&self, current: ResolvedVc<DiskFileSystem>) -> bool {
self.0.values().any(|file_system| *file_system != current)
}
/// Converts an absolute system path into a path owned by one of the installed filesystems.
///
/// Returns `None` if the file path does not exist inside any other root, or if the relative
/// path would not be valid unicode.
pub fn lookup(&self, path: &Path) -> Option<FileSystemPath> {
let (root, fs) = self.0.upper_bound(Bound::Included(path)).peek_prev()?;
let relative = path.strip_prefix(root).ok()?.to_str()?;
Some(FileSystemPath::new_normalized_unchecked(
ResolvedVc::upcast(*fs),
RcStr::from(sys_to_unix(relative)),
))
}
/// Creates a new empty `DiskFileSystemMap`, used when constructing a [`DiskFileSystem`] that
/// cannot traverse to any other roots outside of itself.
pub fn empty() -> OperationVc<DiskFileSystemMap> {
#[turbo_tasks::function(operation)]
pub fn operation() -> Vc<DiskFileSystemMap> {
DiskFileSystemMap(BTreeMap::new()).cell()
}
operation()
}
}
#[cfg(test)]
mod tests {
use turbo_rcstr::rcstr;
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use super::*;
#[tokio::test]
async fn component_safe_lookup() {
#[turbo_tasks::function(operation, root)]
async fn assert_component_safe_lookup() -> anyhow::Result<()> {
let fs = DiskFileSystem::new(rcstr!("root"), Vc::cell(rcstr!("/tmp/root")))
.to_resolved()
.await?;
let map: DiskFileSystemMap = [(PathBuf::from("/tmp/root"), fs)].into_iter().collect();
assert!(!map.has_file_system_other_than(fs));
assert_eq!(
map.lookup(Path::new("/tmp/root/file")).unwrap().path,
"file"
);
assert!(map.lookup(Path::new("/tmp/root-other/file")).is_none());
Ok(())
}
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
tt.run_once(async {
assert_component_safe_lookup()
.read_strongly_consistent()
.await
})
.await
.unwrap();
}
}
...@@ -19,6 +19,7 @@ mod content; ...@@ -19,6 +19,7 @@ mod content;
mod disk; mod disk;
pub mod embed; pub mod embed;
mod error; mod error;
mod fs_map;
pub mod glob; pub mod glob;
mod globset; mod globset;
pub mod invalidation; pub mod invalidation;
...@@ -56,10 +57,10 @@ pub(crate) use crate::{ ...@@ -56,10 +57,10 @@ pub(crate) use crate::{
pub use crate::{ pub use crate::{
content::{ content::{
File, FileContent, FileJsonContent, FileLine, FileLinesContent, FileMeta, LinkContent, File, FileContent, FileJsonContent, FileLine, FileLinesContent, FileMeta, LinkContent,
LinkTarget, Permissions, PersistedFileContent, WriteLinkContent, WriteLinkTarget, LinkTarget, Permissions, PersistedFileContent, WriteLinkContent, WriteLinkTargetType,
WriteLinkTargetType,
}, },
disk::{DiskFileSystem, canonicalize_to_rcstr, validate_path_length}, disk::{DiskFileSystem, canonicalize_to_rcstr, validate_path_length},
fs_map::DiskFileSystemMap,
null_fs::NullFileSystem, null_fs::NullFileSystem,
path::{ path::{
FileSystemPath, FileSystemPathOption, RealPathError, RealPathErrorType, FileSystemPath, FileSystemPathOption, RealPathError, RealPathErrorType,
......
This diff is collapsed.
...@@ -21,7 +21,7 @@ use turbo_tasks::{ ...@@ -21,7 +21,7 @@ use turbo_tasks::{
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DiskFileSystem, File, FileContent, FileSystem, FileSystemPath, WriteLinkContent, DiskFileSystem, File, FileContent, FileSystem, FileSystemPath, WriteLinkContent,
WriteLinkTarget, WriteLinkTargetType, WriteLinkTargetType,
}; };
// `read_or_write_all_paths_operation` always writes the sentinel values to files/symlinks. We can // `read_or_write_all_paths_operation` always writes the sentinel values to files/symlinks. We can
...@@ -349,7 +349,7 @@ async fn write_link( ...@@ -349,7 +349,7 @@ async fn write_link(
let path_str = path.path.clone(); let path_str = path.path.clone();
invalidations.0.lock().unwrap().insert(path_str); invalidations.0.lock().unwrap().insert(path_str);
let link_content = WriteLinkContent { let link_content = WriteLinkContent {
target: WriteLinkTarget::Relative(target), target: path.parent().join(&target)?,
target_type: if is_directory { target_type: if is_directory {
WriteLinkTargetType::DirectoryOrJunctionPoint WriteLinkTargetType::DirectoryOrJunctionPoint
} else { } else {
......
...@@ -14,8 +14,7 @@ use turbo_tasks::{ ...@@ -14,8 +14,7 @@ use turbo_tasks::{
}; };
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DiskFileSystem, FileSystem, FileSystemPath, WriteLinkContent, WriteLinkTarget, DiskFileSystem, FileSystem, FileSystemPath, WriteLinkContent, WriteLinkTargetType,
WriteLinkTargetType,
}; };
#[derive(Args)] #[derive(Args)]
...@@ -240,7 +239,7 @@ async fn write_symlink( ...@@ -240,7 +239,7 @@ async fn write_symlink(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let symlink_path = symlinks_dir.join(&symlink_idx.to_string())?; let symlink_path = symlinks_dir.join(&symlink_idx.to_string())?;
let link_content = WriteLinkContent { let link_content = WriteLinkContent {
target: WriteLinkTarget::Relative(target), target: symlink_path.parent().join(&target)?,
target_type: WriteLinkTargetType::DirectoryOrJunctionPoint, target_type: WriteLinkTargetType::DirectoryOrJunctionPoint,
}; };
symlink_path symlink_path
......
...@@ -4,7 +4,9 @@ use anyhow::{Context, Result}; ...@@ -4,7 +4,9 @@ use anyhow::{Context, Result};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Vc, trace::TraceRawVcs}; use turbo_tasks::{Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{DiskFileSystem, DiskWatcherConfig, FileSystem, canonicalize_to_rcstr}; use turbo_tasks_fs::{
DiskFileSystem, DiskFileSystemMap, DiskWatcherConfig, FileSystem, canonicalize_to_rcstr,
};
#[turbo_tasks::task_input] #[turbo_tasks::task_input]
#[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, Encode, Decode)] #[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, Encode, Decode)]
...@@ -61,6 +63,7 @@ pub async fn project_fs( ...@@ -61,6 +63,7 @@ pub async fn project_fs(
Vc::cell(project_dir), Vc::cell(project_dir),
vec![denied_root_path], vec![denied_root_path],
DiskWatcherConfig::default(), DiskWatcherConfig::default(),
DiskFileSystemMap::empty(),
); );
if watch { if watch {
disk_fs.await?.start_watching().await?; disk_fs.await?.start_watching().await?;
......
...@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, Vc}; ...@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, WriteLinkContent, FileContent, FileJsonContent, FileLinesContent, FileSystemPath, WriteLinkContent,
}; };
use turbo_tasks_hash::{HashAlgorithm, deterministic_hash}; use turbo_tasks_hash::HashAlgorithm;
use crate::version::{VersionedAssetContent, VersionedContent}; use crate::version::{VersionedAssetContent, VersionedContent};
...@@ -126,10 +126,9 @@ impl AssetContent { ...@@ -126,10 +126,9 @@ impl AssetContent {
pub async fn hash(&self, salt: Vc<RcStr>, algorithm: HashAlgorithm) -> Result<Vc<RcStr>> { pub async fn hash(&self, salt: Vc<RcStr>, algorithm: HashAlgorithm) -> Result<Vc<RcStr>> {
Ok(match self { Ok(match self {
AssetContent::File(content) => content.hash(salt, algorithm), AssetContent::File(content) => content.hash(salt, algorithm),
AssetContent::Redirect(content) => Vc::cell(RcStr::from( AssetContent::Redirect(content) => {
// no_hash_salt Vc::cell(content.hash(&*salt.await?, algorithm).await?)
deterministic_hash(&salt.await?, content, algorithm), }
)),
}) })
} }
......
...@@ -2,8 +2,8 @@ use anyhow::{Result, bail}; ...@@ -2,8 +2,8 @@ use anyhow::{Result, bail};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileContent, FileSystemEntryType, FileSystemPath, LinkContent, LinkTarget, WriteLinkContent, FileContent, FileSystemEntryType, FileSystemPath, LinkContent, WriteLinkContent,
WriteLinkTarget, WriteLinkTargetType, WriteLinkTargetType,
}; };
use crate::{ use crate::{
...@@ -70,12 +70,6 @@ impl Asset for FileSource { ...@@ -70,12 +70,6 @@ impl Asset for FileSource {
match file_type { match file_type {
FileSystemEntryType::Symlink => match &*self.path.read_link().await? { FileSystemEntryType::Symlink => match &*self.path.read_link().await? {
LinkContent::Link { target } => { LinkContent::Link { target } => {
let write_target = match target {
LinkTarget::Absolute { resolved } => {
WriteLinkTarget::Absolute(resolved.path.clone())
}
LinkTarget::Relative { raw, .. } => WriteLinkTarget::Relative(raw.clone()),
};
let target_fs_path = target.file_system_path(); let target_fs_path = target.file_system_path();
let write_target_type = match *target_fs_path.get_type().await? { let write_target_type = match *target_fs_path.get_type().await? {
FileSystemEntryType::Directory => { FileSystemEntryType::Directory => {
...@@ -89,12 +83,17 @@ impl Asset for FileSource { ...@@ -89,12 +83,17 @@ impl Asset for FileSource {
_ => WriteLinkTargetType::FileNonPortable, _ => WriteLinkTargetType::FileNonPortable,
}; };
Ok(AssetContent::Redirect(WriteLinkContent { Ok(AssetContent::Redirect(WriteLinkContent {
target: write_target, target: target_fs_path.clone(),
target_type: write_target_type, target_type: write_target_type,
}) })
.cell()) .cell())
} }
_ => bail!("Invalid symlink"), LinkContent::NotFound => {
// This should not normally happen because the path was already identified as
// a symlink, but it may be removed between get_type and read_link.
Ok(AssetContent::File(FileContent::NotFound.resolved_cell()).cell())
}
LinkContent::Invalid { reason } => bail!("Invalid symlink: {reason}"),
}, },
FileSystemEntryType::File => { FileSystemEntryType::File => {
Ok(AssetContent::File(self.path.read().to_resolved().await?).cell()) Ok(AssetContent::File(self.path.read().to_resolved().await?).cell())
......
...@@ -2,7 +2,7 @@ use std::hash::Hash; ...@@ -2,7 +2,7 @@ use std::hash::Hash;
use anyhow::Result; use anyhow::Result;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc, turbobail}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::{FileSystemPath, WriteLinkContent};
use crate::{ use crate::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -77,7 +77,23 @@ impl Asset for RebasedAsset { ...@@ -77,7 +77,23 @@ impl Asset for RebasedAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn content(&self) -> Result<Vc<AssetContent>> { async fn content(&self) -> Result<Vc<AssetContent>> {
if let Some(source) = *self.module.source().await? { if let Some(source) = *self.module.source().await? {
Ok(source.content()) let source_content = source.content();
let AssetContent::Redirect(redirect) = &*source_content.await? else {
return Ok(source_content);
};
// treat symlinks targets as relative to the symlink. When we rebase the symlink, we
// should also rebase its target path
let redirect = WriteLinkContent {
target: FileSystemPath::rebase(
redirect.target.clone(),
self.input_dir.clone(),
self.output_dir.clone(),
)
.owned()
.await?,
target_type: redirect.target_type.clone(),
};
Ok(AssetContent::Redirect(redirect).cell())
} else { } else {
turbobail!("Module {} has no source", self.module.ident()); turbobail!("Module {} has no source", self.module.ident());
} }
......
use std::{borrow::Cow, fmt::Display, io::Write}; use std::{borrow::Cow, fmt::Display, io::Write};
use anyhow::{Context, Result}; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToStringRef, Vc, trace::TraceRawVcs}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToStringRef, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
FileSystem, FileSystemPath, VirtualFileSystem, WriteLinkContent, WriteLinkTarget, FileSystem, FileSystemPath, VirtualFileSystem, WriteLinkContent, WriteLinkTargetType,
WriteLinkTargetType, rope::RopeBuilder, rope::RopeBuilder,
}; };
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64}; use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64};
use turbopack_core::{ use turbopack_core::{
...@@ -497,22 +497,8 @@ impl Asset for ExternalsSymlinkAsset { ...@@ -497,22 +497,8 @@ impl Asset for ExternalsSymlinkAsset {
// path: [output]/bench/app-router-server/.next/node_modules/lodash-ee4fa714b6d81ca3 // path: [output]/bench/app-router-server/.next/node_modules/lodash-ee4fa714b6d81ca3
// target: [project]/node_modules/.pnpm/lodash@3.10.1/node_modules/lodash // target: [project]/node_modules/.pnpm/lodash@3.10.1/node_modules/lodash
let output_root_to_project_root = this.chunking_context.output_root_to_root_path().await?;
let project_root_to_target = &this.target.path;
let path = self.path().await?;
let path_to_output_root = path
.parent()
.get_relative_path_to(&*this.chunking_context.output_root().await?)
.context("path must be inside output root")?;
let target = format!(
"{path_to_output_root}/{output_root_to_project_root}/{project_root_to_target}",
)
.into();
Ok(AssetContent::Redirect(WriteLinkContent { Ok(AssetContent::Redirect(WriteLinkContent {
target: WriteLinkTarget::Relative(target), target: this.target.clone(),
target_type: WriteLinkTargetType::DirectoryOrJunctionPoint, target_type: WriteLinkTargetType::DirectoryOrJunctionPoint,
}) })
.cell()) .cell())
......
const { readlinkSync } = require('fs')
if (readlinkSync(__dirname + '/linked.js') !== 'real.js') {
throw new Error('relative symlink target was not preserved')
}
const { compute } = require('./linked') const { compute } = require('./linked')
console.log(compute()) console.log(compute())
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