|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
15 | 15 | import type * as nodePty from "node-pty";
|
| 16 | +import * as child_process from "child_process"; |
16 | 17 | import * as vscode from "vscode";
|
17 | 18 |
|
18 | 19 | import { requireNativeModule } from "../utilities/native";
|
@@ -98,6 +99,7 @@ export class SwiftPtyProcess implements SwiftProcess {
|
98 | 99 | // The pty process hangs on Windows when debugging the extension if we use conpty
|
99 | 100 | // See https://github.com/microsoft/node-pty/issues/640
|
100 | 101 | const useConpty = isWindows && process.env["VSCODE_DEBUG"] === "1" ? false : true;
|
| 102 | + |
101 | 103 | this.spawnedProcess = spawn(this.command, this.args, {
|
102 | 104 | cwd: this.options.cwd,
|
103 | 105 | env: { ...process.env, ...this.options.env },
|
@@ -153,3 +155,91 @@ export class SwiftPtyProcess implements SwiftProcess {
|
153 | 155 |
|
154 | 156 | onDidClose: vscode.Event<number | void> = this.closeEmitter.event;
|
155 | 157 | }
|
| 158 | + |
| 159 | +/** |
| 160 | + * A {@link SwiftProcess} that spawns a child process and does not bind to stdio. |
| 161 | + * |
| 162 | + * Use this for Swift tasks that do not need to accept input, as its lighter weight and |
| 163 | + * less error prone than using a spawned node-pty process. |
| 164 | + * |
| 165 | + * Specifically node-pty on Linux suffers from a long standing issue where the last chunk |
| 166 | + * of output before a program exits is sometimes dropped, especially if that program produces |
| 167 | + * a lot of output immediately before exiting. See https://github.com/microsoft/node-pty/issues/72 |
| 168 | + */ |
| 169 | +export class ReadOnlySwiftProcess implements SwiftProcess { |
| 170 | + private readonly spawnEmitter: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); |
| 171 | + private readonly writeEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter<string>(); |
| 172 | + private readonly errorEmitter: vscode.EventEmitter<Error> = new vscode.EventEmitter<Error>(); |
| 173 | + private readonly closeEmitter: vscode.EventEmitter<number | void> = new vscode.EventEmitter< |
| 174 | + number | void |
| 175 | + >(); |
| 176 | + |
| 177 | + private spawnedProcess: child_process.ChildProcessWithoutNullStreams | undefined; |
| 178 | + |
| 179 | + constructor( |
| 180 | + public readonly command: string, |
| 181 | + public readonly args: string[], |
| 182 | + private readonly options: vscode.ProcessExecutionOptions = {} |
| 183 | + ) {} |
| 184 | + |
| 185 | + spawn(): void { |
| 186 | + try { |
| 187 | + this.spawnedProcess = child_process.spawn(this.command, this.args, { |
| 188 | + cwd: this.options.cwd, |
| 189 | + env: { ...process.env, ...this.options.env }, |
| 190 | + }); |
| 191 | + |
| 192 | + this.spawnedProcess.stdout.on("data", data => { |
| 193 | + this.writeEmitter.fire(data.toString()); |
| 194 | + }); |
| 195 | + |
| 196 | + this.spawnedProcess.stderr.on("data", data => { |
| 197 | + this.writeEmitter.fire(data.toString()); |
| 198 | + }); |
| 199 | + |
| 200 | + this.spawnedProcess.on("error", error => { |
| 201 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 202 | + this.closeEmitter.fire(); |
| 203 | + }); |
| 204 | + |
| 205 | + this.spawnedProcess.once("exit", code => { |
| 206 | + this.closeEmitter.fire(code ?? undefined); |
| 207 | + this.dispose(); |
| 208 | + }); |
| 209 | + } catch (error) { |
| 210 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 211 | + this.closeEmitter.fire(); |
| 212 | + this.dispose(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + handleInput(_s: string): void { |
| 217 | + // Do nothing |
| 218 | + } |
| 219 | + |
| 220 | + terminate(signal?: NodeJS.Signals): void { |
| 221 | + if (!this.spawnedProcess) { |
| 222 | + return; |
| 223 | + } |
| 224 | + this.spawnedProcess.kill(signal); |
| 225 | + this.dispose(); |
| 226 | + } |
| 227 | + |
| 228 | + setDimensions(_dimensions: vscode.TerminalDimensions): void { |
| 229 | + // Do nothing |
| 230 | + } |
| 231 | + |
| 232 | + dispose(): void { |
| 233 | + this.spawnedProcess?.stdout.removeAllListeners(); |
| 234 | + this.spawnedProcess?.stderr.removeAllListeners(); |
| 235 | + this.spawnedProcess?.removeAllListeners(); |
| 236 | + } |
| 237 | + |
| 238 | + onDidSpawn: vscode.Event<void> = this.spawnEmitter.event; |
| 239 | + |
| 240 | + onDidWrite: vscode.Event<string> = this.writeEmitter.event; |
| 241 | + |
| 242 | + onDidThrowError: vscode.Event<Error> = this.errorEmitter.event; |
| 243 | + |
| 244 | + onDidClose: vscode.Event<number | void> = this.closeEmitter.event; |
| 245 | +} |
0 commit comments