diff --git a/docs/shared/reference/environment-variables.md b/docs/shared/reference/environment-variables.md index d1dc04c61f65e..6c17f2a10b486 100644 --- a/docs/shared/reference/environment-variables.md +++ b/docs/shared/reference/environment-variables.md @@ -30,6 +30,7 @@ The following environment variables are ones that you can set to change the beha | NX_SKIP_LOG_GROUPING | boolean | If set to `true`, Nx will not group command's logs on CI. | | NX_MIGRATE_CLI_VERSION | string | The version of Nx to use for running the `nx migrate` command. If not set, it defaults to `latest`. | | NX_LOAD_DOT_ENV_FILES | boolean | If set to 'false', Nx will not load any environment files (e.g. `.local.env`, `.env.local`) | +| NX_NATIVE_FILE_CACHE_DIRECTORY | string | The cache for native `.node` files is stored under a global temp directory by default. Set this variable to use a different directory. This is interpreted as an absolute path. | Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators. diff --git a/packages/nx/src/command-line/reset/reset.ts b/packages/nx/src/command-line/reset/reset.ts index 88787c9a42f35..12271c2ca80e6 100644 --- a/packages/nx/src/command-line/reset/reset.ts +++ b/packages/nx/src/command-line/reset/reset.ts @@ -5,7 +5,7 @@ import { projectGraphCacheDirectory, } from '../../utils/cache-directory'; import { output } from '../../utils/output'; -import { nativeFileCacheLocation } from '../../native/native-file-cache-location'; +import { getNativeFileCacheLocation } from '../../native/native-file-cache-location'; export async function resetHandler() { output.note({ @@ -15,7 +15,7 @@ export async function resetHandler() { await daemonClient.stop(); output.log({ title: 'Daemon Server - Stopped' }); try { - rmSync(nativeFileCacheLocation, { recursive: true, force: true }); + rmSync(getNativeFileCacheLocation(), { 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 } diff --git a/packages/nx/src/native/index.js b/packages/nx/src/native/index.js index 74ba32ed3e320..fd6c550f35ae0 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 { nativeFileCacheLocation } = require('./native-file-cache-location'); +const { getNativeFileCacheLocation } = require('./native-file-cache-location'); const nxPackages = new Set([ '@nx/nx-android-arm64', @@ -54,6 +54,7 @@ Module._load = function (request, parent, isMain) { const fileName = basename(nativeLocation); // we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded + const nativeFileCacheLocation = getNativeFileCacheLocation(); const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName); if (existsSync(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 index 13f1e532e0bac..42c2bdcc23d15 100644 --- a/packages/nx/src/native/native-file-cache-location.ts +++ b/packages/nx/src/native/native-file-cache-location.ts @@ -1,10 +1,17 @@ -import { tmpdir } from 'os'; +import { tmpdir, userInfo } 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') -); +export function getNativeFileCacheLocation() { + if (process.env.NX_NATIVE_FILE_CACHE_DIRECTORY) { + return process.env.NX_NATIVE_FILE_CACHE_DIRECTORY; + } else { + const shortHash = createHash('sha256') + .update(userInfo().username) + .update(workspaceRoot) + .digest('hex') + .substring(0, 7); + return join(tmpdir(), `nx-native-file-cache-${shortHash}`); + } +}