Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/shared/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/reset/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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]);
Expand Down
19 changes: 13 additions & 6 deletions packages/nx/src/native/native-file-cache-location.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}