fix(build): use basename to detect package.json in copyTracedFiles#1166
Open
ash1day wants to merge 1 commit into
Open
fix(build): use basename to detect package.json in copyTracedFiles#1166ash1day wants to merge 1 commit into
ash1day wants to merge 1 commit into
Conversation
`copyTracedFiles` registered every traced file whose path ended with "package.json" as a node package. This mis-classified user data files whose names happen to share that suffix (e.g. `care-package.json`), causing the Cloudflare adapter's workerd-package copy step to log `Failed to copy <parent-dir>` errors when it tried to read a sibling `package.json` that did not exist. The actual file copy goes through `filesToCopy`, so this was always a noisy log rather than a real failure. Switching to a basename check removes the false positive entirely.
🦋 Changeset detectedLatest commit: 646b685 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
vicb
requested changes
May 12, 2026
vicb
left a comment
Contributor
There was a problem hiding this comment.
I guess using path.basename could be a proper fix?
Comment on lines
+88
to
+97
| // `path.basename` only splits on the OS-native separator, so we explicitly | ||
| // look at both `/` and `\` to handle paths produced under either platform. | ||
| export function isPackageJson(modulePath: string): boolean { | ||
| const lastSep = Math.max( | ||
| modulePath.lastIndexOf("/"), | ||
| modulePath.lastIndexOf("\\"), | ||
| ); | ||
| return modulePath.slice(lastSep + 1) === "package.json"; | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
We should avoid using OS specific constructs as much as possible.
I don't think this is needed here.
|
|
||
| const module = path.join(dotNextDir, subDir, tracedPath); | ||
| if (module.endsWith("package.json")) { | ||
| if (isPackageJson(module)) { |
Contributor
There was a problem hiding this comment.
Would that work instead?
Suggested change
| if (isPackageJson(module)) { | |
| if (path.basename(module) === "package.json") { |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
copyTracedFilesdecides whether a traced file is a node-package manifest withendsWithmatches any filename whose tail ispackage.json, so files likecare-package.jsonormy-package.jsonget registered as if their parent directory were a node package. The map is then handed to the Cloudflare adapter'scopyWorkerdPackages, which tries to read<parent-dir>/package.jsonand surfaces:The actual file copy is unaffected — that goes through
filesToCopy, which is keyed by full paths — so the error is purely cosmetic. But it's misleading enough that someone hitting it usually has to dig into OpenNext's source to convince themselves the deploy is fine, which is what motivated this patch.Repro
In a Next.js app, statically import a JSON data file whose name ends in
-package.json:Next traces that file, OpenNext walks the
.nft.json, and the inlineendsWithcheck fires oncare-package.json. Cloudflare adapter's deploy log then printsFailed to copy <dir>for each affected directory.Fix
Switch the inline check to a real basename comparison. The helper is exported so the regression is covered by a unit test alongside
isExcluded/isNonLinuxPlatformPackage.path.basenamewas avoided because traced paths can come from either platform afterpath.join, andpath.basenameonly splits on the OS-native separator. The manual scan handles both.Tests
Added three cases in
packages/tests-unit/tests/build/copyTracedFiles.test.ts:…/next/package.jsonand barepackage.json→true…/care-package.json,…/my-package.json→false\separators behave the sameRisk
Behavior only changes for filenames that share the
package.jsonsuffix but aren't actually package manifests. Realpackage.jsontraces continue to be picked up identically.Changeset
Included a
patchchangeset under.changeset/fix-copytraced-package-json-suffix.md.