-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(nextjs): Stop baking absolute paths into server chunks #23267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chargome
wants to merge
2
commits into
develop
Choose a base branch
from
fix/nextjs-orchestrion-relocatable-externals
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
dev-packages/e2e-tests/test-applications/nextjs-16-orchestrion/tests/build-output.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { findAbsolutePathImports } from '@sentry-internal/test-utils'; | ||
| import * as path from 'path'; | ||
|
|
||
| test('emits no absolute-path imports into the server output', () => { | ||
| const leaks = findAbsolutePathImports({ outputDir: path.join(process.cwd(), '.next', 'server') }); | ||
|
|
||
| expect(leaks).toEqual([]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
|
|
||
| interface AbsolutePathImportOptions { | ||
| /** Directory holding the emitted bundles, e.g. `<app>/.next/server`. */ | ||
| outputDir: string; | ||
| /** Only used to shorten the reported file paths. Defaults to `process.cwd()`. */ | ||
| buildDir?: string; | ||
| /** File extensions to scan. Defaults to JavaScript output. */ | ||
| extensions?: string[]; | ||
| } | ||
|
|
||
| const SPECIFIER_PATTERNS = [ | ||
| /\brequire\(\s*["']([^"']+)["']\s*\)/g, | ||
| /\bimport\(\s*["']([^"']+)["']\s*\)/g, | ||
| /\bfrom\s*["']([^"']+)["']/g, | ||
| ]; | ||
|
|
||
| /** | ||
| * Returns every absolute-path module specifier in the emitted output, as `<file> → <specifier>`. | ||
| * | ||
| * Such a specifier is baked in at build time, so it only resolves on the build machine: every | ||
| * deploy that relocates the output (Vercel, Docker, `output: 'standalone'`) turns it into a | ||
| * `MODULE_NOT_FOUND` on the first request reaching that chunk. A suite that builds and runs in | ||
| * place can't observe that, hence the direct assertion. | ||
| * | ||
| * Only specifiers count, not any occurrence of a build path — Next.js bakes those into chunks as | ||
| * metadata (`resolvedPagePath`, client-reference proxies), which is inert on relocation. | ||
| */ | ||
| export function findAbsolutePathImports({ | ||
| outputDir, | ||
| buildDir = process.cwd(), | ||
| extensions = ['.js', '.mjs', '.cjs'], | ||
| }: AbsolutePathImportOptions): string[] { | ||
| if (!fs.existsSync(outputDir)) { | ||
| throw new Error(`[findAbsolutePathImports] Output directory does not exist: ${outputDir}`); | ||
| } | ||
|
|
||
| const leaks: string[] = []; | ||
|
|
||
| for (const entry of fs.readdirSync(outputDir, { recursive: true, withFileTypes: true })) { | ||
| if (!entry.isFile() || !extensions.includes(path.extname(entry.name))) { | ||
| continue; | ||
| } | ||
|
|
||
| const file = path.join(entry.parentPath, entry.name); | ||
| const contents = fs.readFileSync(file, 'utf8'); | ||
|
|
||
| for (const pattern of SPECIFIER_PATTERNS) { | ||
| for (const match of contents.matchAll(pattern)) { | ||
| const specifier = match[1] as string; | ||
| if (path.isAbsolute(specifier)) { | ||
| leaks.push(`${path.relative(buildDir, file)} → ${specifier}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return leaks; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,10 @@ | |
| "import": { | ||
| "default": "./build/import-hook.mjs" | ||
| } | ||
| }, | ||
| "./orchestrion-runtime/*": { | ||
| "require": "./build/orchestrion-runtime/*.js", | ||
| "default": "./build/orchestrion-runtime/*.js" | ||
| } | ||
| }, | ||
| "publishConfig": { | ||
|
|
@@ -111,5 +115,19 @@ | |
| "volta": { | ||
| "extends": "../../package.json" | ||
| }, | ||
| "nx": { | ||
| "targets": { | ||
| "build:transpile": { | ||
| "outputs": [ | ||
| "{projectRoot}/build/esm", | ||
| "{projectRoot}/build/cjs", | ||
| "{projectRoot}/build/npm/esm", | ||
| "{projectRoot}/build/npm/cjs", | ||
| "{projectRoot}/build/import-hook.mjs", | ||
| "{projectRoot}/build/orchestrion-runtime" | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
|
Comment on lines
+118
to
+131
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nx and CI only keep the build files a package explicitly declares, so this was needed for tarballs, artifacts etc |
||
| "sideEffects": false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.