Repository navigation
[wrangler] Fix published declaration dependency leaks #16074
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
AyobamiH
wants to merge
10
commits into
cloudflare:main
Choose a base branch
from
AyobamiH:fix/15940-wrangler-declaration-boundary
base: main
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
10 commits
Select commit
Hold shift + click to select a range
09d713e
test: reconstruct PR 16074 from last-green on current main
AyobamiH 2022223
test: preserve workers-utils type identity without externalising runtime
AyobamiH ee458de
fix: close published declaration boundaries while preserving shared t…
AyobamiH a74cae8
refactor(wrangler): define command metadata once for internal and pub…
AyobamiH 4fdd0cb
refactor(wrangler): reuse shared metadata in core definitions
AyobamiH b018fce
refactor(wrangler): reuse shared metadata in experimental API
AyobamiH 1ebe991
refactor(wrangler): share command registry types across internal and …
AyobamiH 33b96e3
Merge current workers-sdk main into declaration-boundary PR
AyobamiH 02f9a5e
docs(wrangler): restore supportTemporary JSDoc default tag
AyobamiH 5a0fe56
Merge Cloudflare main into Wrangler declaration-boundary fix
AyobamiH 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "wrangler": patch | ||
| "@cloudflare/workers-utils": patch | ||
| --- | ||
|
|
||
| Fix unresolved imports in Wrangler's published TypeScript declarations | ||
|
|
||
| Projects that check dependency declarations no longer need Wrangler's private build dependencies to resolve its public types. Inherited bindings remain compatible between Wrangler and the shared utilities used by the Vite and Vitest plugins. |
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
116 changes: 116 additions & 0 deletions
116
packages/wrangler/scripts/check-published-declarations.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,116 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import ts from "typescript"; | ||
| import { | ||
| getEntryPointPaths, | ||
| getPackageNameFromSpecifier, | ||
| isBareSpecifier, | ||
| validateDistImports, | ||
| type PackageJSON, | ||
| } from "../../../tools/deployments/validate-package-dependencies"; | ||
|
|
||
| type PublishedPackageJSON = PackageJSON & { types?: string }; | ||
|
|
||
| if (require.main === module) { | ||
| checkPublishedDeclarations(path.resolve(__dirname, "..")); | ||
| // Wrangler keeps workers-utils' binding types external to preserve their unique | ||
| // symbol identity. Its installed declarations must also resolve without private | ||
| // workspace packages or development dependencies. | ||
| checkPublishedDeclarations(path.resolve(__dirname, "../../workers-utils")); | ||
| } | ||
|
|
||
| /** Validate the declaration files shipped at a package's public type entry points. */ | ||
| export function checkPublishedDeclarations(packageRoot: string): void { | ||
| const packageJson = JSON.parse( | ||
| fs.readFileSync(path.join(packageRoot, "package.json"), "utf-8") | ||
| ) as PublishedPackageJSON; | ||
|
|
||
| const typeEntryPoints = new Set( | ||
| [ | ||
| packageJson.types, | ||
| ...getEntryPointPaths(packageJson).filter(isDeclarationPath), | ||
| ].filter((value): value is string => value !== undefined) | ||
| ); | ||
|
|
||
| const declarationFiles = new Set<string>(); | ||
| for (const entryPoint of typeEntryPoints) { | ||
| const absoluteEntryPoint = path.resolve(packageRoot, entryPoint); | ||
| if (!fs.existsSync(absoluteEntryPoint)) { | ||
| throw new Error("Missing built type entry point: " + entryPoint); | ||
| } | ||
| collectDeclarationFiles(path.dirname(absoluteEntryPoint), declarationFiles); | ||
| } | ||
|
|
||
| const importedPackages = new Set<string>(); | ||
| const errors: string[] = []; | ||
| for (const declarationFile of declarationFiles) { | ||
| const preprocessed = ts.preProcessFile( | ||
| fs.readFileSync(declarationFile, "utf-8"), | ||
| true, | ||
| true | ||
| ); | ||
| const references = [ | ||
| ...preprocessed.importedFiles, | ||
| ...preprocessed.typeReferenceDirectives, | ||
| ...preprocessed.referencedFiles, | ||
| ]; | ||
| for (const { fileName } of references) { | ||
| if (isBareSpecifier(fileName)) { | ||
| importedPackages.add(getPackageNameFromSpecifier(fileName)); | ||
| } else if (fileName.startsWith(".")) { | ||
| const { resolvedModule } = ts.resolveModuleName( | ||
| fileName, | ||
| declarationFile, | ||
| { moduleResolution: ts.ModuleResolutionKind.Bundler }, | ||
| ts.sys | ||
| ); | ||
| if ( | ||
| resolvedModule === undefined || | ||
| !declarationFiles.has(path.resolve(resolvedModule.resolvedFileName)) | ||
| ) { | ||
| errors.push( | ||
| path.relative(packageRoot, declarationFile) + | ||
| " references an unpublished declaration: " + | ||
| fileName | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| errors.push( | ||
| ...validateDistImports(packageJson.name, packageJson, importedPackages) | ||
| ); | ||
| if (errors.length > 0) { | ||
| throw new Error( | ||
| packageJson.name + | ||
| "'s published declarations have unresolved package boundaries:\n" + | ||
| errors.map((error) => "- " + error).join("\n") | ||
| ); | ||
| } | ||
|
|
||
| process.stdout.write( | ||
| "Checked " + | ||
| declarationFiles.size + | ||
| " published declaration file(s) across " + | ||
| typeEntryPoints.size + | ||
| " public type entry point(s) for " + | ||
| packageJson.name + | ||
| ".\n" | ||
| ); | ||
| } | ||
|
|
||
| function isDeclarationPath(filePath: string): boolean { | ||
| return /\.d\.(?:ts|mts|cts)$/.test(filePath); | ||
| } | ||
|
|
||
| function collectDeclarationFiles(directory: string, files: Set<string>): void { | ||
| for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { | ||
| const absolutePath = path.join(directory, entry.name); | ||
| if (entry.isDirectory()) { | ||
| collectDeclarationFiles(absolutePath, files); | ||
| } else if (isDeclarationPath(entry.name)) { | ||
| files.add(absolutePath); | ||
| } | ||
| } | ||
| } |
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.
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.