Skip to content

Commit

Permalink
fix: only clear stderr channel when a run is started
Browse files Browse the repository at this point in the history
  • Loading branch information
typed-sigterm committed Dec 21, 2024
1 parent 86f0218 commit 9ecd05f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
27 changes: 14 additions & 13 deletions extension/command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable no-template-curly-in-string */

import type * as vscode from 'vscode';
import { Buffer } from 'node:buffer';
import { spawn } from 'node:child_process';
import path from 'node:path';
import { decode } from 'iconv-lite';
import ps from 'ps-tree';
import kill from 'tree-kill';
import * as vscode from 'vscode';


const execExt = process.platform === 'win32' ? '.exe' : '';

/**
Expand All @@ -25,8 +25,6 @@ export function evalCommand(template: string, document: vscode.TextDocument) {
.replaceAll('${execExt}', execExt);
}

const commandStderrOutput = vscode.window.createOutputChannel('OI Runner++ Task');

function transformSocketOutput(output: any) {
return output === null
? ''
Expand All @@ -46,17 +44,17 @@ export interface ExecuteCommandResult {
* @param command The command to execute.
* @param args An array of string arguments for the command.
* @param stdin The string to write to the stdin of the command.
* @param stderr The output channel to write stderr to. Once it is written to, it will be shown.
* @param cwd The current working directory for the command.
* @param signal The {@link AbortSignal} that can be for cancel the execution
* @throws {AbortError} If aborted
* @returns A promise that resolves
*/
export function executeCommand(command: string, args: string[], stdin?: string, cwd?: string, signal?: AbortSignal) {
export function executeCommand(command: string, args: string[], stdin?: string, stderr?: vscode.OutputChannel, cwd?: string, signal?: AbortSignal) {
return new Promise<ExecuteCommandResult>((resolve, reject) => {
let startTime: number;
let stderrUsed = false;
let stderrOpened = false;

commandStderrOutput.clear();
const child = spawn(command, args, {
cwd,
shell: true,
Expand All @@ -73,10 +71,15 @@ export function executeCommand(command: string, args: string[], stdin?: string,
});

child.stdin.end(stdin ?? '');
child.stderr.on('data', (data) => {
stderrUsed = true;
commandStderrOutput.append(transformSocketOutput(data));
});
if (stderr) {
child.stderr.on('data', (data) => {
if (!stderrOpened) {
stderrOpened = true;
stderr.show();
}
stderr.append(transformSocketOutput(data));
});
}

child.once('spawn', () => startTime = Date.now());
child.once('error', (e) => {
Expand All @@ -92,8 +95,6 @@ export function executeCommand(command: string, args: string[], stdin?: string,
exitCode: child.exitCode!,
duration: Date.now() - startTime,
});
if (stderrUsed)
commandStderrOutput.show();
});
});
}
5 changes: 3 additions & 2 deletions extension/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Runner extends vscode.EventEmitter<EventMessage> {
super();
}

private _stderrChannel = vscode.window.createOutputChannel('OI Runner++ Task');
private _currentController?: AbortController;

private _evalTask(task: string) {
Expand Down Expand Up @@ -46,7 +47,7 @@ export class Runner extends vscode.EventEmitter<EventMessage> {
Promise.resolve()
.then(() => { // compile
if (step !== 'execute' && t.compile)
return executeCommand(t.compile[0], t.compile[1], undefined, cwd, signal);
return executeCommand(t.compile[0], t.compile[1], undefined, this._stderrChannel, cwd, signal);
})
.then((p) => { // compile completed
if (p && p.exitCode) {
Expand All @@ -61,7 +62,7 @@ export class Runner extends vscode.EventEmitter<EventMessage> {
})
.then(() => { // execute
if (step !== 'compile' && t.execute)
return executeCommand(t.execute[0], t.execute[1], stdin, cwd, signal);
return executeCommand(t.execute[0], t.execute[1], stdin, this._stderrChannel, cwd, signal);
})
.then((p) => { // execute completed
if (!p)
Expand Down

0 comments on commit 9ecd05f

Please sign in to comment.