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

Turbopack: gracefully handle outputFileTracingIncludes matching a symlink (#97507)

Closes https://github.com/vercel/next.js/pull/96999

Make sure we don't do `.read().hash()` which is incorrect with symlinks.
Instead, hash the symlink itself instead of its target. This is what we
copy into the function source anyway

---------
Co-authored-by: 's avatarvercel-fleet[bot] <308483924+vercel-fleet[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 36dd9e21
...@@ -237,8 +237,7 @@ impl Asset for ServerNftJsonAsset { ...@@ -237,8 +237,7 @@ impl Asset for ServerNftJsonAsset {
.get_relative_path_to(&module_path) .get_relative_path_to(&module_path)
.context("failed to compute relative path for server NFT JSON")?, .context("failed to compute relative path for server NFT JSON")?,
module_path module_path
.read() .hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.await?, .await?,
)); ));
...@@ -258,8 +257,7 @@ impl Asset for ServerNftJsonAsset { ...@@ -258,8 +257,7 @@ impl Asset for ServerNftJsonAsset {
base_dir base_dir
.get_relative_path_to(file) .get_relative_path_to(file)
.context("failed to compute relative path for server NFT JSON")?, .context("failed to compute relative path for server NFT JSON")?,
file.read() file.hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.await?, .await?,
)) ))
} }
......
...@@ -241,8 +241,7 @@ impl Asset for NftJsonAsset { ...@@ -241,8 +241,7 @@ impl Asset for NftJsonAsset {
relative_path, relative_path,
Either::Left( Either::Left(
file_path file_path
.read() .hash_file(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.hash(hash_salt, HashAlgorithm::Xxh3Hash128Hex)
.await?, .await?,
), ),
)) ))
......
...@@ -72,6 +72,23 @@ describe('build trace with extra entries', () => { ...@@ -72,6 +72,23 @@ describe('build trace with extra entries', () => {
(file: string) => file === '../../../include-me/second.txt' (file: string) => file === '../../../include-me/second.txt'
) )
).toBe(true) ).toBe(true)
if (isTurbopack) {
// A symlink matched by outputFileTracingIncludes is traced as the symlink itself, even
// when it points at a directory (this used to fail the build with
// `reading file "..." Is a directory (os error 21)`).
// The webpack tracer globs with `nodir: true`, which drops directory symlinks, so this
// only applies to Turbopack.
expect(
tracedFiles.some(
(file: string) => file === '../../../include-me/link-to-dir'
)
).toBe(true)
expect(
appDirRoute1Trace.files.some(
(file: string) => file === '../../../../include-me/link-to-dir'
)
).toBe(true)
}
expect( expect(
indexTrace.files.some((file: string) => file.includes('exclude-me')) indexTrace.files.some((file: string) => file.includes('exclude-me'))
).toBe(false) ).toBe(false)
......
...@@ -206,6 +206,21 @@ pub enum LinkContent { ...@@ -206,6 +206,21 @@ pub enum LinkContent {
NotFound, NotFound,
} }
#[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>> {
Ok(Vc::cell(RcStr::from(deterministic_hash(
&salt.await?,
self,
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 {
......
...@@ -11,6 +11,7 @@ use turbo_tasks::{ ...@@ -11,6 +11,7 @@ use turbo_tasks::{
Completion, NonLocalValue, ResolvedVc, ValueToString, ValueToStringRef, Vc, trace::TraceRawVcs, Completion, NonLocalValue, ResolvedVc, ValueToString, ValueToStringRef, Vc, trace::TraceRawVcs,
turbobail, turbofmt, turbobail, turbofmt,
}; };
use turbo_tasks_hash::HashAlgorithm;
use turbo_unix_path::{get_parent_path, get_relative_path_to, join_path, normalize_path}; use turbo_unix_path::{get_parent_path, get_relative_path_to, join_path, normalize_path};
use crate::{ use crate::{
...@@ -398,6 +399,15 @@ impl FileSystemPath { ...@@ -398,6 +399,15 @@ impl FileSystemPath {
self.fs().read(self.clone()).parse_json5() self.fs().read(self.clone()).parse_json5()
} }
/// Hashes the file content (but not as a byte-exact content hash). This does NOT follow
/// symlinks, so use this when you only want the hash of the file itself, not whatever it
/// might point to.
///
/// This is basically `isSymlink ? self.read_link().hash() : self.read().hash()`.
pub fn hash_file(&self, salt: Vc<RcStr>, algorithm: HashAlgorithm) -> Vc<RcStr> {
hash_file(self.clone(), salt, algorithm)
}
/// Reads content of a directory. /// Reads content of a directory.
/// ///
/// DETERMINISM: Result is in random order. Either sort result or do not /// DETERMINISM: Result is in random order. Either sort result or do not
...@@ -681,6 +691,25 @@ async fn realpath_with_links(path: FileSystemPath) -> Result<Vc<RealPathResult>> ...@@ -681,6 +691,25 @@ async fn realpath_with_links(path: FileSystemPath) -> Result<Vc<RealPathResult>>
.cell()) .cell())
} }
#[turbo_tasks::function]
async fn hash_file(
path: FileSystemPath,
salt: Vc<RcStr>,
algorithm: HashAlgorithm,
) -> Result<Vc<RcStr>> {
match *path.get_type().await? {
FileSystemEntryType::File => Ok(path.read().hash(salt, algorithm)),
FileSystemEntryType::Symlink => Ok(path.read_link().hash(salt, algorithm)),
FileSystemEntryType::NotFound | FileSystemEntryType::Error => {
// Should this rather be `return None`?
turbobail!("Cannot hash content of missing path {path}")
}
FileSystemEntryType::Directory | FileSystemEntryType::Other => {
turbobail!("Cannot hash content of non-file path {path}")
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
......
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