Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): unable to use util.exec in preflight #7019

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/tests/sdk_tests/util/exec.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -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);
tsuf239 marked this conversation as resolved.
Show resolved Hide resolved


test "exec()" {
// "exec() with valid program"
Expand Down
13 changes: 6 additions & 7 deletions libs/wingsdk/src/util/util.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved
args: Array<string>,
opts?: ExecOptions
): Promise<Output> {
): Output {
const execOpts = {
windowsHide: true,
shell: false,
Expand All @@ -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,
Copy link
Contributor

@Chriscbr Chriscbr Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a TODO comment or open an issue to note this limitation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no real limitation I think- the stderr is thrown in the catch statement if exists- and passed to the user :)
I think that it yields the same result of the promisify function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsuf239 I thought even if a command succeeds it can still emit to both stdout and stderr?

};
} catch (error: any) {
Expand All @@ -252,7 +251,7 @@ export class Util {
return {
stdout: error.stdout.toString(),
stderr: error.stderr.toString(),
status: error.code,
status: error.status,
};
}
}
Expand Down
Loading