diff --git a/packages/nx/src/command-line/reset/reset.ts b/packages/nx/src/command-line/reset/reset.ts index 8c260286f41a4..88787c9a42f35 100644 --- a/packages/nx/src/command-line/reset/reset.ts +++ b/packages/nx/src/command-line/reset/reset.ts @@ -5,6 +5,7 @@ import { projectGraphCacheDirectory, } from '../../utils/cache-directory'; import { output } from '../../utils/output'; +import { nativeFileCacheLocation } from '../../native/native-file-cache-location'; export async function resetHandler() { output.note({ @@ -13,6 +14,11 @@ export async function resetHandler() { }); await daemonClient.stop(); output.log({ title: 'Daemon Server - Stopped' }); + try { + rmSync(nativeFileCacheLocation, { recursive: true, force: true }); + } catch (e) { + // ignore, deleting the native file cache is not critical and can fail if another process is locking the file + } rmSync(cacheDir, { recursive: true, force: true }); if (projectGraphCacheDirectory !== cacheDir) { rmSync(projectGraphCacheDirectory, { recursive: true, force: true }); diff --git a/packages/nx/src/native/index.js b/packages/nx/src/native/index.js index eef8087714bfd..74ba32ed3e320 100644 --- a/packages/nx/src/native/index.js +++ b/packages/nx/src/native/index.js @@ -2,7 +2,7 @@ const { join, basename } = require('path'); const { copyFileSync, existsSync, mkdirSync } = require('fs'); const Module = require('module'); const { nxVersion } = require('../utils/versions'); -const { cacheDir } = require('../utils/cache-directory'); +const { nativeFileCacheLocation } = require('./native-file-cache-location'); const nxPackages = new Set([ '@nx/nx-android-arm64', @@ -52,13 +52,14 @@ Module._load = function (request, parent, isMain) { ) { const nativeLocation = require.resolve(modulePath); const fileName = basename(nativeLocation); - // we copy the file to the cache directory (.nx/cache by default) and prefix with nxVersion to avoid stale files being loaded - const tmpFile = join(cacheDir, nxVersion + '-' + fileName); + + // we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded + const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName); if (existsSync(tmpFile)) { return originalLoad.apply(this, [tmpFile, parent, isMain]); } - if (!existsSync(cacheDir)) { - mkdirSync(cacheDir, { recursive: true }); + if (!existsSync(nativeFileCacheLocation)) { + mkdirSync(nativeFileCacheLocation, { recursive: true }); } copyFileSync(nativeLocation, tmpFile); return originalLoad.apply(this, [tmpFile, parent, isMain]); diff --git a/packages/nx/src/native/native-file-cache-location.ts b/packages/nx/src/native/native-file-cache-location.ts new file mode 100644 index 0000000000000..13f1e532e0bac --- /dev/null +++ b/packages/nx/src/native/native-file-cache-location.ts @@ -0,0 +1,10 @@ +import { tmpdir } from 'os'; +import { join } from 'path'; +import { createHash } from 'crypto'; +import { workspaceRoot } from '../utils/workspace-root'; + +export const nativeFileCacheLocation = join( + tmpdir(), + 'nx-native-file-cache', + createHash('sha256').update(workspaceRoot).digest('hex') +);