Skip to content

fix(core): skip writing deps cache if already up-to-date#34582

Merged
FrozenPandaz merged 1 commit intomasterfrom
fix/write-cache-mtime-guard
Mar 3, 2026
Merged

fix(core): skip writing deps cache if already up-to-date#34582
FrozenPandaz merged 1 commit intomasterfrom
fix/write-cache-mtime-guard

Conversation

@AgentEnder
Copy link
Member

Current Behavior

Frequent write cache calls during tasks hashing results in delays

Expected Behavior

Cache is validated but only written when changed

AI Summary

This pull request introduces an optimization to the project graph cache writing logic, reducing unnecessary disk writes when serving repeated requests with unchanged graphs. The main change is the addition of a mechanism to track the cache file's modification time and only write to disk if the file has been externally modified or not written yet by the current process.

Optimizations to cache writing:

  • Added writeCacheIfStale function in nx-deps-cache.ts to prevent redundant cache writes by checking the cache file's modification time before writing. This function is now used in the daemon's graph recomputation logic, replacing the previous unconditional write. [1] [2] [3]
  • Introduced lastWrittenCacheMtimeMs variable to track the last successful write's modification time, updated after each cache write. [1] [2]

Codebase updates:

  • Updated imports in nx-deps-cache.ts to include statSync for file modification time checks.

Related Issue(s)

Fixes #

@AgentEnder AgentEnder requested a review from a team as a code owner February 24, 2026 19:59
@netlify
Copy link

netlify bot commented Feb 24, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 300a826
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69a15311e42ab90008ad32da
😎 Deploy Preview https://deploy-preview-34582--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Contributor

nx-cloud bot commented Feb 24, 2026

View your CI Pipeline Execution ↗ for commit 300a826

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 59m 22s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3m 20s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 8s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 2s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-02-27 09:21:18 UTC

@netlify
Copy link

netlify bot commented Feb 24, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 300a826
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69a15311527ff80009a1402f
😎 Deploy Preview https://deploy-preview-34582--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment on lines +294 to +311
export function writeCacheIfStale(
cache: FileMapCache,
projectGraph: ProjectGraph,
sourceMaps: ConfigurationSourceMaps,
errors: ProjectGraphErrorTypes[]
): void {
if (lastWrittenCacheMtimeMs !== undefined) {
try {
const currentMtimeMs = statSync(nxProjectGraph).mtimeMs;
if (currentMtimeMs === lastWrittenCacheMtimeMs) {
return;
}
} catch {
// File doesn't exist or can't be stat'd — proceed with write
}
}
writeCache(cache, projectGraph, sourceMaps, errors);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Critical logic flaw: writeCacheIfStale will skip writing when the in-memory graph has changed but the file hasn't been modified externally.

Scenario:

  1. Process computes Graph A, writes it, sets lastWrittenCacheMtimeMs = T1
  2. Graph changes due to file modifications, daemon recomputes to Graph B
  3. writeCacheIfStale is called with new Graph B data
  4. Checks file mtime (still T1 since this process last wrote it)
  5. currentMtimeMs === lastWrittenCacheMtimeMs → returns without writing
  6. Graph B is never written to disk, cache is stale

The function assumes unchanged file = unchanged data, but the in-memory graph may have been recomputed with new data that needs persisting.

Fix: Compare a hash/signature of the graph data itself, not just the file timestamp:

export function writeCacheIfStale(...) {
  // Need to track what data was last written and compare
  // against current data, not just check file mtime
  const currentDataHash = hashGraphData(cache, projectGraph, sourceMaps, errors);
  if (lastWrittenDataHash === currentDataHash) {
    return; // Same data, skip write
  }
  writeCache(cache, projectGraph, sourceMaps, errors);
  lastWrittenDataHash = currentDataHash;
}
Suggested change
export function writeCacheIfStale(
cache: FileMapCache,
projectGraph: ProjectGraph,
sourceMaps: ConfigurationSourceMaps,
errors: ProjectGraphErrorTypes[]
): void {
if (lastWrittenCacheMtimeMs !== undefined) {
try {
const currentMtimeMs = statSync(nxProjectGraph).mtimeMs;
if (currentMtimeMs === lastWrittenCacheMtimeMs) {
return;
}
} catch {
// File doesn't exist or can't be stat'd — proceed with write
}
}
writeCache(cache, projectGraph, sourceMaps, errors);
}
export function writeCacheIfStale(
cache: FileMapCache,
projectGraph: ProjectGraph,
sourceMaps: ConfigurationSourceMaps,
errors: ProjectGraphErrorTypes[]
): void {
const currentDataHash = hashGraphData(cache, projectGraph, sourceMaps, errors);
if (lastWrittenDataHash === currentDataHash) {
return; // Same data, skip write
}
writeCache(cache, projectGraph, sourceMaps, errors);
lastWrittenDataHash = currentDataHash;
}

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@AgentEnder AgentEnder force-pushed the fix/write-cache-mtime-guard branch from 8af63d5 to 274f22e Compare February 24, 2026 20:04
@AgentEnder AgentEnder force-pushed the fix/write-cache-mtime-guard branch from 274f22e to 300a826 Compare February 27, 2026 08:17
@FrozenPandaz FrozenPandaz merged commit e4f2d39 into master Mar 3, 2026
23 checks passed
@FrozenPandaz FrozenPandaz deleted the fix/write-cache-mtime-guard branch March 3, 2026 21:08
FrozenPandaz pushed a commit that referenced this pull request Mar 4, 2026
## Current Behavior
Frequent write cache calls during tasks hashing results in delays

## Expected Behavior
Cache is validated but only written when changed

## AI Summary
This pull request introduces an optimization to the project graph cache
writing logic, reducing unnecessary disk writes when serving repeated
requests with unchanged graphs. The main change is the addition of a
mechanism to track the cache file's modification time and only write to
disk if the file has been externally modified or not written yet by the
current process.

Optimizations to cache writing:

* Added `writeCacheIfStale` function in `nx-deps-cache.ts` to prevent
redundant cache writes by checking the cache file's modification time
before writing. This function is now used in the daemon's graph
recomputation logic, replacing the previous unconditional write.
[[1]](diffhunk://#diff-82bd1a5a7b7320ffc3233470f191782c054bf69a696dc16001d2c4b1d0b04963R285-R312)
[[2]](diffhunk://#diff-d5bf3c66e62cac1884a071bf07fd1991320a3e62b07bfc03af3b9557b714c892L17-R17)
[[3]](diffhunk://#diff-d5bf3c66e62cac1884a071bf07fd1991320a3e62b07bfc03af3b9557b714c892L136-R149)
* Introduced `lastWrittenCacheMtimeMs` variable to track the last
successful write's modification time, updated after each cache write.
[[1]](diffhunk://#diff-82bd1a5a7b7320ffc3233470f191782c054bf69a696dc16001d2c4b1d0b04963R202-R208)
[[2]](diffhunk://#diff-82bd1a5a7b7320ffc3233470f191782c054bf69a696dc16001d2c4b1d0b04963R256-R261)

Codebase updates:

* Updated imports in `nx-deps-cache.ts` to include `statSync` for file
modification time checks.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit e4f2d39)
@github-actions
Copy link
Contributor

github-actions bot commented Mar 9, 2026

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants