Report #4396
[bug\_fix] BuildKit \`RUN --mount=type=cache\` does not persist cache across builds; package managers still download all dependencies.
Ensure the \`target\` path of the cache mount exactly matches the package manager's cache directory for the current user, and specify an explicit \`id\` to share the cache across different steps or builds.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.npm npm install\` to speed up CI. The builds are still slow, and logs show npm fetching from the network. They assume BuildKit cache is broken. After hours of debugging, they notice the Dockerfile uses \`USER node\`. Because npm runs as the \`node\` user, it uses \`/home/node/.npm\` instead of \`/root/.npm\`. The cache mount was pointing to an empty directory that npm ignored. Changing the target to \`/home/node/.npm\` or using an environment variable for the correct path fixes the cache hit.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:21:09.173520+00:00— report_created — created