Unverified Commit 7f20cbd6 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: symlinks and parent globs in `outputFileTracingIncludes` (#82528)

1. Don't resolve symlinks in `outputFileTracingIncludes`. Otherwise if you specify `node_modules/foo/*` in there, it will resolve to `node_modules/.pnpm/foo/*` and `require("foo")` won't work anymore because there no symlink in `node_modules/foo` anymore. This is also what happens with Webpack
2. Align `read_glob` with `read_dir` in that the hashmap keys only contain the path segment, and not the full path.
3. Support globing parents of the start folder: if the glob can match `..`, then also try to match `directory.parent_folder().read_glob(glob)`

Closes PACK-5219
parent bd802d13
......@@ -104,6 +104,7 @@ async fn apply_includes(
) -> Result<BTreeSet<RcStr>> {
debug_assert_eq!(project_root_path.fs, ident_folder.fs);
// Read files matching the glob pattern from the project root
// This result itself has random order, but the BTreeSet will ensure a deterministic ordering.
let glob_result = project_root_path.read_glob(glob).await?;
// Walk the full glob_result using an explicit stack to avoid async recursion overheads.
......@@ -113,15 +114,15 @@ async fn apply_includes(
while let Some(glob_result) = stack.pop_back() {
// Process direct results (files and directories at this level)
for entry in glob_result.results.values() {
let DirectoryEntry::File(file_path) = entry else {
let (DirectoryEntry::File(file_path) | DirectoryEntry::Symlink(file_path)) = entry
else {
continue;
};
let file_path_ref = file_path;
// Convert to relative path from ident_folder to the file
// unwrap is safe because project_root_path and ident_folder have the same filesystem
// and paths produced by read_glob stay in the filesystem
let relative_path = ident_folder.get_relative_path_to(file_path_ref).unwrap();
let relative_path = ident_folder.get_relative_path_to(file_path).unwrap();
result.insert(relative_path);
}
......
export const dynamic = 'force-dynamic'
export async function GET() {
return new Response('foo')
}
module.exports = {
outputFileTracingIncludes: {
'/route1': ['../other/included.txt'],
},
}
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '../app')
describe('build trace with extra entries in monorepo', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('should build and trace correctly', async () => {
const result = await nextBuild(appDir, undefined, {
cwd: appDir,
stderr: true,
stdout: true,
})
expect(result.code).toBe(0)
console.log(result.stderr)
console.log(result.stdout)
const appDirRoute1Trace = await fs.readJSON(
join(appDir, '.next/server/app/route1/route.js.nft.json')
)
expect(appDirRoute1Trace.files).toContain(
'../../../../../other/included.txt'
)
})
}
)
})
......@@ -2,7 +2,6 @@ const path = require('path')
module.exports = {
webpack(cfg, { isServer, nextRuntime }) {
console.log(cfg.entry)
const origEntry = cfg.entry
cfg.entry = async () => {
const origEntries = await origEntry()
......@@ -13,7 +12,6 @@ module.exports = {
path.join(__dirname, 'lib/get-data.js'),
...curEntry,
]
console.log(origEntries)
}
return origEntries
}
......@@ -21,7 +19,8 @@ module.exports = {
},
outputFileTracingIncludes: {
'/index': ['include-me/**/*'],
'/route1': ['./include-me/**/*'],
'/route1': ['./include-me/**/*', 'node_modules/pkg-behind-symlink/*'],
'/*': ['include-me-global.txt'],
},
outputFileTracingExcludes: {
'/index': ['public/exclude-me/**/*'],
......
......@@ -16,7 +16,6 @@ describe('build trace with extra entries', () => {
stderr: true,
stdout: true,
})
console.log(result)
expect(result.code).toBe(0)
const appTrace = await fs.readJSON(
......@@ -41,16 +40,26 @@ describe('build trace with extra entries', () => {
expect(appDirRoute1Trace.files).toContain(
'../../../../include-me/second.txt'
)
expect(
appDirRoute1Trace.files.some(
(file) => file === '../../../../include-me-global.txt'
)
).toBe(true)
expect(
appDirRoute1Trace.files.some((file) => file.includes('exclude-me'))
).toBe(false)
// Skip hello.json check for Turbopack as it doesn't support webpack entry modifications
if (!process.env.TURBOPACK_BUILD) {
expect(
appTrace.files.some((file) => file.endsWith('hello.json'))
).toBe(true)
}
expect(appDirRoute1Trace.files).toEqual(
expect.arrayContaining([
'../../../../node_modules/pkg-behind-symlink/index.js',
'../../../../node_modules/pkg-behind-symlink/package.json',
])
)
// This assertion not really needed, but ensures consistency between Turbopack and Webpack
expect(
appDirRoute1Trace.files.some((file) =>
file.startsWith('../../../../node_modules/pkg/')
)
).toBe(false)
expect(
indexTrace.files.filter(
......@@ -62,12 +71,24 @@ describe('build trace with extra entries', () => {
).length
)
// Skip hello.json check for Turbopack as it doesn't support webpack entry modifications
if (!process.env.TURBOPACK_BUILD) {
expect(
appTrace.files.some((file) => file.endsWith('hello.json'))
).toBe(true)
}
// Skip lib/get-data.js check for Turbopack as it doesn't support webpack entry modifications
if (!process.env.TURBOPACK_BUILD) {
expect(
appTrace.files.some((file) => file.endsWith('lib/get-data.js'))
).toBe(true)
}
expect(
appTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect(
indexTrace.files.some((file) => file.endsWith('hello.json'))
).toBeFalsy()
......@@ -90,6 +111,11 @@ describe('build trace with extra entries', () => {
expect(
indexTrace.files.some((file) => file.includes('exclude-me'))
).toBe(false)
expect(
indexTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect(
anotherTrace.files.some((file) =>
......@@ -106,12 +132,23 @@ describe('build trace with extra entries', () => {
file.includes('nested-structure/dist/constants.js')
)
).toBe(true)
expect(
anotherTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect(
imageTrace.files.some((file) => file.includes('public/another.jpg'))
).toBe(true)
expect(
imageTrace.files.some((file) => file.includes('public/test.jpg'))
).toBe(false)
expect(
imageTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
})
}
)
......
......@@ -1249,6 +1249,8 @@ impl FileSystemPath {
Ok(None)
}
/// DETERMINISM: Result is in random order. Either sort result or do not depend
/// on the order.
pub fn read_glob(&self, glob: Vc<Glob>) -> Vc<ReadGlobResult> {
read_glob(self.clone(), glob)
}
......
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