Skip to content

Commit 900bb27

Browse files
authored
fix(core): repair nx mcp in pnpm/yarn (#32452)
This pull request updates the `mcp.ts` command handler to improve compatibility with multiple package managers and enhance argument handling for command execution. The changes ensure that the correct flags are used for each package manager and that commands with spaces are executed properly. * Adjusted arguments passed to the `dlx` command for `npm`, `yarn`, and `bun` to use their respective flags (`-y`, `--quiet`, `--silent`), ensuring consistent behavior across different package managers. * Improved handling of `dlx` commands that may contain spaces (e.g., `pnpm dlx`) by splitting the command and passing arguments appropriately to `spawnSync`. * Updated both `mcpHandler` and `showHelp` functions to use the new argument handling logic for executing commands and displaying help output.
1 parent ed723b0 commit 900bb27

File tree

1 file changed

+50
-20
lines changed
  • packages/nx/src/command-line/mcp

1 file changed

+50
-20
lines changed
Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
11
import { spawnSync } from 'child_process';
2-
import { getPackageManagerCommand } from '../../utils/package-manager';
2+
import {
3+
detectPackageManager,
4+
getPackageManagerCommand,
5+
} from '../../utils/package-manager';
36
import { workspaceRoot } from '../../utils/workspace-root';
47

58
export async function mcpHandler(args: any) {
6-
const packageManagerCommands = getPackageManagerCommand();
9+
const packageManager = detectPackageManager();
10+
const packageManagerCommands = getPackageManagerCommand(packageManager);
711

812
const passthroughArgs =
913
args['_'][0] === 'mcp' ? args['_'].slice(1) : args['_'];
10-
spawnSync(
11-
packageManagerCommands.dlx,
12-
['-y', 'nx-mcp@latest', ...passthroughArgs],
13-
{
14-
stdio: 'inherit',
15-
cwd: workspaceRoot,
16-
}
17-
);
14+
15+
let dlxArgs: string[];
16+
17+
if (packageManager === 'npm') {
18+
dlxArgs = ['-y', 'nx-mcp@latest', ...passthroughArgs];
19+
} else if (packageManager === 'yarn') {
20+
dlxArgs = ['--quiet', 'nx-mcp@latest', ...passthroughArgs];
21+
} else if (packageManager === 'bun') {
22+
dlxArgs = ['--silent', 'nx-mcp@latest', ...passthroughArgs];
23+
} else {
24+
dlxArgs = ['nx-mcp@latest', ...passthroughArgs];
25+
}
26+
27+
// For commands that might contain spaces like "pnpm dlx"
28+
const dlxCommand = packageManagerCommands.dlx.split(' ');
29+
const executable = dlxCommand[0];
30+
const execArgs = [...dlxCommand.slice(1), ...dlxArgs];
31+
32+
spawnSync(executable, execArgs, {
33+
stdio: 'inherit',
34+
cwd: workspaceRoot,
35+
});
1836
}
1937

2038
export async function showHelp() {
21-
const packageManagerCommands = getPackageManagerCommand();
22-
23-
const helpOutput = spawnSync(
24-
packageManagerCommands.dlx,
25-
['-y', 'nx-mcp@latest', '--help'],
26-
{
27-
cwd: workspaceRoot,
28-
encoding: 'utf-8',
29-
}
30-
);
39+
const packageManager = detectPackageManager();
40+
const packageManagerCommands = getPackageManagerCommand(packageManager);
41+
42+
let dlxArgs: string[];
43+
if (packageManager === 'npm') {
44+
dlxArgs = ['-y', 'nx-mcp@latest', '--help'];
45+
} else if (packageManager === 'yarn') {
46+
dlxArgs = ['--quiet', 'nx-mcp@latest', '--help'];
47+
} else if (packageManager === 'bun') {
48+
dlxArgs = ['--silent', 'nx-mcp@latest', '--help'];
49+
} else {
50+
dlxArgs = ['nx-mcp@latest', '--help'];
51+
}
52+
53+
const dlxCommand = packageManagerCommands.dlx.split(' ');
54+
const executable = dlxCommand[0];
55+
const execArgs = [...dlxCommand.slice(1), ...dlxArgs];
56+
57+
const helpOutput = spawnSync(executable, execArgs, {
58+
cwd: workspaceRoot,
59+
encoding: 'utf-8',
60+
});
3161

3262
console.log(helpOutput.stdout?.toString().replaceAll('nx-mcp', 'nx mcp'));
3363
}

0 commit comments

Comments
 (0)