fix(setup): register a stable launcher when run through bunx (#1731)
Setup run through bunx or bun x registered the bin inside bun's temporary package cache. Detect bun's package-runner launch from npm_command=exec with a bun user agent and register the pinned bunx launcher, wrapped in cmd.exe on Windows like the npx launcher. Doctor, update maintenance and uninstall recognize it as REA-owned, and setup's printed commands name the runner that launched it. Read the runner variables case-insensitively on Windows, where the CLI environment snapshot upper-cases variable names and setup run through npx also fell back to the cache path. Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -3,20 +3,71 @@ import { valid } from "semver";
|
||||
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
|
||||
/** Resolve a pinned npx launcher that stdio clients can spawn on the host. */
|
||||
export const npxRegistrationCommand = (
|
||||
/** Package runners whose pinned launcher setup can register. */
|
||||
export type PackageRunner = "npx" | "bunx";
|
||||
|
||||
/** Read one variable; Windows variable names are case-insensitive. */
|
||||
const environmentValue = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
name: string,
|
||||
platform: NodeJS.Platform,
|
||||
): string | undefined => {
|
||||
if (platform !== "win32") return environment[name];
|
||||
const key = Object.keys(environment).find(
|
||||
(candidate) => candidate.toLowerCase() === name,
|
||||
);
|
||||
return key === undefined ? undefined : environment[key];
|
||||
};
|
||||
|
||||
/** Identify the package runner that launched this process, if any. */
|
||||
export const invokingPackageRunner = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): PackageRunner | undefined => {
|
||||
if (environmentValue(environment, "npm_command", platform) !== "exec")
|
||||
return undefined;
|
||||
// `bunx` and `bun x` also report npm_command=exec, with a bun user agent.
|
||||
return environmentValue(
|
||||
environment,
|
||||
"npm_config_user_agent",
|
||||
platform,
|
||||
)?.startsWith("bun/") === true
|
||||
? "bunx"
|
||||
: "npx";
|
||||
};
|
||||
|
||||
/** Copy an environment without the variable that marks a package-runner launch. */
|
||||
export const withoutPackageRunnerIdentity = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): NodeJS.ProcessEnv =>
|
||||
Object.fromEntries(
|
||||
Object.entries(environment).filter(([key]) =>
|
||||
platform === "win32"
|
||||
? key.toLowerCase() !== "npm_command"
|
||||
: key !== "npm_command",
|
||||
),
|
||||
);
|
||||
|
||||
/** Resolve a pinned package-runner launcher that stdio clients can spawn on the host. */
|
||||
export const packageRunnerRegistrationCommand = (
|
||||
runner: PackageRunner,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): readonly string[] => {
|
||||
const command = [
|
||||
"npx",
|
||||
"-y",
|
||||
PRODUCT_IDENTITY.registrationPackageSpecifier,
|
||||
"mcp",
|
||||
];
|
||||
const command =
|
||||
runner === "npx"
|
||||
? ["npx", "-y", PRODUCT_IDENTITY.registrationPackageSpecifier, "mcp"]
|
||||
: ["bunx", PRODUCT_IDENTITY.registrationPackageSpecifier, "mcp"];
|
||||
// Both runners may be .cmd shims on Windows, which clients cannot spawn directly.
|
||||
return platform === "win32" ? ["cmd.exe", "/d", "/c", ...command] : command;
|
||||
};
|
||||
|
||||
const unwrapNpxCommand = (command: readonly string[]): readonly string[] => {
|
||||
/** Resolve a pinned npx launcher that stdio clients can spawn on the host. */
|
||||
export const npxRegistrationCommand = (
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): readonly string[] => packageRunnerRegistrationCommand("npx", platform);
|
||||
|
||||
const unwrapCmdCommand = (command: readonly string[]): readonly string[] => {
|
||||
const executable = command[0]?.toLowerCase();
|
||||
if (executable !== "cmd" && executable !== "cmd.exe") return command;
|
||||
if (command[1]?.toLowerCase() === "/c") return command.slice(2);
|
||||
@@ -25,6 +76,35 @@ const unwrapNpxCommand = (command: readonly string[]): readonly string[] => {
|
||||
return [];
|
||||
};
|
||||
|
||||
const packageRunnerReference = (
|
||||
command: readonly string[],
|
||||
): string | undefined => {
|
||||
if (
|
||||
command.length === 4 &&
|
||||
command[0] === "npx" &&
|
||||
command[1] === "-y" &&
|
||||
command[3] === "mcp"
|
||||
)
|
||||
return command[2];
|
||||
if (command.length === 3 && command[0] === "bunx" && command[2] === "mcp")
|
||||
return command[1];
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const isOwnedPackageReference = (packageReference: string): boolean => {
|
||||
if (
|
||||
packageReference === PRODUCT_IDENTITY.packageName ||
|
||||
packageReference === PRODUCT_IDENTITY.packageSpecifier ||
|
||||
packageReference === PRODUCT_IDENTITY.registrationPackageSpecifier
|
||||
)
|
||||
return true;
|
||||
const versionPrefix = `${PRODUCT_IDENTITY.packageName}@`;
|
||||
return (
|
||||
packageReference.startsWith(versionPrefix) &&
|
||||
valid(packageReference.slice(versionPrefix.length)) !== null
|
||||
);
|
||||
};
|
||||
|
||||
/** Check that one parsed command points to REA's MCP entry point. */
|
||||
export const isOwnedClientRegistrationCommand = (
|
||||
command: readonly string[],
|
||||
@@ -49,25 +129,8 @@ export const isOwnedClientRegistrationCommand = (
|
||||
return true;
|
||||
}
|
||||
|
||||
const npxCommand = unwrapNpxCommand(command);
|
||||
if (
|
||||
npxCommand.length !== 4 ||
|
||||
npxCommand[0] !== "npx" ||
|
||||
npxCommand[1] !== "-y" ||
|
||||
npxCommand[3] !== "mcp"
|
||||
)
|
||||
return false;
|
||||
|
||||
const packageReference = npxCommand[2] ?? "";
|
||||
if (
|
||||
packageReference === PRODUCT_IDENTITY.packageName ||
|
||||
packageReference === PRODUCT_IDENTITY.packageSpecifier ||
|
||||
packageReference === PRODUCT_IDENTITY.registrationPackageSpecifier
|
||||
)
|
||||
return true;
|
||||
const versionPrefix = `${PRODUCT_IDENTITY.packageName}@`;
|
||||
const packageReference = packageRunnerReference(unwrapCmdCommand(command));
|
||||
return (
|
||||
packageReference.startsWith(versionPrefix) &&
|
||||
valid(packageReference.slice(versionPrefix.length)) !== null
|
||||
packageReference !== undefined && isOwnedPackageReference(packageReference)
|
||||
);
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { MCP_STARTUP_POLICY } from "../mcpStartupPolicy.js";
|
||||
import {
|
||||
isOwnedClientRegistrationCommand,
|
||||
npxRegistrationCommand,
|
||||
packageRunnerRegistrationCommand,
|
||||
} from "./ClientRegistrationIdentity.js";
|
||||
import {
|
||||
clientEvidencePaths,
|
||||
@@ -232,10 +232,14 @@ const registrationAligned = (
|
||||
resolve(command[1] ?? "") === currentCommandPath
|
||||
)
|
||||
return true;
|
||||
const npxCommand = npxRegistrationCommand(platform);
|
||||
if (
|
||||
command.length === npxCommand.length &&
|
||||
command.every((argument, index) => argument === npxCommand[index])
|
||||
(["npx", "bunx"] as const).some((runner) => {
|
||||
const runnerCommand = packageRunnerRegistrationCommand(runner, platform);
|
||||
return (
|
||||
command.length === runnerCommand.length &&
|
||||
command.every((argument, index) => argument === runnerCommand[index])
|
||||
);
|
||||
})
|
||||
)
|
||||
return true;
|
||||
return (
|
||||
|
||||
@@ -253,9 +253,9 @@ describe("setup workflow action selection", () => {
|
||||
|
||||
expect(result.status).toBe("ready");
|
||||
expect(host.configuredCommands).toEqual([
|
||||
setupRegistrationCommand("win32", false),
|
||||
setupRegistrationCommand("win32", undefined),
|
||||
]);
|
||||
expect(setupRegistrationCommand("win32", false)).toEqual([
|
||||
expect(setupRegistrationCommand("win32", undefined)).toEqual([
|
||||
process.execPath,
|
||||
resolve(process.argv[1] ?? "rea"),
|
||||
"mcp",
|
||||
|
||||
@@ -52,7 +52,7 @@ export class FakeSetupHost implements SetupHost {
|
||||
|
||||
constructor(platform: NodeJS.Platform = "darwin") {
|
||||
this.platform = platform;
|
||||
this.registrationCommand = setupRegistrationCommand(platform, false);
|
||||
this.registrationCommand = setupRegistrationCommand(platform, undefined);
|
||||
}
|
||||
|
||||
macosVersion = (): Promise<string | undefined> =>
|
||||
|
||||
@@ -5,7 +5,11 @@ import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { npxRegistrationCommand } from "./ClientRegistrationIdentity.js";
|
||||
import {
|
||||
invokingPackageRunner,
|
||||
packageRunnerRegistrationCommand,
|
||||
type PackageRunner,
|
||||
} from "./ClientRegistrationIdentity.js";
|
||||
import {
|
||||
SUPPORTED_NODE_VERSION_PROSE,
|
||||
supportsNodeVersion,
|
||||
@@ -48,10 +52,10 @@ import type { DoctorScope } from "./Doctor.js";
|
||||
/** Resolve the executable and arguments used in a managed MCP registration. */
|
||||
export const setupRegistrationCommand = (
|
||||
platform: NodeJS.Platform,
|
||||
useNpmRunner: boolean,
|
||||
packageRunner: PackageRunner | undefined,
|
||||
): readonly string[] =>
|
||||
useNpmRunner
|
||||
? npxRegistrationCommand(platform)
|
||||
packageRunner !== undefined
|
||||
? packageRunnerRegistrationCommand(packageRunner, platform)
|
||||
: platform === "win32"
|
||||
? [
|
||||
process.execPath,
|
||||
@@ -113,7 +117,7 @@ export const systemSetupHost = (
|
||||
skillDestinations(homeDirectory, clientIds, environment, platform),
|
||||
registrationCommand: setupRegistrationCommand(
|
||||
platform,
|
||||
environment.npm_command === "exec",
|
||||
invokingPackageRunner(environment, platform),
|
||||
),
|
||||
nodeVersion: process.versions.node,
|
||||
macosVersion: () => doctorHost.macosVersion(),
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
type UpdateOutput,
|
||||
} from "./Update.js";
|
||||
import { planIntegrationMaintenance } from "./UpdateMaintenance.js";
|
||||
import { withoutPackageRunnerIdentity } from "./ClientRegistrationIdentity.js";
|
||||
|
||||
/** npm filesystem queries used to identify the installation being updated. */
|
||||
export interface NpmInstallationHost {
|
||||
@@ -45,9 +46,8 @@ export const runUpdateCommand = (
|
||||
resolveResult(err("No executable supplied."));
|
||||
return;
|
||||
}
|
||||
const environment = { ...selectedEnvironment };
|
||||
// A fresh installed entry point must not inherit npx's invocation identity.
|
||||
delete environment.npm_command;
|
||||
// A fresh installed entry point must not inherit a package runner's invocation identity.
|
||||
const environment = withoutPackageRunnerIdentity(selectedEnvironment);
|
||||
const child = spawn(executable, args, {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: environment,
|
||||
|
||||
Reference in New Issue
Block a user