Repository navigation
Prefer the .cmd sibling when Windows resolves an extensionless npm shim - #9238
Conversation
shutil.which can hand back npm/pnpm's extensionless POSIX shim ahead of the .cmd sibling that Windows can actually execute, so unsloth start codex/pi died in CreateProcess with WinError 193. Resolve the sibling first, then let the existing cmd-shim parser take over. Fixes unslothai#9167
|
Confirmed this hits the path in unsloth_cli/commands/start.py where _resolved_launch_command only enters the shim parser for .cmd/.bat, so an extensionless npm shim still reaches CreateProcess. Will get this reviewed. |
CreateProcess can run a PE binary regardless of its name, so a resolved real executable must keep priority over a stale .cmd wrapper beside it. Substitute the sibling only when the resolved file opens with a shebang, which is what an unlaunchable npm/pnpm POSIX shim always does.
|
mahiatlinux
left a comment
There was a problem hiding this comment.
Review posted as a comment above.
The rescue lived only in _resolved_launch_command, which _launch alone reaches. The version and capability probes, npm install resolution, and the MCP subagent bridges all spawn the same shutil.which result directly, so an extensionless shim still broke them (and everything under --no-launch). Extract _prefer_windows_cmd_sibling, apply it in _which_with_install_dirs, _npm_executable, both subagent bridges, and keep the _resolved_launch_command call for _launch's raw resolver. Also per review: drop .bat (neither npm's cmd-shim nor pnpm's writer produces one), restore the no-sibling test's mutation coverage lost to the shebang gate, and pin the OSError suppression with a directory fixture.
|
@mahiatlinux Thank you for the thorough review — every point was right, addressed in a5f1f06:
508 pass locally (500 at merge base). |
unslothai#9167's pnpm dir holds pi.CMD, not pi.cmd. Dropping .CMD from the probe tuple was caught by no test, so the entry could be removed silently. Default NTFS matches either casing on the .cmd probe, but a case-sensitive volume (ReFS, or a directory flagged with fsutil setCaseSensitiveInfo) does not, and neither does a case-sensitive CI filesystem.
|
Went through this one carefully since we cannot run Windows on our CI box, and I wanted evidence rather than a code read. Summary: the issue is real, the fix is correct, and I could not find a path it regresses. I pushed one small test on top. Before and afterBefore, on Windows with a Python in the 3.12.0 to 3.12.9 range, Root cause is upstream, not oursThis is CPython gh-109590 / gh-127001. PR #103179 made How I verified itI built a simulator that models the two real behaviours this sits between: Same 19 scenarios run against the merge base and the head:
Seven scenarios flip from WinError 193 to a successful launch, and none regress:
Passing identically at both ends, which is the part I cared about most:
I also mutation-tested the guards rather than trusting that the tests cover them. Breaking Compatibility: the helper compiles and behaves correctly on 3.9 through 3.14. Nothing under Not covered by any of this: no real Windows host ran a single line of it. The simulation encodes documented behaviour, not observed behaviour, so a confirmation from anyone on Windows with a 3.12.x in the affected range would be worth having. What I pushed
One thing left deliberately alone: if someone has |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Also correct the docstring's account of when shutil.which returns the bare name: it is the CPython 3.12.0 to 3.12.9 regression (gh-127001), or a PATHEXT containing a dot-only entry, not any earlier PATH entry.
Both bridges resolved an executable and handed it straight to Popen. On Windows that can be a .cmd, which CreateProcess runs through cmd.exe, and cmd.exe treats CR/LF inside %* as command separators. The Codex prompt and Claude's --append-system-prompt always span lines, so the child could receive a truncated prompt or execute prompt text as shell input. This predates the .cmd rescue: shutil.which already returned the .cmd on every Python outside the 3.12.0 to 3.12.9 window. The rescue makes it reachable on that window too, so route both spawns through _resolved_launch_command, which resolves an npm shim to node.exe plus the script and bypasses cmd.exe. It is a no-op on POSIX and for any non-shim executable.
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Both assertions only passed because unsloth_cli/tests runs on ubuntu-latest only. On windows-latest they fail: test_prefer_cmd_sibling_is_none_safe_and_posix_noop asserted no rewrite without pinning os.name, so on Windows the rescue fired and the assert checked the opposite of its intent. Pin os.name to posix. test_resolved_launch_command_rescues_uppercase_cmd_sibling compared path spelling, but a case-insensitive volume answers the earlier .cmd probe with the .CMD file, so the returned spelling differs while the file is the same. Compare with os.path.samefile. Confirmed against a real windows-latest runner. Neither is a defect in the shipped helper; both were tests asserting something false on Windows.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Follow-up to my earlier review. I said then that no real Windows host had run any of this and that a confirmation from one would be worth having, so I went and got one: the branch was replicated onto a disposable staging repo and the CLI tests were run on real ubuntu-latest, macos-15 and windows-latest runners under Python 3.12. That immediately found two tests that were wrong, one of them mine. What the real runners found
The first asserted that a POSIX host never rewrites a resolution, but never pinned Both are fixed in Results after the fix
Both fixed tests now pass on Windows, checked by name rather than inferred from the count. The The 9 remaining Windows failures are pre-existing and not this branch's doing. Each was checked against the merge base and this branch touches none of them: chmod-bit assertions ( Subagent CIAll seven The two red checksNeither is caused by this branch, and I checked both against main rather than assuming:
Still not verifiedNo live agent was ever spawned through a real Windows npm shim. The Windows evidence covers the resolver logic and the test suite on a real Windows filesystem, plus documented |
I had written 3.12.0 to 3.12.9 and cited gh-127001. Measured on real windows-latest runners instead: only 3.12.0 returns the bare name, and 3.12.1, 3.12.2, 3.12.3, 3.12.7, 3.12.10, 3.11.9 and 3.13.15 all return the .CMD sibling. That is gh-109590, fixed in 3.12.1, not the later gh-127001. The original wording naming 3.12.0 alone was right and my edit broke it.
unsloth_cli/tests runs only on ubuntu, yet the launcher's hardest paths are Windows-shaped: npm .cmd shims, the sys.executable re-exec, PowerShell --no-launch recipes, CREATE_NEW_PROCESS_GROUP. Reviewing unslothai#9238 on a real windows-latest runner found 9 failures in test_start.py, all tests asserting POSIX shapes of behavior that is deliberately different on Windows: - two chmod assertions: private-mode bits, which Windows does not model - two --no-launch recipes: PowerShell has no VAR=value prefix form, so the self-contained-last-line contract is POSIX; assert the $env: block - the studio server argv and process-group kwargs: Windows launches through sys.executable -X utf8 -c (unslothai#8490) with creationflags - the Pi extension path: separators - two unwritable-root fallbacks: staged via POSIX dir modes, skipped Verified on a real windows-latest runner: test_start.py 501 passed, 0 failed (from 9). The new Backend CI job runs exactly that file; the rest of unsloth_cli/tests still carries ~50 Windows failures whose per-file distribution shifts with the installed dependency set, so a broader leg would flake rather than guard.
unsloth_cli/tests runs only on ubuntu, yet the launcher's hardest paths are Windows-shaped: npm .cmd shims, the sys.executable re-exec, PowerShell --no-launch recipes, CREATE_NEW_PROCESS_GROUP. Reviewing unslothai#9238 on a real windows-latest runner found 9 failures in test_start.py, all tests asserting POSIX shapes of behavior that is deliberately different on Windows: - two chmod assertions: private-mode bits, which Windows does not model - two --no-launch recipes: PowerShell has no VAR=value prefix form, so the self-contained-last-line contract is POSIX; assert the $env: block - the studio server argv and process-group kwargs: Windows launches through sys.executable -X utf8 -c (unslothai#8490) with creationflags - the Pi extension path: separators - two unwritable-root fallbacks: staged via POSIX dir modes, skipped Verified on a real windows-latest runner: test_start.py 501 passed, 0 failed (from 9). The new Backend CI job runs exactly that file; the rest of unsloth_cli/tests still carries ~50 Windows failures whose per-file distribution shifts with the installed dependency set, so a broader leg would flake rather than guard.
What
On Windows,
unsloth start codex/unsloth start picould die inCreateProcesswithWinError 193whenshutil.which()resolved npm/pnpm's extensionless POSIX shim ahead of the.cmdsibling. Fixes #9167.How
_prefer_windows_cmd_sibling(): when on Windows, the resolved path does not carry a Windows-executable suffix, and the file opens with a shebang (what an unlaunchable npm/pnpm POSIX shim always is), substitute a sibling.cmd/.CMDif one exists. A PE binary — which CreateProcess can run regardless of its name — keeps priority over any stale wrapper beside it.Applied at the resolver layer, not just the launch path (per review — thanks @mahiatlinux):
_which_with_install_dirs— covers the Claude/Codex/OpenCode version and capability probes that spawn the result directly, including under--no-launch_npm_executable— covers agent installation via PowerShellcodex_subagent_mcp.py/claude_subagent_mcp.py— the MCP bridges that callshutil.which+Popenthemselves_resolved_launch_command— covers_launch's rawshutil.whichresolverNotes on the guard:
foo.bar.cmdnext tofoo.bar) is rescued too..cmdthen.CMD(case-sensitive volumes) only — neither npm's cmd-shim nor pnpm's writer produces a.batsibling.OSErrorduring the peek (directory, unreadable, deleted between resolve and open) falls through to the original path.Reach
In the same-directory layout #9167 reports,
shutil.whichoffers the bare name first on CPython 3.12.0 specifically (3.12.1+ move it last; most other versions never offer it). The rescue also applies when PATH ordering hands back an extensionless shim from an earlier directory, and is a no-op everywhere else.Tests
Eight tests around
_resolved_launch_command/_prefer_windows_cmd_sibling/_which_with_install_dirs: sibling preference, dotted bin name, shebang gate (PE + stale sibling untouched), no-sibling pass-through (mutation-verified: replacingsibling.is_file()withTruefails it), directory-fixture OSError suppression, None-safety, POSIX no-op, and resolver-layer application. Fullunsloth_cli/tests/test_start.py: 508 pass (500 at merge base). Ruff clean, kwarg-spacing formatter applied.