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): copy native files to tmp file location instead of .nx/cache #23375

Merged
merged 1 commit into from
May 15, 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
6 changes: 6 additions & 0 deletions packages/nx/src/command-line/reset/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 });
Expand Down
11 changes: 6 additions & 5 deletions 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 { cacheDir } = require('../utils/cache-directory');
const { nativeFileCacheLocation } = require('./native-file-cache-location');

const nxPackages = new Set([
'@nx/nx-android-arm64',
Expand Down Expand Up @@ -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]);
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/native/native-file-cache-location.ts
Original file line number Diff line number Diff line change
@@ -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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this nesting at all? Can't it be join(tmpdir(), 'nx/bin')?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to keep the same behaviour as before where the files are workspace-scoped. I'm not sure if there could be funky side-effects when one repo calls nx reset and deletes the .node file that is currently used by another process in another workspace.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sounds good.

);