Skip to content

Commit c9244d2

Browse files
authoredMar 1, 2023
fix(core): encapsulated nx repos should invoke wrapper when invoked via npx or global install (#15337)
1 parent 2791444 commit c9244d2

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed
 

‎packages/nx/bin/nx.ts

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ if (
6262

6363
// this file is already in the local workspace
6464
if (localNx === resolveNx(null)) {
65+
if (localNx.includes('.nx') && !process.env.NX_WRAPPER_SET) {
66+
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
67+
require(nxWrapperPath);
68+
}
6569
initLocal(workspace);
6670
} else {
6771
// Nx is being run from globally installed CLI - hand off to the local

‎packages/nx/migrations.json

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
"version": "15.0.12-beta.1",
4848
"description": "Set project names in project.json files",
4949
"implementation": "./src/migrations/update-15-1-0/set-project-names"
50+
},
51+
"15.8.2-update-nx-wrapper": {
52+
"cli": "nx",
53+
"version": "15.8.2-beta.0",
54+
"description": "Updates the nx wrapper in encapsulated repos.",
55+
"implementation": "./src/migrations/update-15-8-2/update-nxw"
5056
}
5157
}
5258
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {
2+
getNxWrapperContents,
3+
nxWrapperPath,
4+
} from 'nx/src/nx-init/encapsulated/add-nx-scripts';
5+
import { normalizePath } from '../../utils/path';
6+
import { Tree } from '../../generators/tree';
7+
8+
export default async function (tree: Tree) {
9+
const wrapperPath = normalizePath(nxWrapperPath());
10+
if (tree.exists(wrapperPath)) {
11+
tree.write(wrapperPath, getNxWrapperContents());
12+
}
13+
}

‎packages/nx/src/nx-init/encapsulated/add-nx-scripts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '../../generators/tree';
1212
import { writeJson } from '../../generators/utils/json';
1313

14-
const nxWrapperPath = (p: typeof import('path') = path) =>
14+
export const nxWrapperPath = (p: typeof import('path') = path) =>
1515
p.join('.nx', 'nxw.js');
1616

1717
const NODE_MISSING_ERR =
@@ -36,7 +36,7 @@ export function generateEncapsulatedNxSetup(version?: string) {
3636
const host = new FsTree(process.cwd(), false);
3737
writeMinimalNxJson(host, version);
3838
updateGitIgnore(host);
39-
host.write(nxWrapperPath(), getNodeScriptContents());
39+
host.write(nxWrapperPath(), getNxWrapperContents());
4040
host.write('nx.bat', BATCH_SCRIPT_CONTENTS);
4141
host.write('nx', SHELL_SCRIPT_CONTENTS, {
4242
mode: FsConstants.S_IXUSR | FsConstants.S_IRUSR | FsConstants.S_IWUSR,
@@ -75,7 +75,7 @@ export function updateGitIgnore(host: Tree) {
7575
);
7676
}
7777

78-
function getNodeScriptContents() {
78+
export function getNxWrapperContents() {
7979
// Read nxw.js, but remove any empty comments or comments that start with `//#: `
8080
// This removes the sourceMapUrl since it is invalid, as well as any internal comments.
8181
return readFileSync(path.join(__dirname, 'nxw.js'), 'utf-8').replace(

‎packages/nx/src/nx-init/encapsulated/nxw.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ function ensureUpToDateInstallation() {
9999
}
100100
}
101101

102-
if (require.main === module) {
102+
if (require.main === module && !process.env.NX_WRAPPER_SET) {
103+
process.env.NX_WRAPPER_SET = 'true';
103104
ensureUpToDateInstallation();
104105
require('./installation/node_modules/nx/bin/nx');
105106
}

1 commit comments

Comments
 (1)

vercel[bot] commented on Mar 1, 2023

@vercel[bot]

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.