1- import { mkdtempSync , mkdirSync , writeFileSync , rmSync } from "node:fs" ;
1+ import { mkdtempSync , mkdirSync , writeFileSync , rmSync , realpathSync } from "node:fs" ;
22import { tmpdir } from "node:os" ;
33import { join } from "node:path" ;
44import { afterAll , describe , expect , it , vi } from "vitest" ;
@@ -17,7 +17,15 @@ else if (args[0] === 'wait') process.stdin.resume();
1717else console.log(JSON.stringify(args));
1818` ) ;
1919const cmd = join ( binDir , "codex.cmd" ) ;
20- writeFileSync ( cmd , `@echo off\r\n"${ process . execPath } " "%~dp0probe.cjs" %*\r\n` ) ;
20+ // Match npm/cmd-shim's dispatch: its goto/endLocal sequence avoids a second
21+ // batch context interpreting forwarded arguments. A plain `%*` wrapper does not.
22+ writeFileSync ( cmd , [
23+ '@ECHO off' , 'GOTO start' , ':find_dp0' , 'SET dp0=%~dp0' , 'EXIT /b' ,
24+ ':start' , 'SETLOCAL' , 'CALL :find_dp0' ,
25+ `SET "_prog=${ process . execPath } "` ,
26+ 'endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%probe.cjs" %*' ,
27+ '' ,
28+ ] . join ( '\r\n' ) ) ;
2129afterAll ( ( ) => { vi . unstubAllEnvs ( ) ; rmSync ( home , { recursive : true , force : true } ) ; } ) ;
2230
2331describe ( "engine process launch" , ( ) => {
@@ -48,9 +56,11 @@ describe.skipIf(process.platform !== "win32")("Windows npm launcher", () => {
4856 const args = [ 'two words' , 'a"b' , '(paren)' , 'x&y' , '%PATH%' , 'semi;colon' ] ;
4957 const child = spawnEngine ( cmd , args , { stdio : [ "ignore" , "pipe" , "pipe" ] } ) ;
5058 let output = "" ;
59+ let stderr = "" ;
5160 child . stdout . on ( "data" , ( data ) => { output += data ; } ) ;
61+ child . stderr . on ( "data" , ( data ) => { stderr += data ; } ) ;
5262 const code = await new Promise ( ( resolve , reject ) => { child . on ( "error" , reject ) ; child . on ( "close" , resolve ) ; } ) ;
53- expect ( code ) . toBe ( 0 ) ;
63+ expect ( code , stderr ) . toBe ( 0 ) ;
5464 expect ( JSON . parse ( output ) ) . toEqual ( args ) ;
5565 } ) ;
5666
@@ -77,6 +87,6 @@ describe.skipIf(process.platform !== "win32")("Windows npm launcher", () => {
7787 writeFileSync ( join ( binDir , "codex" ) , "#!/bin/sh\nexit 1\n" ) ;
7888 vi . resetModules ( ) ;
7989 const { resolveEngineBin } = await import ( "./engineBin.js" ) ;
80- expect ( resolveEngineBin ( "codex" ) ) . toBe ( cmd ) ;
90+ expect ( realpathSync ( resolveEngineBin ( "codex" ) ) ) . toBe ( realpathSync ( cmd ) ) ;
8191 } ) ;
8292} ) ;
0 commit comments