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( ...@@ -104,6 +104,7 @@ async fn apply_includes(
) -> Result<BTreeSet<RcStr>> { ) -> Result<BTreeSet<RcStr>> {
debug_assert_eq!(project_root_path.fs, ident_folder.fs); debug_assert_eq!(project_root_path.fs, ident_folder.fs);
// Read files matching the glob pattern from the project root // 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?; let glob_result = project_root_path.read_glob(glob).await?;
// Walk the full glob_result using an explicit stack to avoid async recursion overheads. // Walk the full glob_result using an explicit stack to avoid async recursion overheads.
...@@ -113,15 +114,15 @@ async fn apply_includes( ...@@ -113,15 +114,15 @@ async fn apply_includes(
while let Some(glob_result) = stack.pop_back() { while let Some(glob_result) = stack.pop_back() {
// Process direct results (files and directories at this level) // Process direct results (files and directories at this level)
for entry in glob_result.results.values() { 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; continue;
}; };
let file_path_ref = file_path;
// Convert to relative path from ident_folder to the file // Convert to relative path from ident_folder to the file
// unwrap is safe because project_root_path and ident_folder have the same filesystem // unwrap is safe because project_root_path and ident_folder have the same filesystem
// and paths produced by read_glob stay in the 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); 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') ...@@ -2,7 +2,6 @@ const path = require('path')
module.exports = { module.exports = {
webpack(cfg, { isServer, nextRuntime }) { webpack(cfg, { isServer, nextRuntime }) {
console.log(cfg.entry)
const origEntry = cfg.entry const origEntry = cfg.entry
cfg.entry = async () => { cfg.entry = async () => {
const origEntries = await origEntry() const origEntries = await origEntry()
...@@ -13,7 +12,6 @@ module.exports = { ...@@ -13,7 +12,6 @@ module.exports = {
path.join(__dirname, 'lib/get-data.js'), path.join(__dirname, 'lib/get-data.js'),
...curEntry, ...curEntry,
] ]
console.log(origEntries)
} }
return origEntries return origEntries
} }
...@@ -21,7 +19,8 @@ module.exports = { ...@@ -21,7 +19,8 @@ module.exports = {
}, },
outputFileTracingIncludes: { outputFileTracingIncludes: {
'/index': ['include-me/**/*'], '/index': ['include-me/**/*'],
'/route1': ['./include-me/**/*'], '/route1': ['./include-me/**/*', 'node_modules/pkg-behind-symlink/*'],
'/*': ['include-me-global.txt'],
}, },
outputFileTracingExcludes: { outputFileTracingExcludes: {
'/index': ['public/exclude-me/**/*'], '/index': ['public/exclude-me/**/*'],
......
...@@ -16,7 +16,6 @@ describe('build trace with extra entries', () => { ...@@ -16,7 +16,6 @@ describe('build trace with extra entries', () => {
stderr: true, stderr: true,
stdout: true, stdout: true,
}) })
console.log(result)
expect(result.code).toBe(0) expect(result.code).toBe(0)
const appTrace = await fs.readJSON( const appTrace = await fs.readJSON(
...@@ -41,16 +40,26 @@ describe('build trace with extra entries', () => { ...@@ -41,16 +40,26 @@ describe('build trace with extra entries', () => {
expect(appDirRoute1Trace.files).toContain( expect(appDirRoute1Trace.files).toContain(
'../../../../include-me/second.txt' '../../../../include-me/second.txt'
) )
expect(
appDirRoute1Trace.files.some(
(file) => file === '../../../../include-me-global.txt'
)
).toBe(true)
expect( expect(
appDirRoute1Trace.files.some((file) => file.includes('exclude-me')) appDirRoute1Trace.files.some((file) => file.includes('exclude-me'))
).toBe(false) ).toBe(false)
expect(appDirRoute1Trace.files).toEqual(
// Skip hello.json check for Turbopack as it doesn't support webpack entry modifications expect.arrayContaining([
if (!process.env.TURBOPACK_BUILD) { '../../../../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( expect(
appTrace.files.some((file) => file.endsWith('hello.json')) appDirRoute1Trace.files.some((file) =>
).toBe(true) file.startsWith('../../../../node_modules/pkg/')
} )
).toBe(false)
expect( expect(
indexTrace.files.filter( indexTrace.files.filter(
...@@ -62,12 +71,24 @@ describe('build trace with extra entries', () => { ...@@ -62,12 +71,24 @@ describe('build trace with extra entries', () => {
).length ).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 // Skip lib/get-data.js check for Turbopack as it doesn't support webpack entry modifications
if (!process.env.TURBOPACK_BUILD) { if (!process.env.TURBOPACK_BUILD) {
expect( expect(
appTrace.files.some((file) => file.endsWith('lib/get-data.js')) appTrace.files.some((file) => file.endsWith('lib/get-data.js'))
).toBe(true) ).toBe(true)
} }
expect(
appTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect( expect(
indexTrace.files.some((file) => file.endsWith('hello.json')) indexTrace.files.some((file) => file.endsWith('hello.json'))
).toBeFalsy() ).toBeFalsy()
...@@ -90,6 +111,11 @@ describe('build trace with extra entries', () => { ...@@ -90,6 +111,11 @@ describe('build trace with extra entries', () => {
expect( expect(
indexTrace.files.some((file) => file.includes('exclude-me')) indexTrace.files.some((file) => file.includes('exclude-me'))
).toBe(false) ).toBe(false)
expect(
indexTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect( expect(
anotherTrace.files.some((file) => anotherTrace.files.some((file) =>
...@@ -106,12 +132,23 @@ describe('build trace with extra entries', () => { ...@@ -106,12 +132,23 @@ describe('build trace with extra entries', () => {
file.includes('nested-structure/dist/constants.js') file.includes('nested-structure/dist/constants.js')
) )
).toBe(true) ).toBe(true)
expect(
anotherTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
expect( expect(
imageTrace.files.some((file) => file.includes('public/another.jpg')) imageTrace.files.some((file) => file.includes('public/another.jpg'))
).toBe(true) ).toBe(true)
expect( expect(
imageTrace.files.some((file) => file.includes('public/test.jpg')) imageTrace.files.some((file) => file.includes('public/test.jpg'))
).toBe(false) ).toBe(false)
expect(
imageTrace.files.some(
(file) => file === '../../../include-me-global.txt'
)
).toBe(true)
}) })
} }
) )
......
...@@ -1249,6 +1249,8 @@ impl FileSystemPath { ...@@ -1249,6 +1249,8 @@ impl FileSystemPath {
Ok(None) 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> { pub fn read_glob(&self, glob: Vc<Glob>) -> Vc<ReadGlobResult> {
read_glob(self.clone(), glob) 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