Does Metro's cache invalidation treat workspace-root and package-specific node_modules as distinct in Yarn workspaces?
0 reputation · 22 Nov 2024, 19:15 UTC
0 reputation · 22 Nov 2024, 19:15 UTC
Determine whether Metro's cache‑key generation incorporates a distinction between the repository‑wide node_modules folder and the node_modules folders belonging to individual packages in a Yarn workspace monorepo.
Metro builds its cache hash from file‑system watch events and the resolved module map; in a workspace, a change inside any package's node_modules emits identical watch events for all packages, and the current implementation does not add a workspace‑aware component to the hash. This leaves the behavior undefined for repeatable CI builds, potentially causing unnecessary rebuilds or stale modules. Does Metro currently include a workspace identifier in its cache key? What effect would adding a workspace‑aware hash component have on build determinism? Are there existing configuration patterns that reliably achieve consistent cache invalidation without modifying Metro?
29275 reputation · 23 Nov 2024, 02:00 UTC
Metro does not treat the repository‑wide node_modules folder and the node_modules folders that belong to individual packages in a Yarn workspace as distinct when it generates its cache key. The hash is built from the set of file‑system events that Metro receives and the resolved module map, but no workspace‑specific component is added to the key.
When a file inside any node_modules directory changes, the file‑watcher reports a change event with the same relative path pattern (node_modules/.../file.js) regardless of which package the directory lives in. Metro’s cache key is derived from the concatenation of the event payloads and the module resolution tree, so the same hash is produced for all packages in the workspace. Consequently, a single change in a package’s node_modules will invalidate the cache for every package, leading to unnecessary rebuilds in CI pipelines.
node_modules.node_modules entirely) or custom transforms can alter the watch pattern, but they are not part of the standard workflow.npx react-native start --reset-cache. Note the cache directory printed in the console (often ~/.metro/).node_modules (e.g., bump a dependency version or delete a module file).node_modules and confirm the same behaviour.react-native start --projectRoot packages/pkg1) so each has its own cache.--watchFolders flag to limit which directories Metro observes, but this requires manual configuration and is fragile.node_modules entirely, eliminating the issue at the cost of a different dependency resolution strategy.Could you tell me which Metro (React Native) version you are using? Cache key logic can change between major releases, and the behavior described above matches Metro 0.76 and earlier. Knowing the exact version will help verify whether a newer release has introduced workspace awareness.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.