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): encapsulated nx repos should invoke wrapper when invoked via npx or global install #15337

Merged
merged 1 commit into from
Mar 1, 2023
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
4 changes: 4 additions & 0 deletions packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ if (

// this file is already in the local workspace
if (localNx === resolveNx(null)) {
if (localNx.includes('.nx') && !process.env.NX_WRAPPER_SET) {
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
require(nxWrapperPath);
}
initLocal(workspace);
} else {
// Nx is being run from globally installed CLI - hand off to the local
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"version": "15.0.12-beta.1",
"description": "Set project names in project.json files",
"implementation": "./src/migrations/update-15-1-0/set-project-names"
},
"15.8.2-update-nx-wrapper": {
"cli": "nx",
"version": "15.8.2-beta.0",
"description": "Updates the nx wrapper in encapsulated repos.",
"implementation": "./src/migrations/update-15-8-2/update-nxw"
}
}
}
13 changes: 13 additions & 0 deletions packages/nx/src/migrations/update-15-8-2/update-nxw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
getNxWrapperContents,
nxWrapperPath,
} from 'nx/src/nx-init/encapsulated/add-nx-scripts';
import { normalizePath } from '../../utils/path';
import { Tree } from '../../generators/tree';

export default async function (tree: Tree) {
const wrapperPath = normalizePath(nxWrapperPath());
if (tree.exists(wrapperPath)) {
tree.write(wrapperPath, getNxWrapperContents());
}
}
6 changes: 3 additions & 3 deletions packages/nx/src/nx-init/encapsulated/add-nx-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../generators/tree';
import { writeJson } from '../../generators/utils/json';

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

const NODE_MISSING_ERR =
Expand All @@ -36,7 +36,7 @@ export function generateEncapsulatedNxSetup(version?: string) {
const host = new FsTree(process.cwd(), false);
writeMinimalNxJson(host, version);
updateGitIgnore(host);
host.write(nxWrapperPath(), getNodeScriptContents());
host.write(nxWrapperPath(), getNxWrapperContents());
host.write('nx.bat', BATCH_SCRIPT_CONTENTS);
host.write('nx', SHELL_SCRIPT_CONTENTS, {
mode: FsConstants.S_IXUSR | FsConstants.S_IRUSR | FsConstants.S_IWUSR,
Expand Down Expand Up @@ -75,7 +75,7 @@ export function updateGitIgnore(host: Tree) {
);
}

function getNodeScriptContents() {
export function getNxWrapperContents() {
// Read nxw.js, but remove any empty comments or comments that start with `//#: `
// This removes the sourceMapUrl since it is invalid, as well as any internal comments.
return readFileSync(path.join(__dirname, 'nxw.js'), 'utf-8').replace(
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/nx-init/encapsulated/nxw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function ensureUpToDateInstallation() {
}
}

if (require.main === module) {
if (require.main === module && !process.env.NX_WRAPPER_SET) {
process.env.NX_WRAPPER_SET = 'true';
ensureUpToDateInstallation();
require('./installation/node_modules/nx/bin/nx');
}