-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Turbopack: Add support for additionalRoots to deployment adapters #99015
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
packages/next/src/build/adapter/synthetic-symlinks.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,56 @@ | ||
| import fs from 'fs/promises' | ||
| import os from 'os' | ||
| import path from 'path' | ||
| import { createAdapterSyntheticSymlinkDirectory } from './synthetic-symlinks' | ||
|
|
||
| describe('adapter synthetic symlinks', () => { | ||
| // @force-gate !windows | ||
| describe('SyntheticSymlinkManager', () => { | ||
| let testDirectory: string | ||
|
|
||
| beforeEach(async () => { | ||
| testDirectory = await fs.mkdtemp( | ||
| path.join(os.tmpdir(), 'next-adapter-symlinks-') | ||
| ) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(testDirectory, { | ||
| recursive: true, | ||
| force: true, | ||
| maxRetries: 3, | ||
| }) | ||
| }) | ||
|
|
||
| it('cleans stale staging and reuses the staged path', async () => { | ||
| const distDir = path.join(testDirectory, '.next') | ||
| const stagingRoot = path.join(distDir, 'adapter', 'synthetic_symlinks') | ||
| const staleFile = path.join(stagingRoot, 'stale') | ||
| await fs.mkdir(stagingRoot, { recursive: true }) | ||
| await fs.writeFile(staleFile, 'stale') | ||
|
|
||
| const manager = createAdapterSyntheticSymlinkDirectory(distDir) | ||
| await expect(fs.access(staleFile)).rejects.toMatchObject({ | ||
| code: 'ENOENT', | ||
| }) | ||
|
|
||
| const source = path.join(testDirectory, 'source-link') | ||
| const linkTarget = path.relative( | ||
| path.join('functions', 'app', 'node_modules'), | ||
| path.join('next_additional_roots', 'packages', 'pkg') | ||
| ) | ||
| const targetHash = 'a'.repeat(64) | ||
| const first = manager.createLink(source, linkTarget, targetHash) | ||
| const second = manager.createLink( | ||
| path.join(testDirectory, 'equivalent-source-link'), | ||
| linkTarget, | ||
| targetHash | ||
| ) | ||
|
|
||
| expect(path.dirname(first)).toBe(stagingRoot) | ||
| expect(path.basename(first)).toBe('a'.repeat(32)) | ||
| expect(second).toBe(first) | ||
| expect(await fs.readlink(first)).toBe(linkTarget) | ||
| }) | ||
| }) | ||
| }) |
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,73 @@ | ||
| import fs from 'fs' | ||
| import path from 'path' | ||
|
|
||
| type SymlinkTargetType = 'file' | 'dir' | ||
|
|
||
| /** | ||
| * When passing cross-root symlinks from Turbopack's additional roots feature to | ||
| * the adapter, we may need to rewrite the symlink target's relative path. | ||
| * | ||
| * Adapters receive a map of `{"destination": "source"}` file paths, and they | ||
| * copy symlink paths verbatim. | ||
| * | ||
| * We must write a "synthetic" symlink at a source path (inside | ||
| * `.next/adapter/synthetic_symlinks`) for the adapter to copy to its output | ||
| * artifact (e.g. a Lambda zip file). | ||
| * | ||
| * As a future optimization, we could pass symlink information directly to the | ||
| * adapter, if the adapter signals that it supports accepting that information. | ||
| * That would avoid a lot of small filesystem operations. | ||
| */ | ||
| export class SyntheticSymlinkManager { | ||
| private readonly stagedLinkNames = new Set<string>() | ||
|
|
||
| constructor(private readonly stagingRoot: string) {} | ||
|
|
||
| createLink(source: string, linkTarget: string, targetHash: string): string { | ||
| let targetType: SymlinkTargetType = 'file' | ||
| // Keep 128 bits of entropy while shortening the path to avoid Windows' | ||
| // 260-character path limit. | ||
| let stagedName = targetHash.slice(0, 32) | ||
|
|
||
| if (process.platform === 'win32') { | ||
| try { | ||
| targetType = fs.statSync(source).isDirectory() ? 'dir' : 'file' | ||
| } catch (error) { | ||
| const code = (error as NodeJS.ErrnoException).code | ||
| // We cannot determine the target type, so just create a file symlink | ||
| // ENOENT: Dangling link, preserve the dangling link as a file link | ||
| // ELOOP: Unresolvable link cycle, preserve any part of the cycle that | ||
| // was traced | ||
| if (code !== 'ENOENT' && code !== 'ELOOP') { | ||
| throw error | ||
| } | ||
| } | ||
| stagedName += `_${targetType}` | ||
| } | ||
|
|
||
| const stagedPath = path.join(this.stagingRoot, stagedName) | ||
| if (!this.stagedLinkNames.has(stagedName)) { | ||
| try { | ||
| fs.symlinkSync(linkTarget, stagedPath, targetType) | ||
| } catch (error) { | ||
| // This link may exist if `rmSync` (with `force: true`) failed to delete | ||
| // some files (can happen on Windows), but it's content-addressed, so | ||
| // we can safely ignore EEXIST. | ||
| if ((error as NodeJS.ErrnoException).code !== 'EEXIST') { | ||
| throw error | ||
| } | ||
| } | ||
| this.stagedLinkNames.add(stagedName) | ||
| } | ||
| return stagedPath | ||
| } | ||
| } | ||
|
|
||
| export function createAdapterSyntheticSymlinkDirectory( | ||
| distDir: string | ||
| ): SyntheticSymlinkManager { | ||
| const stagingRoot = path.join(distDir, 'adapter', 'synthetic_symlinks') | ||
| fs.rmSync(stagingRoot, { recursive: true, force: true, maxRetries: 3 }) | ||
| fs.mkdirSync(stagingRoot, { recursive: true }) | ||
| return new SyntheticSymlinkManager(stagingRoot) | ||
| } | ||
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,62 @@ | ||
| import path from 'path' | ||
| import { mapNftFileEntries, type NftJson } from './nft' | ||
|
|
||
| describe('mapNftFileEntries', () => { | ||
| it('marks only symlinks whose tuple specifies another root', () => { | ||
| const traceFilePath = path.join( | ||
| path.parse(process.cwd()).root, | ||
| 'repo', | ||
| '.next', | ||
| 'server', | ||
| 'page.js.nft.json' | ||
| ) | ||
| const repoRoot = path.join(path.parse(traceFilePath).root, 'repo') | ||
| const nft: NftJson = { | ||
| version: 1, | ||
| files: ['same-root-link', 'root-zero-link', 'base-root-link'], | ||
| symlinks: [ | ||
| [0, 'same-root-target'], | ||
| [1, 'root-zero-target', 0], | ||
| [2, 'base-root-target', -1], | ||
| ], | ||
| additionalRoots: [ | ||
| { | ||
| name: 'root-zero', | ||
| path: '../../../external', | ||
| files: [], | ||
| symlinks: [], | ||
| }, | ||
| ], | ||
| } | ||
|
|
||
| const [sameRoot, rootZero, baseRoot] = mapNftFileEntries( | ||
| nft, | ||
| traceFilePath, | ||
| repoRoot | ||
| ) | ||
|
|
||
| expect(sameRoot).toEqual( | ||
| expect.objectContaining({ | ||
| symlinkTarget: path.join('.next', 'server', 'same-root-target'), | ||
| }) | ||
| ) | ||
| expect(sameRoot.symlinkCrossesRoot).toBe(false) | ||
|
|
||
| expect(rootZero).toEqual( | ||
| expect.objectContaining({ | ||
| symlinkTarget: path.join( | ||
| 'next_additional_roots', | ||
| 'root-zero', | ||
| 'root-zero-target' | ||
| ), | ||
| symlinkCrossesRoot: true, | ||
| }) | ||
| ) | ||
| expect(baseRoot).toEqual( | ||
| expect.objectContaining({ | ||
| symlinkTarget: path.join('.next', 'server', 'base-root-target'), | ||
| symlinkCrossesRoot: true, | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.