Skip to content

Commit 7ff68e5

Browse files
MaxKlessFrozenPandaz
authored andcommitted
fix(core): copy native files to tmp file location instead of .nx/cache (#23375)
## Current Behavior Currently, the `.node` files required to load native code are saved in `.nx/cache`. This can cause different issues: - users on windows sometimes experience errors during `nx reset` because it's trying to delete the entire folder and some process is still locking the file - `@angular-eslint` users are seeing the `.nx/cache` folder in their workspace since it uses `@nx/devkit` ## Expected Behavior We want no errors and for noone to be bothered by the `.node` file. This is why we move the `.node` file to a tmp location outside the workspace instead of `.nx/cache`. We still make sure to delete it during `nx reset` but throw no errors if that fails. It will simply be deleted by the next invocation of `nx reset`. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #23224 (cherry picked from commit 5ce5337)
1 parent 7da337e commit 7ff68e5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

packages/nx/src/command-line/reset/reset.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
projectGraphCacheDirectory,
66
} from '../../utils/cache-directory';
77
import { output } from '../../utils/output';
8+
import { nativeFileCacheLocation } from '../../native/native-file-cache-location';
89

910
export async function resetHandler() {
1011
output.note({
@@ -13,6 +14,11 @@ export async function resetHandler() {
1314
});
1415
await daemonClient.stop();
1516
output.log({ title: 'Daemon Server - Stopped' });
17+
try {
18+
rmSync(nativeFileCacheLocation, { recursive: true, force: true });
19+
} catch (e) {
20+
// ignore, deleting the native file cache is not critical and can fail if another process is locking the file
21+
}
1622
rmSync(cacheDir, { recursive: true, force: true });
1723
if (projectGraphCacheDirectory !== cacheDir) {
1824
rmSync(projectGraphCacheDirectory, { recursive: true, force: true });

packages/nx/src/native/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { join, basename } = require('path');
22
const { copyFileSync, existsSync, mkdirSync } = require('fs');
33
const Module = require('module');
44
const { nxVersion } = require('../utils/versions');
5-
const { cacheDir } = require('../utils/cache-directory');
5+
const { nativeFileCacheLocation } = require('./native-file-cache-location');
66

77
const nxPackages = new Set([
88
'@nx/nx-android-arm64',
@@ -52,13 +52,14 @@ Module._load = function (request, parent, isMain) {
5252
) {
5353
const nativeLocation = require.resolve(modulePath);
5454
const fileName = basename(nativeLocation);
55-
// we copy the file to the cache directory (.nx/cache by default) and prefix with nxVersion to avoid stale files being loaded
56-
const tmpFile = join(cacheDir, nxVersion + '-' + fileName);
55+
56+
// we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded
57+
const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName);
5758
if (existsSync(tmpFile)) {
5859
return originalLoad.apply(this, [tmpFile, parent, isMain]);
5960
}
60-
if (!existsSync(cacheDir)) {
61-
mkdirSync(cacheDir, { recursive: true });
61+
if (!existsSync(nativeFileCacheLocation)) {
62+
mkdirSync(nativeFileCacheLocation, { recursive: true });
6263
}
6364
copyFileSync(nativeLocation, tmpFile);
6465
return originalLoad.apply(this, [tmpFile, parent, isMain]);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { tmpdir } from 'os';
2+
import { join } from 'path';
3+
import { createHash } from 'crypto';
4+
import { workspaceRoot } from '../utils/workspace-root';
5+
6+
export const nativeFileCacheLocation = join(
7+
tmpdir(),
8+
'nx-native-file-cache',
9+
createHash('sha256').update(workspaceRoot).digest('hex')
10+
);

0 commit comments

Comments
 (0)