Skip to content

Commit

Permalink
fix(core): atomic writes of nxdeps.json (#12920)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhamm authored Nov 17, 2022
1 parent 1d848dd commit 7467e71
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions packages/nx/src/project-graph/nx-deps-cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { existsSync } from 'fs';
import { ensureDirSync } from 'fs-extra';
import { ensureDirSync, renameSync } from 'fs-extra';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { projectGraphCacheDirectory } from '../utils/cache-directory';
import { directoryExists, fileExists } from '../utils/fileutils';
import { NxJsonConfiguration } from '../config/nx-json';
import {
FileData,
ProjectFileMap,
Expand All @@ -12,9 +11,14 @@ import {
ProjectGraphExternalNode,
ProjectGraphNode,
} from '../config/project-graph';
import { readJsonFile, writeJsonFile } from '../utils/fileutils';
import { NxJsonConfiguration } from '../config/nx-json';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import { projectGraphCacheDirectory } from '../utils/cache-directory';
import {
directoryExists,
fileExists,
readJsonFile,
writeJsonFile,
} from '../utils/fileutils';

export interface ProjectGraphCache {
version: string;
Expand Down Expand Up @@ -106,7 +110,34 @@ export function createCache(

export function writeCache(cache: ProjectGraphCache): void {
performance.mark('write cache:start');
writeJsonFile(nxDepsPath, cache);
let retry = 1;
let done = false;
do {
// write first to a unique temporary filename and then do a
// rename of the file to the correct filename
// this is to avoid any problems with half-written files
// in case of crash and/or partially written files due
// to multiple parallel processes reading and writing this file
const unique = (Math.random().toString(16) + '0000000').slice(2, 10);
const tmpDepsPath = `${nxDepsPath}~${unique}`;

try {
writeJsonFile(tmpDepsPath, cache);
renameSync(tmpDepsPath, nxDepsPath);
done = true;
} catch (err: any) {
if (err instanceof Error) {
console.log(
`ERROR (${retry}) when writing \n${err.message}\n${err.stack}`
);
} else {
console.log(
`ERROR (${retry}) unknonw error when writing ${nxDepsPath}`
);
}
++retry;
}
} while (!done && retry < 5);
performance.mark('write cache:end');
performance.measure('write cache', 'write cache:start', 'write cache:end');
}
Expand Down

1 comment on commit 7467e71

@vercel
Copy link

@vercel vercel bot commented on 7467e71 Nov 17, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.