From 9f49b16d3943bef097173505341e54a16226de44 Mon Sep 17 00:00:00 2001 From: Tsuf Cohen <39455181+tsuf239@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:40:40 +0300 Subject: [PATCH] fix(sdk): unable to use `util.exec` in preflight (#7019) fixes: #6915 ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- examples/tests/sdk_tests/util/exec.test.w | 6 ++++++ libs/wingsdk/src/util/util.ts | 13 ++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/tests/sdk_tests/util/exec.test.w b/examples/tests/sdk_tests/util/exec.test.w index 83615488410..564328e3e52 100644 --- a/examples/tests/sdk_tests/util/exec.test.w +++ b/examples/tests/sdk_tests/util/exec.test.w @@ -13,6 +13,12 @@ let assertThrows = inflight (expected: str, block: (): void) => { assert(error); }; +let output1 = util.exec("echo", ["-n", "Hello, Wing!"]); + +expect.equal(output1.stdout, "Hello, Wing!"); +expect.equal(output1.stderr, ""); +expect.equal(output1.status, 0); + test "exec()" { // "exec() with valid program" diff --git a/libs/wingsdk/src/util/util.ts b/libs/wingsdk/src/util/util.ts index 61bc84c903f..633f8eeffc2 100644 --- a/libs/wingsdk/src/util/util.ts +++ b/libs/wingsdk/src/util/util.ts @@ -1,4 +1,4 @@ -import { exec, execFile } from "child_process"; +import { exec, execFileSync } from "child_process"; import { createHash } from "crypto"; import { promisify } from "util"; import { nanoid, customAlphabet } from "nanoid"; @@ -9,7 +9,6 @@ import { InflightClient } from "../core"; import { Duration, IInflight } from "../std"; const execPromise = promisify(exec); -const execFilePromise = promisify(execFile); /** * Describes what to do with a standard I/O stream for a child process. @@ -223,11 +222,11 @@ export class Util { * @param opts `ExecOptions`, such as the working directory and environment variables. * @returns A struct containing `stdout`, `stderr` and exit `status` of the executed program. */ - public static async exec( + public static exec( program: string, args: Array, opts?: ExecOptions - ): Promise { + ): Output { const execOpts = { windowsHide: true, shell: false, @@ -239,10 +238,10 @@ export class Util { }; try { - const { stdout, stderr } = await execFilePromise(program, args, execOpts); + const stdout = execFileSync(program, args, execOpts); return { stdout: stdout.toString(), - stderr: stderr.toString(), + stderr: "", status: 0, }; } catch (error: any) { @@ -252,7 +251,7 @@ export class Util { return { stdout: error.stdout.toString(), stderr: error.stderr.toString(), - status: error.code, + status: error.status, }; } }