Skip to content

[wrangler] Smart cache directory detection for Yarn PnP compatibility#12466

Merged
petebacondarwin merged 8 commits intomainfrom
fix/4299-configurable-temp-dir
Feb 16, 2026
Merged

[wrangler] Smart cache directory detection for Yarn PnP compatibility#12466
petebacondarwin merged 8 commits intomainfrom
fix/4299-configurable-temp-dir

Conversation

@petebacondarwin
Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin commented Feb 7, 2026

Fixes #4299.

This PR improves how Wrangler and Miniflare determine where to store cache files, making them compatible with Yarn PnP (Plug'n'Play) and other package managers that don't use traditional node_modules directories.

Wrangler Cache Directory Resolution

Wrangler now uses the following priority to determine the cache directory:

  1. Environment variable override: WRANGLER_CACHE_DIR
  2. Existing cache: Use existing node_modules/.cache/wrangler or .wrangler/cache if found
  3. node_modules available: Create cache in node_modules/.cache/wrangler
  4. Fallback: Use .wrangler/cache

Miniflare Cache Directory Resolution

Miniflare now uses the following priority to determine where to store its cf.json cache:

  1. Environment variable override: MINIFLARE_CACHE_DIR
  2. Existing cache: Use existing node_modules/.mf or .wrangler/cache if found
  3. node_modules available: Create cache in node_modules/.mf
  4. Fallback: Use .wrangler/cache

This approach:

  • Preserves backward compatibility for existing projects
  • Works automatically with Yarn PnP, pnpm, and other non-node_modules setups
  • Requires no configuration for most users

Environment Variables

  • WRANGLER_CACHE_DIR - Override wrangler's cache directory
  • MINIFLARE_CACHE_DIR - Override miniflare's cf.json cache directory

Implementation Notes

The cache folder path is computed fresh on each call (no in-memory caching). This design choice:

  • Ensures correct behavior when working directory changes (e.g., in tests)
  • Has negligible performance impact (~2-3 filesystem lookups per command)
  • Simplifies the code and improves test isolation

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

🦔

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 7, 2026

🦋 Changeset detected

Latest commit: 189646a

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

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Feb 7, 2026

Claude finished @petebacondarwin's task —— View job


Changeset Review Complete

Todo List:

  • Read changeset guidelines from .changeset/README.md
  • Review .changeset/configurable-temp-dir.md
  • Review .changeset/exclude-wrangler-from-pages-upload.md
  • Review .changeset/miniflare-cache-dir.md
  • Post review findings

All changesets look good

All three changesets follow the guidelines:

  • Version types are appropriate (minor for new env vars/features, patch for bug fix and miniflare cache)
  • Descriptions are clear and meaningful
  • No problematic markdown headers
  • No analytics concerns

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 7, 2026

create-cloudflare

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

@cloudflare/kv-asset-handler

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

miniflare

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

@cloudflare/pages-shared

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

@cloudflare/unenv-preset

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

@cloudflare/vite-plugin

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

@cloudflare/vitest-pool-workers

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

@cloudflare/workers-editor-shared

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

@cloudflare/workers-utils

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

wrangler

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

commit: 189646a

@petebacondarwin petebacondarwin force-pushed the fix/4299-configurable-temp-dir branch 4 times, most recently from ef70325 to 64524ff Compare February 13, 2026 21:32
@petebacondarwin petebacondarwin force-pushed the fix/4299-configurable-temp-dir branch from 64524ff to e26e830 Compare February 16, 2026 09:37
@petebacondarwin petebacondarwin changed the title [wrangler] Make temp file directory configurable for Yarn PnP compatibility [wrangler] Smart cache directory detection for Yarn PnP compatibility Feb 16, 2026
@petebacondarwin petebacondarwin marked this pull request as ready for review February 16, 2026 14:04
@petebacondarwin petebacondarwin requested a review from a team as a code owner February 16, 2026 14:04
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: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

Add WRANGLER_HOME and WRANGLER_CACHE_DIR environment variables to allow
users to configure where Wrangler stores temporary and cache files. This
addresses compatibility issues with Yarn PnP projects that don't have
node_modules directories.

- Add WRANGLER_HOME env var to override .wrangler directory location
- Add WRANGLER_CACHE_DIR env var to override cache directory location
- Auto-detect Yarn PnP projects via .pnp.cjs/.pnp.js files
- Update Miniflare to respect these env vars for cf.json cache
- Add turbo.json passthrough for new environment variables

Fixes #4299
Remove WRANGLER_HOME env var and simplify the implementation to only use:
- WRANGLER_CACHE_DIR for wrangler's cache directory
- MINIFLARE_CACHE_DIR for miniflare's cf.json cache
- Automatic Yarn PnP detection (cached for performance)

Both wrangler and miniflare now auto-detect Yarn PnP projects and use
.wrangler/cache/ instead of node_modules/ directories.
Replace the Yarn PnP-specific detection with a more general approach:
1. Use WRANGLER_CACHE_DIR / MINIFLARE_CACHE_DIR env var if set
2. Use existing cache directory if found (node_modules/.cache/wrangler or .wrangler/cache)
3. Create cache in node_modules if it exists
4. Fall back to .wrangler/cache

This approach is more robust and works with any package manager that
doesn't use traditional node_modules directories.
Miniflare now only uses MINIFLARE_CACHE_DIR for env var override,
keeping the two packages independent while still providing the
same smart cache directory detection.
Remove module-level caching of cache folder paths in both Wrangler and
Miniflare. The caching was causing test isolation issues where the cached
path from one test would leak into subsequent tests running in different
temp directories.

The performance impact is negligible (2-3 extra filesystem lookups per
command, ~10ms total) and removing the caching:
- Fixes test isolation issues naturally
- Simplifies the code
- Removes the need for dynamic imports in tests

Also renamed config-cache-without-cache-dir.test.ts to
config-cache-wrangler-fallback.test.ts and updated tests to verify the
new .wrangler/cache fallback behavior.
@petebacondarwin petebacondarwin force-pushed the fix/4299-configurable-temp-dir branch from c021c5a to 5920dc4 Compare February 16, 2026 14:46
The .wrangler directory contains local cache and state files that should
never be deployed. This aligns Pages upload behavior with Workers Assets,
which already excludes .wrangler via .assetsignore.
@petebacondarwin petebacondarwin requested a review from a team as a code owner February 16, 2026 16:55
* 5. .wrangler/cache as final fallback
*/
function getDefaultCfPath(): string {
// Priority 1: MINIFLARE_CACHE_DIR (miniflare-specific override)
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.

Numbered comments are such an LLM tell 😀

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Feb 16, 2026
@petebacondarwin petebacondarwin merged commit caf9b11 into main Feb 16, 2026
38 checks passed
@petebacondarwin petebacondarwin deleted the fix/4299-configurable-temp-dir branch February 16, 2026 17:39
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Feb 16, 2026
@petebacondarwin
Copy link
Copy Markdown
Contributor Author

Thanks @ascorbic

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.

🚀 Feature Request: make temp file directory configurable

3 participants