From 887bad585996b3ea253e1936cd13fc2f0e2bae3a Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 5 Feb 2025 12:48:31 -0500 Subject: [PATCH] fix(core): do not write filemap cache when there are errors (#29892) ## Current Behavior Errors when processing the project graph still result in the project file map being cached causing invalid information to be used when recalculating the project graph. ## Expected Behavior The project file map will not be cached if there are errors calculating the project graph. The dependencies of the graph will be recalculated from scratch the next time Nx runs. ## Related Issue(s) Fixes # (cherry picked from commit 35486caa87beb1b98f9aeaebc32095958625f06d) --- packages/nx/src/project-graph/nx-deps-cache.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/nx/src/project-graph/nx-deps-cache.ts b/packages/nx/src/project-graph/nx-deps-cache.ts index eb0be075aa25b3..aa43af595271f2 100644 --- a/packages/nx/src/project-graph/nx-deps-cache.ts +++ b/packages/nx/src/project-graph/nx-deps-cache.ts @@ -223,11 +223,16 @@ export function writeCache( }); renameSync(tmpProjectGraphPath, nxProjectGraph); - writeJsonFile(tmpFileMapPath, cache); - renameSync(tmpFileMapPath, nxFileMap); - writeJsonFile(tmpSourceMapPath, sourceMaps); renameSync(tmpSourceMapPath, nxSourceMaps); + + // only write the file map if there are no errors + // if there were errors, the errors make the filemap invalid + // TODO: We should be able to keep the valid part of the filemap if the errors being thrown told us which parts of the filemap were invalid + if (errors.length === 0) { + writeJsonFile(tmpFileMapPath, cache); + renameSync(tmpFileMapPath, nxFileMap); + } done = true; } catch (err: any) { if (err instanceof Error) {