Skip to content

[workers-utils] Fix compat date resolution in workspaces#11910

Closed
ksawaneh wants to merge 2 commits intocloudflare:mainfrom
ksawaneh:fix/workers-utils-compat-date
Closed

[workers-utils] Fix compat date resolution in workspaces#11910
ksawaneh wants to merge 2 commits intocloudflare:mainfrom
ksawaneh:fix/workers-utils-compat-date

Conversation

@ksawaneh
Copy link
Copy Markdown
Contributor

@ksawaneh ksawaneh commented Jan 14, 2026

getLocalWorkerdCompatibilityDate() could fail in workspaces because it used require.resolve("miniflare"), and miniflare’s main points at dist/ (which may not exist until built). That caused the function to fall back even though workerd is installed.

This change:

  • Resolves miniflare/package.json instead, so resolution works even before miniflare is built
  • Updates the unit test to use Wrangler’s workspace context for resolution
  • Adds a changeset for @cloudflare/workers-utils

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:

Commands run:

  • pnpm check

  • pnpm test:ci --filter @cloudflare/workers-utils

  • Public documentation

    • Cloudflare docs PR(s):
    • Documentation not necessary because: no user-facing behavior/docs change (internal resolution fix + tests)

A picture of a cute animal (not mandatory, but encouraged)

/_/\
( o.o )

^ <


Open with Devin

@ksawaneh ksawaneh requested a review from a team as a code owner January 14, 2026 09:52
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 14, 2026

🦋 Changeset detected

Latest commit: 3981ff3

The changes in this PR will be included in the next version bump.

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

Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little trick!

Comment on lines +12 to +16
// Note: this works because Wrangler depends on `miniflare` (and therefore `workerd`)
// in the monorepo.
const wranglerPackageJson = fileURLToPath(
new URL("../../wrangler/package.json", import.meta.url)
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried that this is brittle due to relying upon the relative location of the wrangler package compared to this file. But I guess it is only in a test and if it changes the test will break and we'll know to change it.

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Jan 14, 2026
@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Jan 14, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jan 14, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11910

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11910

miniflare

npm i https://pkg.pr.new/miniflare@11910

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11910

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11910

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11910

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11910

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11910

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11910

wrangler

npm i https://pkg.pr.new/wrangler@11910

commit: 3981ff3

@petebacondarwin
Copy link
Copy Markdown
Contributor

@claude can you rebase and fix the conflicts on this PR

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Feb 3, 2026

Claude encountered an error —— View job

Failed with exit code 128

I'll analyze this and get back to you.

@petebacondarwin petebacondarwin requested review from a team as code owners February 3, 2026 23:00
@petebacondarwin petebacondarwin force-pushed the fix/workers-utils-compat-date branch from 06af74e to 2514e08 Compare February 3, 2026 23:03
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View issue and 2 additional flags in Devin Review.

Open in Devin Review

Comment thread packages/workers-utils/tests/compatibility-date.test.ts
@petebacondarwin
Copy link
Copy Markdown
Contributor

Really sorry! I messed up this branch when rebasing and lost your commits.
I have recreated as best I can...

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View issue and 2 additional flags in Devin Review.

Open in Devin Review

Comment on lines +17 to +22
const wranglerPackageJson = fileURLToPath(
new URL("../../wrangler/package.json", import.meta.url)
);
const { date, source } = getLocalWorkerdCompatibilityDate({
projectPath: wranglerPackageJson,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Test passes file path instead of directory path to getLocalWorkerdCompatibilityDate

The test passes a file path (wranglerPackageJson) as projectPath, but getLocalWorkerdCompatibilityDate expects a directory path and appends package.json to it.

Click to expand

How the bug is triggered

The test at lines 17-22 does:

const wranglerPackageJson = fileURLToPath(
    new URL("../../wrangler/package.json", import.meta.url)
);
const { date, source } = getLocalWorkerdCompatibilityDate({
    projectPath: wranglerPackageJson,
});

But getLocalWorkerdCompatibilityDate at packages/workers-utils/src/compatibility-date.ts:40-42 does:

const projectRequire = module.createRequire(
    path.join(projectPath, "package.json")
);

So if projectPath is /path/to/wrangler/package.json, then path.join(projectPath, "package.json") becomes /path/to/wrangler/package.json/package.json, which is an invalid path.

Actual vs Expected

  • Actual: The test passes a file path, resulting in an invalid path like /path/to/wrangler/package.json/package.json
  • Expected: The test should pass a directory path like /path/to/wrangler

Impact

The test will fail because module resolution will fail with the invalid path, causing the function to fall back to the fallback date instead of successfully resolving workerd's compatibility date.

Recommendation: Change the test to pass a directory path instead of a file path:

const wranglerDir = fileURLToPath(
    new URL("../../wrangler", import.meta.url)
);
const { date, source } = getLocalWorkerdCompatibilityDate({
    projectPath: wranglerDir,
});
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@dario-piotrowicz
Copy link
Copy Markdown
Member

if we go with the solution in #12387 I think the changes here will become irrelevant.

I do think that we need to proceed with #12387 since that solution addresses the following issue: in pnpm projects that have a wrangler dependency but not a direct miniflare dependency, require.resolve("miniflare") (and also require.resolve("miniflare/package.json")) results in a module-not-found error (since pnpm strictly isolated dependencies by default).

@petebacondarwin
Copy link
Copy Markdown
Contributor

Hey @ksawaneh - thanks for this contribution. I think we are going to land #12387 instead, which should also solve this problem. So I'm going to close this PR.

@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants