npm Installs Codex as 3 Files on Windows. Launch the Wrong One: Not a Valid Win32 App

2026-11-28来源 dev.toEnglish
npm Installs Codex as 3 Files on Windows. Launch the Wrong One: Not a Valid Win32 App

On November 1, a Windows user opened this issue against Hermes IDE 2.3.4 after trying to start a Codex task:

hi: cannot start <path>\codex: %1 is not a valid Win32 application. (os error 193) 

Codex was installed. The same report says codex.cmd --version worked. The launcher had picked a different file from the same folder, one with the same name and no extension.

I build Hermes, so that bug was mine, and you should weigh the rest with that in mind. The file it should have picked has its own history: it runs through cmd.exe, the parser behind a pair of 2024 CVEs.

npm writes three files for one command

The @openai/codex package declares a single command, codex, pointing at bin/codex.js. On Unix that would be a symlink. On Windows npm uses cmd-shim, which exists because, in its own words, "symlinks are not suitable for this purpose there". Its source ends by writing three files:

return Promise.all([ writeFile(to + '.ps1', pwsh, 'utf8'), writeFile(to + '.cmd', cmd, 'utf8'), writeFile(to, sh, 'utf8'), ]).then(() => chmodShim(to)) 

to is the path with no extension. npm's docs give %AppData%\npm as the Windows example for the global prefix, and executables land directly in it. So one install puts three files in that folder:

File What it is Who reads it
codex A #!/bin/sh script with cases for Cygwin, MinGW, MSYS and WSL2 Unix-style shells on Windows
codex.cmd A batch file that starts with @ECHO off cmd.exe
codex.ps1 A PowerShell script PowerShell

All three do the same job: start node on bin/codex.js. npm isn't the only route, since Codex's README also gives a PowerShell installer for Windows.

Three identical doors in a row, two bricked shut and the third open with orange light behind it
One command, three files, and the one that works still needs cmd.exe.

Error 193 is a file format complaint

Microsoft lists ERROR_BAD_EXE_FORMAT as error 193 (0xC1), with the text %1 is not a valid Win32 application. That %1 is a template slot. It reached the message above unfilled, which is why the error reads like a bug in the error.

CreateProcess takes a "module" to execute, and a normal Windows program is a PE image, with a PE\0\0 signature that identifies the file. The extensionless shim opens with #!/bin/sh. Nothing in the CreateProcess docs reads an interpreter line, and cross-spawn's README lists "does not support shebangs" among Node's problems on Windows. A Mozilla bug about this same error puts it plainly: a file "needs to be a true Win32 executable or it needs to be spawned through a shell."

So the launcher's job was to avoid that file. The 2.3.5 commit states the cause: hi run, the step that started the agent in that report, "resolved codex to npm's extensionless shell shim" before trying PATHEXT, the environment variable that lists the extensions Windows treats as runnable.

On Unix that search order is right, because the file with no extension is the program. In an npm folder on Windows, that file is one CreateProcess refuses.

Other tools can take the same turn. Emdash, another tool for running coding agents in parallel, has a pull request, since closed as out of date, that describes discovery returning "an extensionless npm shim plus codex.cmd". The issue it links reads "provider direct PTY spawn can choose extensionless shim and fail with ERROR_BAD_EXE_FORMAT (193)". That PR predates Emdash's v1, and neither it nor the issue says whether current versions are affected. WezTerm's portable-pty also checks "exactly the user's string" in each PATH directory before it tries PATHEXT, so look at what a terminal built on it resolves. I haven't tested that.

The fix is one rule: on Windows, accept a name as given only if it already ends in a PATHEXT extension. This is my sketch of that rule, not the Hermes code:

fn accept_as_given(name: &str, pathext: &str) -> bool { let name = name.to_ascii_lowercase(); pathext .split(';') .filter(|ext| !ext.is_empty()) .any(|ext| name.ends_with(&ext.to_ascii_lowercase())) } 

When that returns false for codex, keep searching codex plus each PATHEXT entry, and codex.cmd wins.

Try it on a Windows machine

I haven't run these myself. Install any global npm command, swap the name if it isn't codex, and compare your results with the table below. The scripts assume npm's default prefix. Run npm prefix -g and swap the path if yours differs, as it did in the report above, where Node came from WinGet.

use std::{env, process::Command}; fn main() { let bin = env::var("APPDATA").unwrap() + r"\npm\codex"; for ext in ["", ".ps1", ".cmd"] { let path = format!("{bin}{ext}"); let out = Command::new(&path).arg("--version").output(); match out { Ok(o) => println!("{ext:?}: exit {:?}", o.status.code()), Err(e) => println!("{ext:?}: {e}"), } } } 

The same test in Node:

const { spawnSync } = require("node:child_process"); const { join } = require("node:path"); const bin = join(process.env.APPDATA, "npm", "codex"); for (const ext of ["", ".ps1", ".cmd"]) { const r = spawnSync(bin + ext, ["--version"]); console.log(JSON.stringify(ext), r.error?.code ?? r.status); } 

This is what the sources predict, read from code and docs rather than from a run:

File Rust Command Node spawnSync, no shell
codex os error 193 ENOENT
codex.ps1 os error 193 EFTYPE
codex.cmd starts, through cmd.exe EINVAL

