@@ -18,6 +18,7 @@ import {
1818 listLoginShellCandidates ,
1919 mergePathEntries ,
2020 mergePathValues ,
21+ npmShimExecutable ,
2122 preferGitForWindowsBinary ,
2223 readEnvironmentFromLoginShell ,
2324 readEnvironmentFromWindowsShell ,
@@ -654,6 +655,111 @@ describe("preferGitForWindowsBinary", () => {
654655 } ) ;
655656} ) ;
656657
658+ // Byte for byte what `npm install -g opencode-ai` writes as `opencode.cmd`.
659+ const NPM_OPENCODE_SHIM = [
660+ "@ECHO off" ,
661+ "GOTO start" ,
662+ ":find_dp0" ,
663+ "SET dp0=%~dp0" ,
664+ "EXIT /b" ,
665+ ":start" ,
666+ "SETLOCAL" ,
667+ "CALL :find_dp0" ,
668+ '"%dp0%\\node_modules\\opencode-ai\\bin\\opencode.exe" %*' ,
669+ "" ,
670+ ] . join ( "\r\n" ) ;
671+
672+ describe ( "npmShimExecutable" , ( ) => {
673+ const shimPath = "C:\\Users\\me\\AppData\\Roaming\\npm\\opencode.cmd" ;
674+ const executable =
675+ "C:\\Users\\me\\AppData\\Roaming\\npm\\node_modules\\opencode-ai\\bin\\opencode.exe" ;
676+ const exists = ( filePath : string ) => filePath === executable ;
677+
678+ it ( "finds the executable npm's shim runs" , ( ) => {
679+ expect ( npmShimExecutable ( shimPath , ( ) => NPM_OPENCODE_SHIM , exists ) ) . toBe ( executable ) ;
680+ expect (
681+ npmShimExecutable ( shimPath . replace ( ".cmd" , ".CMD" ) , ( ) => NPM_OPENCODE_SHIM , exists ) ,
682+ ) . toBe ( executable ) ;
683+ } ) ;
684+
685+ it ( "keeps a shim that does more than run the executable" , ( ) => {
686+ const withSetting = NPM_OPENCODE_SHIM . replace (
687+ "SETLOCAL" ,
688+ "SETLOCAL\r\nSET OPENCODE_CONFIG=C:\\x" ,
689+ ) ;
690+ expect ( npmShimExecutable ( shimPath , ( ) => withSetting , exists ) ) . toBeUndefined ( ) ;
691+ const nodeScript = NPM_OPENCODE_SHIM . replace (
692+ '"%dp0%\\node_modules\\opencode-ai\\bin\\opencode.exe" %*' ,
693+ '"%_prog%" "%dp0%\\node_modules\\opencode-ai\\bin\\opencode.js" %*' ,
694+ ) ;
695+ expect (
696+ npmShimExecutable (
697+ shimPath ,
698+ ( ) => nodeScript ,
699+ ( ) => true ,
700+ ) ,
701+ ) . toBeUndefined ( ) ;
702+ } ) ;
703+
704+ it ( "keeps a shim CMD would read differently" , ( ) => {
705+ // CMD keeps trailing characters in an unquoted `SET` value, so these point
706+ // `dp0`, and with it the executable, somewhere else.
707+ for ( const suffix of [ "\u00a0" , "\t" , " " ] ) {
708+ const altered = NPM_OPENCODE_SHIM . replace ( "SET dp0=%~dp0" , `SET dp0=%~dp0${ suffix } ` ) ;
709+ expect (
710+ npmShimExecutable (
711+ shimPath ,
712+ ( ) => altered ,
713+ ( ) => true ,
714+ ) ,
715+ ) . toBeUndefined ( ) ;
716+ }
717+ const nonBreakingSeparator = NPM_OPENCODE_SHIM . replace (
718+ 'opencode.exe" %*' ,
719+ 'opencode.exe"\u00a0%*' ,
720+ ) ;
721+ expect (
722+ npmShimExecutable (
723+ shimPath ,
724+ ( ) => nonBreakingSeparator ,
725+ ( ) => true ,
726+ ) ,
727+ ) . toBeUndefined ( ) ;
728+ const indented = NPM_OPENCODE_SHIM . replace ( "SETLOCAL" , " SETLOCAL" ) ;
729+ expect (
730+ npmShimExecutable (
731+ shimPath ,
732+ ( ) => indented ,
733+ ( ) => true ,
734+ ) ,
735+ ) . toBeUndefined ( ) ;
736+ } ) ;
737+
738+ it ( "keeps the shim when its executable is missing or it is not a .cmd" , ( ) => {
739+ expect (
740+ npmShimExecutable (
741+ shimPath ,
742+ ( ) => NPM_OPENCODE_SHIM ,
743+ ( ) => false ,
744+ ) ,
745+ ) . toBeUndefined ( ) ;
746+ expect (
747+ npmShimExecutable (
748+ shimPath ,
749+ ( ) => undefined ,
750+ ( ) => true ,
751+ ) ,
752+ ) . toBeUndefined ( ) ;
753+ expect (
754+ npmShimExecutable (
755+ shimPath . replace ( ".cmd" , ".bat" ) ,
756+ ( ) => NPM_OPENCODE_SHIM ,
757+ ( ) => true ,
758+ ) ,
759+ ) . toBeUndefined ( ) ;
760+ } ) ;
761+ } ) ;
762+
657763effectIt . layer ( NodeServices . layer ) ( "resolveSpawnCommand" , ( it ) => {
658764 it . effect ( "runs Windows executables directly without a shell" , ( ) =>
659765 Effect . gen ( function * ( ) {
@@ -672,6 +778,45 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
672778 } ) ,
673779 ) ;
674780
781+ it . effect . runIf ( HostProcess . Platform . defaultValue ( ) === "win32" ) (
782+ "runs the executable behind an npm shim directly when asked" ,
783+ ( ) =>
784+ Effect . scoped (
785+ Effect . gen ( function * ( ) {
786+ const fs = yield * FileSystem . FileSystem ;
787+ const path = yield * Path . Path ;
788+ const prefix = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-npm-shim-" } ) ;
789+ const shim = path . join ( prefix , "opencode.cmd" ) ;
790+ const executable = path . join (
791+ prefix ,
792+ "node_modules" ,
793+ "opencode-ai" ,
794+ "bin" ,
795+ "opencode.exe" ,
796+ ) ;
797+ yield * fs . makeDirectory ( path . dirname ( executable ) , { recursive : true } ) ;
798+ yield * fs . writeFileString ( executable , "" ) ;
799+ yield * fs . writeFileString ( shim , NPM_OPENCODE_SHIM ) ;
800+ const resolve = ( bypassNpmShim : boolean ) =>
801+ resolveSpawnCommand ( "opencode" , [ "serve" , "--port=4096" ] , {
802+ env : { PATH : "" , PATHEXT : ".COM;.EXE;.BAT;.CMD" } ,
803+ bypassNpmShim,
804+ } ) . pipe (
805+ Effect . provideService ( HostProcess . Platform , "win32" ) ,
806+ Effect . provideService ( CommandResolutionCache , new Map ( ) ) ,
807+ Effect . provideService ( SpawnExecutableResolution , ( ) => shim ) ,
808+ ) ;
809+
810+ expect ( yield * resolve ( true ) ) . toEqual ( {
811+ command : executable ,
812+ args : [ "serve" , "--port=4096" ] ,
813+ shell : false ,
814+ } ) ;
815+ expect ( ( yield * resolve ( false ) ) . shell ) . toBe ( true ) ;
816+ } ) ,
817+ ) ,
818+ ) ;
819+
675820 it . effect ( "builds one escaped cmd.exe line for Windows command shims" , ( ) =>
676821 Effect . gen ( function * ( ) {
677822 const command = yield * resolveSpawnCommand (
0 commit comments