Skip to content

Commit 10f97b9

Browse files
authored
fix(core): use current user when hashing native file & enable setting its directory via env (#24326)
1 parent 61e4ab2 commit 10f97b9

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

docs/shared/reference/environment-variables.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following environment variables are ones that you can set to change the beha
3030
| NX_SKIP_LOG_GROUPING | boolean | If set to `true`, Nx will not group command's logs on CI. |
3131
| NX_MIGRATE_CLI_VERSION | string | The version of Nx to use for running the `nx migrate` command. If not set, it defaults to `latest`. |
3232
| NX_LOAD_DOT_ENV_FILES | boolean | If set to 'false', Nx will not load any environment files (e.g. `.local.env`, `.env.local`) |
33+
| 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. |
3334

3435
Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators.
3536

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +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';
8+
import { getNativeFileCacheLocation } from '../../native/native-file-cache-location';
99

1010
export async function resetHandler() {
1111
output.note({
@@ -15,7 +15,7 @@ export async function resetHandler() {
1515
await daemonClient.stop();
1616
output.log({ title: 'Daemon Server - Stopped' });
1717
try {
18-
rmSync(nativeFileCacheLocation, { recursive: true, force: true });
18+
rmSync(getNativeFileCacheLocation(), { recursive: true, force: true });
1919
} catch (e) {
2020
// ignore, deleting the native file cache is not critical and can fail if another process is locking the file
2121
}

packages/nx/src/native/index.js

+2-1
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 { nativeFileCacheLocation } = require('./native-file-cache-location');
5+
const { getNativeFileCacheLocation } = require('./native-file-cache-location');
66

77
const nxPackages = new Set([
88
'@nx/nx-android-arm64',
@@ -54,6 +54,7 @@ Module._load = function (request, parent, isMain) {
5454
const fileName = basename(nativeLocation);
5555

5656
// we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded
57+
const nativeFileCacheLocation = getNativeFileCacheLocation();
5758
const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName);
5859
if (existsSync(tmpFile)) {
5960
return originalLoad.apply(this, [tmpFile, parent, isMain]);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { tmpdir } from 'os';
1+
import { tmpdir, userInfo } from 'os';
22
import { join } from 'path';
33
import { createHash } from 'crypto';
44
import { workspaceRoot } from '../utils/workspace-root';
55

6-
export const nativeFileCacheLocation = join(
7-
tmpdir(),
8-
'nx-native-file-cache',
9-
createHash('sha256').update(workspaceRoot).digest('hex')
10-
);
6+
export function getNativeFileCacheLocation() {
7+
if (process.env.NX_NATIVE_FILE_CACHE_DIRECTORY) {
8+
return process.env.NX_NATIVE_FILE_CACHE_DIRECTORY;
9+
} else {
10+
const shortHash = createHash('sha256')
11+
.update(userInfo().username)
12+
.update(workspaceRoot)
13+
.digest('hex')
14+
.substring(0, 7);
15+
return join(tmpdir(), `nx-native-file-cache-${shortHash}`);
16+
}
17+
}

0 commit comments

Comments
 (0)