fix(core): use current user when hashing native file & enable setting its directory via env (#24326)

This commit is contained in:
MaxKless 2024-05-24 15:10:51 +02:00 committed by GitHub
parent 61e4ab2eef
commit 10f97b99bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 9 deletions

View File

@ -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_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_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_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. Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators.

View File

@ -5,7 +5,7 @@ import {
projectGraphCacheDirectory, projectGraphCacheDirectory,
} from '../../utils/cache-directory'; } from '../../utils/cache-directory';
import { output } from '../../utils/output'; 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() { export async function resetHandler() {
output.note({ output.note({
@ -15,7 +15,7 @@ export async function resetHandler() {
await daemonClient.stop(); await daemonClient.stop();
output.log({ title: 'Daemon Server - Stopped' }); output.log({ title: 'Daemon Server - Stopped' });
try { try {
rmSync(nativeFileCacheLocation, { recursive: true, force: true }); rmSync(getNativeFileCacheLocation(), { recursive: true, force: true });
} catch (e) { } catch (e) {
// ignore, deleting the native file cache is not critical and can fail if another process is locking the file // ignore, deleting the native file cache is not critical and can fail if another process is locking the file
} }

View File

@ -2,7 +2,7 @@ const { join, basename } = require('path');
const { copyFileSync, existsSync, mkdirSync } = require('fs'); const { copyFileSync, existsSync, mkdirSync } = require('fs');
const Module = require('module'); const Module = require('module');
const { nxVersion } = require('../utils/versions'); const { nxVersion } = require('../utils/versions');
const { nativeFileCacheLocation } = require('./native-file-cache-location'); const { getNativeFileCacheLocation } = require('./native-file-cache-location');
const nxPackages = new Set([ const nxPackages = new Set([
'@nx/nx-android-arm64', '@nx/nx-android-arm64',
@ -54,6 +54,7 @@ Module._load = function (request, parent, isMain) {
const fileName = basename(nativeLocation); const fileName = basename(nativeLocation);
// we copy the file to a workspace-scoped tmp directory and prefix with nxVersion to avoid stale files being loaded // 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); const tmpFile = join(nativeFileCacheLocation, nxVersion + '-' + fileName);
if (existsSync(tmpFile)) { if (existsSync(tmpFile)) {
return originalLoad.apply(this, [tmpFile, parent, isMain]); return originalLoad.apply(this, [tmpFile, parent, isMain]);

View File

@ -1,10 +1,17 @@
import { tmpdir } from 'os'; import { tmpdir, userInfo } from 'os';
import { join } from 'path'; import { join } from 'path';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import { workspaceRoot } from '../utils/workspace-root'; import { workspaceRoot } from '../utils/workspace-root';
export const nativeFileCacheLocation = join( export function getNativeFileCacheLocation() {
tmpdir(), if (process.env.NX_NATIVE_FILE_CACHE_DIRECTORY) {
'nx-native-file-cache', return process.env.NX_NATIVE_FILE_CACHE_DIRECTORY;
createHash('sha256').update(workspaceRoot).digest('hex') } else {
); const shortHash = createHash('sha256')
.update(userInfo().username)
.update(workspaceRoot)
.digest('hex')
.substring(0, 7);
return join(tmpdir(), `nx-native-file-cache-${shortHash}`);
}
}