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

std_output / std_error is not always captured as expected #21410

Open
ttytm opened this issue May 3, 2024 · 4 comments
Open

std_output / std_error is not always captured as expected #21410

ttytm opened this issue May 3, 2024 · 4 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: vlib Bugs/feature requests, that are related to the vlib.

Comments

@ttytm
Copy link
Member

ttytm commented May 3, 2024

Describe the bug

I can't get the expected output of some system commands whose output I want to reuse after executing them in a V program.

I'm running into this when using os.execute or using an os.Process.

Reproduction Steps

  • V on Windows

    // main.v
    import os
    
    println(os.execute('fc /lnwt mod.txt mod2.txt').output) // Prints a blank line.
    // os.system('fc /lnwt mod.txt mod2.txt') // Results in the correct output in the terminal.
  • python example

    # main.py
    import subprocess
    
    def run_fc(file1, file2):
       cmd = f'fc /lnwt {file1} {file2}'
       result = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
       return result.stdout.decode()
    
    output = run_fc('mod.txt', 'mod2.txt')
    print(output) # Prints the expected output.
  • other V example (not limited to windows):

    println(os.execute('${@VEXE} -prod run ${@VEXEROOT}/examples/hello_world.v').output)
    /*
    ❯ v run main.v
    Hello, World!
    
    */
    
    os.system('${@VEXE} -prod run ${@VEXEROOT}/examples/hello_world.v')
    /*
    ❯ v run main.v
    Note: building an optimized binary takes much longer. It shouldn't be used with `v run`.
    Use `v run` without optimization, or build an optimized binary with -prod first, then run it separately.
    Hello, World!
    */

Expected Behavior

Full std_output and std_error in output

Current Behavior

No or limited string in output.

Possible Solution

No response

Additional Information/Context

No response

V version

weekly.2024.18.2

Environment details (OS name and version, etc.)

x64 linux, windows 11 VM

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@ttytm ttytm added the Bug This tag is applied to issues which reports bugs. label May 3, 2024
@felipensp felipensp added the Unit: vlib Bugs/feature requests, that are related to the vlib. label May 4, 2024
@spytheman
Copy link
Member

Try reverting c7fb755 locally, to see if that will help?

It is a recent change to os.execute .

@spytheman
Copy link
Member

Although c7fb755 was in vlib/os/os_nix.c.v, and you are reporting it for windows 🤔 .
Perhaps we should investigate the vlib/os/os_windows.c.v version more instead.

@spytheman
Copy link
Member

Does os.raw_execute work?

@ttytm
Copy link
Member Author

ttytm commented May 5, 2024

Thanks for looking into it @spytheman.

Can't get it working when reverting.

raw_execute doesn't work either. execute is very close to raw_execute on windows

v/vlib/os/os_windows.c.v

Lines 289 to 297 in 58527cc

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'
}
}
return unsafe { raw_execute(cmd) }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: vlib Bugs/feature requests, that are related to the vlib.
Projects
None yet
Development

No branches or pull requests

3 participants