Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7cb949d

Browse files
jeffbcrossvsavkin
authored andcommittedFeb 26, 2020
fix(core): change mkdir logic to avoid race condition
The process of checking for directory existence then creating A directory was causing race conditions when running scripts In parallel. I inverted the logic to always try to Create the directory without checking 1st, then handle any errors.
1 parent 8e83fbb commit 7cb949d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed
 

‎packages/workspace/src/core/project-graph/project-graph.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,26 @@ interface ProjectGraphCache {
9292
fileMap: FileMap;
9393
}
9494

95-
const nxDepsPath = `${appRootPath}/dist/nxdeps.json`;
95+
const distPath = `${appRootPath}/dist`;
96+
const nxDepsPath = `${distPath}/nxdeps.json`;
9697

9798
function readCache(): false | { data: ProjectGraphCache; mtime: number } {
98-
if (!directoryExists(`${appRootPath}/dist`)) {
99-
mkdirSync(`${appRootPath}/dist`);
99+
try {
100+
mkdirSync(distPath);
101+
} catch (e) {
102+
/*
103+
* @jeffbcross: Node JS docs recommend against checking for existence of directory immediately before creating it.
104+
* Instead, just try to create the directory and handle the error.
105+
*
106+
* We ran into race conditions when running scripts concurrently, where multiple scripts were
107+
* arriving here simultaneously, checking for directory existence, then trying to create the directory simultaneously.
108+
*
109+
* In this case, we're creating the directory. If the operation failed, we ensure that the directory
110+
* exists before continuing (or raise an exception).
111+
*/
112+
if (!directoryExists(distPath)) {
113+
throw new Error(`Failed to create directory: ${distPath}`);
114+
}
100115
}
101116

102117
const data = getValidCache(

0 commit comments

Comments
 (0)
Please sign in to comment.