Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: buildNpmScriptCommand always uses cmd.exe on Windows (#734)

Jamie Chicagob570b4a2f6eb
diff --git a/__tests__/scripts/dev-safe.test.ts b/__tests__/scripts/dev-safe.test.ts
index eb3033ab4f..98b3f27acf 100644
--- a/__tests__/scripts/dev-safe.test.ts
+++ b/__tests__/scripts/dev-safe.test.ts
@@ -655,24 +655,45 @@ describe("getOrCreatePersistedSessionApiKey", () => {
});
describe("buildNpmScriptCommand", () => {
- it("reuses npm's own CLI path when available", () => {
+ it("runs npm through cmd.exe on Windows even when npm_execpath is set", () => {
+ // npm_execpath points to a path with spaces like
+ // "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js".
+ // spawnService uses shell:true on Windows, so passing that path as an
+ // argument causes cmd.exe to split on the space and fail with
+ // "'C:\Program' is not recognized as an internal or external command".
+ // The win32 branch must fire BEFORE the npm_execpath branch.
const command = buildNpmScriptCommand(
"dev:frontend",
"win32",
{
- npm_execpath: "C:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
- npm_node_execpath: "C:\\nodejs\\node.exe",
+ ComSpec: "C:\\Windows\\System32\\cmd.exe",
+ npm_execpath:
+ "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
+ npm_node_execpath: "C:\\Program Files\\nodejs\\node.exe",
},
- "C:\\fallback\\node.exe",
+ "C:\\Program Files\\nodejs\\node.exe",
);
expect(command).toEqual({
- command: "C:\\nodejs\\node.exe",
- args: [
- "C:\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
- "run",
- "dev:frontend",
- ],
+ command: "C:\\Windows\\System32\\cmd.exe",
+ args: ["/d", "/s", "/c", "npm", "run", "dev:frontend"],
+ });
+ });
+
+ it("reuses npm's own CLI path when available on POSIX", () => {
+ const command = buildNpmScriptCommand(
+ "dev:frontend",
+ "linux",
+ {
+ npm_execpath: "/usr/lib/node_modules/npm/bin/npm-cli.js",
+ npm_node_execpath: "/usr/bin/node",
+ },
+ "/fallback/node",
+ );
+
+ expect(command).toEqual({
+ command: "/usr/bin/node",
+ args: ["/usr/lib/node_modules/npm/bin/npm-cli.js", "run", "dev:frontend"],
});
});
diff --git a/scripts/dev-safe.mjs b/scripts/dev-safe.mjs
index ba0ca4431e..8d70d2953d 100644
--- a/scripts/dev-safe.mjs
+++ b/scripts/dev-safe.mjs
@@ -606,6 +606,17 @@ function buildConfigFromPorts(ports, cwd, env) {
*/
export function buildAgentServerEnv(config) {
return {
+ // Force Python to use UTF-8 for all file I/O and streams.
+ //
+ // On Windows, Python defaults to the system ANSI codepage (e.g. cp1252).
+ // The agent-server writes conversation metadata JSON that can contain
+ // emoji (e.g. βœ… U+2705) which cp1252 cannot encode, producing:
+ // UnicodeEncodeError: 'charmap' codec can't encode character '\u2705'
+ // Setting PYTHONUTF8=1 enables Python's UTF-8 mode (PEP 540) for the
+ // entire agent-server process, matching the behaviour on Linux/macOS
+ // where the locale is already UTF-8.
+ // This is a no-op on Linux/macOS where the locale is already UTF-8.
+ PYTHONUTF8: "1",
TMUX_TMPDIR: config.tmuxTmpDir,
OH_CONVERSATIONS_PATH: config.conversationsPath,
OH_BASH_EVENTS_DIR: config.bashEventsDir,
@@ -753,17 +764,24 @@ export function buildNpmScriptCommand(
env = process.env,
nodeExecPath = process.execPath,
) {
- if (env.npm_execpath) {
+ // On Windows, always use cmd.exe regardless of whether npm_execpath is set.
+ // npm_execpath points to a path like
+ // "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" which contains
+ // spaces. When that path is passed as an argument with shell:true in
+ // spawnService, cmd.exe splits on the space and tries to run "C:\Program"
+ // as a command, producing "not recognized as an internal or external command".
+ // Using "npm" via cmd.exe avoids the problem entirely.
+ if (platform === "win32") {
return {
- command: env.npm_node_execpath || nodeExecPath,
- args: [env.npm_execpath, "run", scriptName],
+ command: env.ComSpec || "cmd.exe",
+ args: ["/d", "/s", "/c", "npm", "run", scriptName],
};
}
- if (platform === "win32") {
+ if (env.npm_execpath) {
return {
- command: env.ComSpec || "cmd.exe",
- args: ["/d", "/s", "/c", "npm", "run", scriptName],
+ command: env.npm_node_execpath || nodeExecPath,
+ args: [env.npm_execpath, "run", scriptName],
};
}
diff --git a/scripts/dev-with-automation.mjs b/scripts/dev-with-automation.mjs
index 915445250e..cf41bd1461 100644
--- a/scripts/dev-with-automation.mjs
+++ b/scripts/dev-with-automation.mjs
@@ -583,6 +583,9 @@ function startAutomationBackend(config) {
{
cwd: config.stateDir,
env: {
+ // Force UTF-8 for all Python file I/O (same reason as agent-server;
+ // see buildAgentServerEnv in dev-safe.mjs).
+ PYTHONUTF8: "1",
// The URL the automation backend itself uses to call the
// agent-server's REST API (tarball upload + bash dispatch).
//