From c7fb7553b75d344ce0a140583205295318d070eb Mon Sep 17 00:00:00 2001 From: Henrik Holst <6200749+hholst80@users.noreply.github.com> Date: Thu, 2 May 2024 17:16:02 -0400 Subject: [PATCH] os: fix os.execute stderr redirection (fix #20986) (#21404) --- vlib/os/os_nix.c.v | 5 +---- vlib/os/os_test.c.v | 10 ++++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 4cb8068c42..a49e135144 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -339,10 +339,7 @@ pub fn mkdir(path string, params MkdirParams) ! { // execute starts the specified command, waits for it to complete, and returns its output. @[manualfree] pub fn execute(cmd string) Result { - // if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') { - // return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' } - // } - pcmd := if cmd.contains('2>') { cmd.clone() } else { '${cmd} 2>&1' } + pcmd := 'exec 2>&1;${cmd}' defer { unsafe { pcmd.free() } } diff --git a/vlib/os/os_test.c.v b/vlib/os/os_test.c.v index daea4e69e8..135f1bc733 100644 --- a/vlib/os/os_test.c.v +++ b/vlib/os/os_test.c.v @@ -907,6 +907,16 @@ fn test_execute_with_stderr_redirection() { assert os.exists(stderr_path) } +fn test_execute_with_linefeeds() { + if os.user_os() == 'windows' { + return + } + result := os.execute('true\n') + assert result.exit_code == 0 + result2 := os.execute('false\n') + assert result2.exit_code == 1 +} + fn test_command() { if os.user_os() == 'windows' { eprintln('>>> os.Command is not implemented fully on Windows yet')