@@ -27,7 +27,7 @@ import { resolve } from 'path';
2727import { createServer , Socket } from 'net' ;
2828import { lstatSync , readlinkSync } from 'fs' ;
2929import { opendir , readFile } from 'fs/promises' ;
30- import { ChildProcess , execSync , spawn , SpawnOptionsWithoutStdio } from 'child_process' ;
30+ import { ChildProcess , execSync , spawn , SpawnOptions } from 'child_process' ;
3131import { registerCommonFeatures } from './common' ;
3232
3333const ahkconfig = workspace . getConfiguration ( 'AutoHotkey2' ) ;
@@ -254,7 +254,7 @@ async function runScript(textEditor: TextEditor, selection = false) {
254254 args . push ( lc ) ;
255255 return '' ;
256256 } ) ;
257- const opt : SpawnOptionsWithoutStdio = {
257+ const opt : SpawnOptions = {
258258 cwd : resolve ( textEditor . document . fileName , '..' ) ,
259259 } ;
260260 const uiAccess = ahkStatusBarItem . text . endsWith ( '[UIAccess]' ) ;
@@ -263,6 +263,7 @@ async function runScript(textEditor: TextEditor, selection = false) {
263263 const redirect = `(()=>(std:=FileOpen('${ pipe } ','w'),DllCall('SetStdHandle','uint',-11,'ptr',std.Handle),DllCall('SetStdHandle','uint',-12,'ptr',std.Handle),OnExit((*)=>!std)))()` ;
264264 createPipeReadStream ( pipe ) . then ( out => out . on ( 'data' , output_append ) ) ;
265265 args . push ( '/include' , createTempFile ( redirect , 'ahk-stdout-redirect' ) ) ;
266+ opt . stdio = 'ignore' ;
266267 if ( selecttext )
267268 path = createTempFile ( selecttext ) ;
268269 } else opt . env = Object . fromEntries ( Object . entries ( process . env )
@@ -285,13 +286,14 @@ async function runScript(textEditor: TextEditor, selection = false) {
285286 } ) ;
286287 if ( ! cp . pid )
287288 return ;
288- if ( path === '*' )
289- cp . stdin ?. write ( selecttext ) , cp . stdin ?. end ( ) ;
289+ if ( ! uiAccess ) {
290+ cp . stderr ! . on ( 'data' , output_append ) , cp . stdout ! . on ( 'data' , output_append ) ;
291+ if ( path === '*' )
292+ cp . stdin ! . end ( selecttext ) ;
293+ }
290294 ahkprocesses . set ( cp . pid , cp ) ;
291295 cp . path = path ;
292296 commands . executeCommand ( 'setContext' , 'ahk2:isRunning' , true ) ;
293- if ( ! uiAccess )
294- cp . stderr ?. on ( 'data' , output_append ) , cp . stdout ?. on ( 'data' , output_append ) ;
295297 cp . on ( 'exit' , ( code ) => {
296298 outputchannel . appendLine ( `[info] ${ spid } exited with code=${ code } in ${ ( Date . now ( ) - startTime ) / 1000 } seconds` ) ;
297299 ahkprocesses . delete ( cp . pid ! ) ;
@@ -377,8 +379,8 @@ async function compileScript(editor: TextEditor) {
377379 else
378380 window . showErrorMessage ( localize ( 'ahk2.compiledfailed' ) ) ;
379381 } ) ;
380- cp . stderr ? .on ( 'data' , output_append ) ;
381- cp . stdout ? .on ( 'data' , output_append ) ;
382+ cp . stderr . on ( 'data' , output_append ) ;
383+ cp . stdout . on ( 'data' , output_append ) ;
382384 } else
383385 window . showErrorMessage ( localize ( 'ahk2.compiledfailed' ) ) ;
384386}
@@ -439,7 +441,7 @@ if ${!!word} && !DllCall('oleacc\\AccessibleObjectFromWindow', 'ptr', ctl, 'uint
439441 const isUIAccess = ahkStatusBarItem . text . endsWith ( '[UIAccess]' ) ;
440442 const cp = spawn ( executePath , [ '/ErrorStdOut' , isUIAccess ? createTempFile ( script ) : '*' ] ) ;
441443 if ( ! isUIAccess )
442- cp . stdin . write ( script ) , cp . stdin . end ( ) ;
444+ cp . stdin . end ( script ) ;
443445}
444446
445447function getConfig < T > ( section : string ) {
@@ -718,9 +720,11 @@ function createTempFile(str: string, prefix = 'ahk-script') {
718720function createPipeReadStream ( path : string ) : Promise < Socket > {
719721 return new Promise ( ( resolve ) => {
720722 const server = createServer ( ( socket ) => {
723+ const destroy = ( ) => socket . destroy ( ) ;
721724 server . close ( ) ;
722725 socket . setEncoding ( 'utf-8' ) ;
723- socket . on ( 'error' , ( ) => socket . destroy ( ) ) ;
726+ socket . on ( 'close' , destroy ) ;
727+ socket . on ( 'error' , destroy ) ;
724728 resolve ( socket ) ;
725729 } ) . listen ( path ) ;
726730 setTimeout ( ( ) => server . close ( ) , 2000 ) ;
0 commit comments