Rust's resolve_exe "falls back to the plain file name if a full path is given and the extension is omitted", so the shim reaches CreateProcess. libuv, underneath Node, tries a literal name only if it has an extension, then appends .com and .exe, so it never finds the shim. A .ps1 is a plain text file, and libuv maps error 193 to EFTYPE. Node's EINVAL on .cmd is deliberate, a 2024 security fix.

Three files and two runtimes give five different outcomes. Any of the four errors in a launcher's logs should send you to check which file it asked for.

The .cmd works through a side door

Microsoft's CreateProcess docs say that to run a batch file, "you must start the command interpreter", with cmd.exe as the application. Rust's docs describe what happens if you don't: "An undocumented feature of this function is that when given a .bat file as the application to run, it will automatically convert that into running cmd.exe /c". They add that this "may be removed in the future and so should not be relied upon."

That conversion is where the trouble starts. On Windows, arguments travel as one string, and each program parses its own. cmd.exe parses differently from the C runtime. On April 9, 2024, RyotaK of Flatt Security published BatBadBut, with this example, which starts calc.exe with no shell option set:

spawn('./test.bat', ['"&calc.exe']) 

Each runtime answered it differently:

  • Rust: CVE-2024-24576, rated critical, fixed in 1.77.2. Command now returns InvalidInput when it can't escape an argument safely.
  • Node: CVE-2024-27980, rated high, on the 18.x, 20.x and 21.x lines. spawn on a .bat or .cmd without shell now errors with EINVAL.
  • Others: Flatt's table, at publication, lists patches for Haskell and PHP, documentation changes for Erlang, Go, Python and Ruby, and "Won't fix" for Java.

A parcel moving along a conveyor through a checkpoint gate while a small orange second parcel slips out of its side
An argument built for one parser, read by another.

The arguments to an agent CLI are often a prompt, a ticket title or a pasted stack trace, which is text you didn't check character by character. Send it through a .cmd and it meets cmd.exe's parser. Your options, best first:

Skip the shim when you can. Run node on the package's script, and cmd.exe never enters the picture. Global packages live under {prefix}/node_modules on Windows:

const { spawn } = require("node:child_process"); const { join } = require("node:path"); const prefix = join(process.env.APPDATA, "npm"); const script = join( prefix, "node_modules", "@openai", "codex", "bin", "codex.js", ); const args = process.argv.slice(2); spawn(process.execPath, [script, ...args], { stdio: "inherit" }); 

Confirm the prefix with npm prefix -g, and read the script path from the package's bin field, because that layout belongs to the package and can change.

In Rust, launch codex.cmd and treat the error as real. The standard library swaps in cmd.exe and escapes arguments for it. If an argument can't be escaped, the spawn fails. Show that failure to the user instead of retrying some other way. Rust's process docs still call batch files "especially vulnerable to malicious input" and say running them this way may be removed, so keep this as the fallback for when you can't skip the shim.

In Node, keep untrusted text away from shell: true. DEP0190, a runtime deprecation since v24, says that with an args array "the values are not escaped, only space-separated, which can lead to shell injection."

What changed in Hermes 2.3.5

Hermes 2.3.5 shipped on November 6, five days after the report. The release note is one line: "On Windows, starting a Codex task (or any agent installed with npm) no longer fails with "%1 is not a valid Win32 application"."

The functional change is in one file. A bare name now runs as given on Windows only when it ends in a PATHEXT extension. A Windows-only test checks three cases: codex resolves to codex.cmd, codex.cmd resolves to itself, and with the .cmd removed, codex resolves to nothing. The commit says the test runs in CI on windows-2022.

After the fix, hi run hands codex.cmd to Rust's Command with the first prompt as an argument, so it is on the second path above: the standard library escapes the prompt or the spawn fails. Line breaks in the prompt become spaces first, because cmd.exe can't pass them inside an argument.

This sheet is where that Codex task starts.

The New task sheet in Hermes with its agent picker open, showing Claude Code and Codex as installed and other supported agents marked Not installed
The New task sheet with its agent picker open: Claude Code and Codex installed, the other supported agents marked Not installed.

Hermes runs each agent in a real terminal with that agent's own sign-in. It's free to download, source-available under BSL 1.1, and runs on macOS, Linux and Windows (best-effort). It's not affiliated with Nous Research, OpenAI or any other vendor named here.

If you maintain anything that finds an agent's binary, open that code and read the search order. If the literal name comes before PATHEXT and the result goes straight to CreateProcess, you can hit this bug on Windows wherever an npm shim sits on PATH, whether or not a user has told you yet.

If this was useful

An agent you ship on Node keeps making these decisions: which process to start, and what text is allowed to reach a shell. I wrote AI That Ships to cover the guardrails, evals, cost control and Node.js deployment around that, in TypeScript. If you run several agents, Hermes IDE is a free download, and a star on GitHub helps other developers find it.

AI That Ships — AI in TypeScript

It's book 5 of AI in TypeScript, and all five are here.

原文:https://www.a8q9.cn/en/article/npm-installs-codex-as-3-files-on-windows-launch-the-wrong-one-not-a-valid-win32--b245a5.html

相关文章