| ... | @@ -45,23 +45,29 @@ export interface ProcessRunOutput { | ... | @@ -45,23 +45,29 @@ export interface ProcessRunOutput { |
| 45 | | 45 | |
| 46 | const ProcessInvocationFields = { | 46 | const ProcessInvocationFields = { |
| 47 | command: Schema.String, | 47 | command: Schema.String, |
| 48 | args: Schema.Array(Schema.String), | 48 | argumentCount: Schema.Number, |
| 49 | cwd: Schema.optional(Schema.String), | 49 | cwd: Schema.optional(Schema.String), |
| | 50 | spawnCwd: Schema.optional(Schema.String), |
| 50 | }; | 51 | }; |
| 51 | | 52 | |
| 52 | const formatProcessInvocation = (input: { | 53 | const formatProcessInvocation = (input: { |
| 53 | readonly command: string; | 54 | readonly command: string; |
| 54 | readonly args: ReadonlyArray<string>; | | |
| 55 | readonly cwd?: string | undefined; | 55 | readonly cwd?: string | undefined; |
| | 56 | readonly spawnCwd?: string | undefined; |
| 56 | }): string => { | 57 | }): string => { |
| 57 | const command = [input.command, ...input.args].join(" "); | 58 | const executionCwd = input.spawnCwd ?? input.cwd; |
| 58 | return input.cwd === undefined ? `'${command}'` : `'${command}' in '${input.cwd}'`; | 59 | return executionCwd === undefined |
| | 60 | ? `'${input.command}'` |
| | 61 | : `'${input.command}' in '${executionCwd}'`; |
| 59 | }; | 62 | }; |
| 60 | | 63 | |
| 61 | export class ProcessSpawnError extends Schema.TaggedErrorClass<ProcessSpawnError>()( | 64 | export class ProcessSpawnError extends Schema.TaggedErrorClass<ProcessSpawnError>()( |
| 62 | "ProcessSpawnError", | 65 | "ProcessSpawnError", |
| 63 | { | 66 | { |
| 64 | ...ProcessInvocationFields, | 67 | ...ProcessInvocationFields, |
| | 68 | resolvedCommand: Schema.optional(Schema.String), |
| | 69 | resolvedArgumentCount: Schema.optional(Schema.Number), |
| | 70 | shell: Schema.optional(Schema.Boolean), |
| 65 | cause: Schema.Defect(), | 71 | cause: Schema.Defect(), |
| 66 | }, | 72 | }, |
| 67 | ) { | 73 | ) { |
| ... | @@ -74,6 +80,7 @@ export class ProcessStdinError extends Schema.TaggedErrorClass<ProcessStdinError | ... | @@ -74,6 +80,7 @@ export class ProcessStdinError extends Schema.TaggedErrorClass<ProcessStdinError |
| 74 | "ProcessStdinError", | 80 | "ProcessStdinError", |
| 75 | { | 81 | { |
| 76 | ...ProcessInvocationFields, | 82 | ...ProcessInvocationFields, |
| | 83 | stdinBytes: Schema.Number, |
| 77 | cause: Schema.Defect(), | 84 | cause: Schema.Defect(), |
| 78 | }, | 85 | }, |
| 79 | ) { | 86 | ) { |
| ... | @@ -88,10 +95,11 @@ export class ProcessOutputLimitError extends Schema.TaggedErrorClass<ProcessOutp | ... | @@ -88,10 +95,11 @@ export class ProcessOutputLimitError extends Schema.TaggedErrorClass<ProcessOutp |
| 88 | ...ProcessInvocationFields, | 95 | ...ProcessInvocationFields, |
| 89 | stream: Schema.Literals(["stdout", "stderr"]), | 96 | stream: Schema.Literals(["stdout", "stderr"]), |
| 90 | maxBytes: Schema.Number, | 97 | maxBytes: Schema.Number, |
| | 98 | observedBytes: Schema.Number, |
| 91 | }, | 99 | }, |
| 92 | ) { | 100 | ) { |
| 93 | override get message(): string { | 101 | override get message(): string { |
| 94 | return `Process ${formatProcessInvocation(this)} ${this.stream} exceeded ${this.maxBytes} bytes`; | 102 | return `Process ${formatProcessInvocation(this)} ${this.stream} produced ${this.observedBytes} bytes, exceeding the ${this.maxBytes} byte limit`; |
| 95 | } | 103 | } |
| 96 | } | 104 | } |
| 97 | | 105 | |
| ... | @@ -120,12 +128,14 @@ export class ProcessTimeoutError extends Schema.TaggedErrorClass<ProcessTimeoutE | ... | @@ -120,12 +128,14 @@ export class ProcessTimeoutError extends Schema.TaggedErrorClass<ProcessTimeoutE |
| 120 | } | 128 | } |
| 121 | } | 129 | } |
| 122 | | 130 | |
| 123 | export type ProcessRunError = | 131 | export const ProcessRunError = Schema.Union([ |
| 124 | | ProcessSpawnError | 132 | ProcessSpawnError, |
| 125 | | ProcessStdinError | 133 | ProcessStdinError, |
| 126 | | ProcessOutputLimitError | 134 | ProcessOutputLimitError, |
| 127 | | ProcessReadError | 135 | ProcessReadError, |
| 128 | | ProcessTimeoutError; | 136 | ProcessTimeoutError, |
| | 137 | ]); |
| | 138 | export type ProcessRunError = typeof ProcessRunError.Type; |
| 129 | | 139 | |
| 130 | export class ProcessRunner extends Context.Service< | 140 | export class ProcessRunner extends Context.Service< |
| 131 | ProcessRunner, | 141 | ProcessRunner, |
| ... | @@ -163,6 +173,7 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { | ... | @@ -163,6 +173,7 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 163 | readonly command: string; | 173 | readonly command: string; |
| 164 | readonly args: ReadonlyArray<string>; | 174 | readonly args: ReadonlyArray<string>; |
| 165 | readonly cwd?: string | undefined; | 175 | readonly cwd?: string | undefined; |
| | 176 | readonly spawnCwd?: string | undefined; |
| 166 | readonly streamName: "stdout" | "stderr"; | 177 | readonly streamName: "stdout" | "stderr"; |
| 167 | readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>; | 178 | readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>; |
| 168 | readonly maxOutputBytes: number; | 179 | readonly maxOutputBytes: number; |
| ... | @@ -174,8 +185,9 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { | ... | @@ -174,8 +185,9 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 174 | (cause) => | 185 | (cause) => |
| 175 | new ProcessReadError({ | 186 | new ProcessReadError({ |
| 176 | command: input.command, | 187 | command: input.command, |
| 177 | args: input.args, | 188 | argumentCount: input.args.length, |
| 178 | cwd: input.cwd, | 189 | cwd: input.cwd, |
| | 190 | spawnCwd: input.spawnCwd, |
| 179 | stream: input.streamName, | 191 | stream: input.streamName, |
| 180 | cause, | 192 | cause, |
| 181 | }), | 193 | }), |
| ... | @@ -203,14 +215,16 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { | ... | @@ -203,14 +215,16 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 203 | () => ({ chunks: [], bytes: 0 }), | 215 | () => ({ chunks: [], bytes: 0 }), |
| 204 | (state, chunk) => { | 216 | (state, chunk) => { |
| 205 | const remainingBytes = input.maxOutputBytes - state.bytes; | 217 | const remainingBytes = input.maxOutputBytes - state.bytes; |
| 206 | if (remainingBytes <= 0 || chunk.byteLength > remainingBytes) { | 218 | if (chunk.byteLength > remainingBytes) { |
| 207 | return Effect.fail( | 219 | return Effect.fail( |
| 208 | new ProcessOutputLimitError({ | 220 | new ProcessOutputLimitError({ |
| 209 | command: input.command, | 221 | command: input.command, |
| 210 | args: input.args, | 222 | argumentCount: input.args.length, |
| 211 | cwd: input.cwd, | 223 | cwd: input.cwd, |
| | 224 | spawnCwd: input.spawnCwd, |
| 212 | stream: input.streamName, | 225 | stream: input.streamName, |
| 213 | maxBytes: input.maxOutputBytes, | 226 | maxBytes: input.maxOutputBytes, |
| | 227 | observedBytes: state.bytes + chunk.byteLength, |
| 214 | }), | 228 | }), |
| 215 | ); | 229 | ); |
| 216 | } | 230 | } |
| ... | @@ -259,8 +273,9 @@ function finalizeRunProcess<R>( | ... | @@ -259,8 +273,9 @@ function finalizeRunProcess<R>( |
| 259 | return Effect.fail( | 273 | return Effect.fail( |
| 260 | new ProcessTimeoutError({ | 274 | new ProcessTimeoutError({ |
| 261 | command: input.command, | 275 | command: input.command, |
| 262 | args: input.args, | 276 | argumentCount: input.args.length, |
| 263 | cwd: input.cwd, | 277 | cwd: input.cwd, |
| | 278 | spawnCwd: input.spawnCwd, |
| 264 | timeoutMs: Duration.toMillis(timeout), | 279 | timeoutMs: Duration.toMillis(timeout), |
| 265 | }), | 280 | }), |
| 266 | ); | 281 | ); |
| ... | @@ -300,23 +315,30 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( | ... | @@ -300,23 +315,30 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 300 | (cause) => | 315 | (cause) => |
| 301 | new ProcessSpawnError({ | 316 | new ProcessSpawnError({ |
| 302 | command: input.command, | 317 | command: input.command, |
| 303 | args: input.args, | 318 | argumentCount: input.args.length, |
| 304 | cwd: input.cwd, | 319 | cwd: input.cwd, |
| | 320 | spawnCwd: input.spawnCwd, |
| | 321 | resolvedCommand: spawnCommand.command, |
| | 322 | resolvedArgumentCount: spawnCommand.args.length, |
| | 323 | shell: spawnCommand.shell, |
| 305 | cause, | 324 | cause, |
| 306 | }), | 325 | }), |
| 307 | ), | 326 | ), |
| 308 | ); | 327 | ); |
| 309 | | 328 | |
| | 329 | const stdin = input.stdin; |
| 310 | const writeStdin = | 330 | const writeStdin = |
| 311 | input.stdin === undefined | 331 | stdin === undefined |
| 312 | ? Effect.void | 332 | ? Effect.void |
| 313 | : Stream.run(Stream.encodeText(Stream.make(input.stdin)), child.stdin).pipe( | 333 | : Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe( |
| 314 | Effect.mapError( | 334 | Effect.mapError( |
| 315 | (cause) => | 335 | (cause) => |
| 316 | new ProcessStdinError({ | 336 | new ProcessStdinError({ |
| 317 | command: input.command, | 337 | command: input.command, |
| 318 | args: input.args, | 338 | argumentCount: input.args.length, |
| 319 | cwd: input.cwd, | 339 | cwd: input.cwd, |
| | 340 | spawnCwd: input.spawnCwd, |
| | 341 | stdinBytes: Buffer.byteLength(stdin), |
| 320 | cause, | 342 | cause, |
| 321 | }), | 343 | }), |
| 322 | ), | 344 | ), |
| ... | @@ -328,6 +350,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( | ... | @@ -328,6 +350,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 328 | command: input.command, | 350 | command: input.command, |
| 329 | args: input.args, | 351 | args: input.args, |
| 330 | cwd: input.cwd, | 352 | cwd: input.cwd, |
| | 353 | spawnCwd: input.spawnCwd, |
| 331 | streamName: "stdout", | 354 | streamName: "stdout", |
| 332 | stream: child.stdout, | 355 | stream: child.stdout, |
| 333 | maxOutputBytes, | 356 | maxOutputBytes, |
| ... | @@ -338,6 +361,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( | ... | @@ -338,6 +361,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 338 | command: input.command, | 361 | command: input.command, |
| 339 | args: input.args, | 362 | args: input.args, |
| 340 | cwd: input.cwd, | 363 | cwd: input.cwd, |
| | 364 | spawnCwd: input.spawnCwd, |
| 341 | streamName: "stderr", | 365 | streamName: "stderr", |
| 342 | stream: child.stderr, | 366 | stream: child.stderr, |
| 343 | maxOutputBytes, | 367 | maxOutputBytes, |
| ... | @@ -354,8 +378,9 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( | ... | @@ -354,8 +378,9 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 354 | (cause) => | 378 | (cause) => |
| 355 | new ProcessReadError({ | 379 | new ProcessReadError({ |
| 356 | command: input.command, | 380 | command: input.command, |
| 357 | args: input.args, | 381 | argumentCount: input.args.length, |
| 358 | cwd: input.cwd, | 382 | cwd: input.cwd, |
| | 383 | spawnCwd: input.spawnCwd, |
| 359 | stream: "exitCode", | 384 | stream: "exitCode", |
| 360 | cause, | 385 | cause, |
| 361 | }), | 386 | }), |