v icon indicating copy to clipboard operation
v copied to clipboard

std_output / std_error is not always captured as expected

Open ttytm opened this issue 1 year ago • 4 comments

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 avatar May 03 '24 12:05 ttytm

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

It is a recent change to os.execute .

spytheman avatar May 05 '24 16:05 spytheman

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 avatar May 05 '24 16:05 spytheman

Does os.raw_execute work?

spytheman avatar May 05 '24 17:05 spytheman

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

https://github.com/vlang/v/blob/58527cc71c49d8d77d99ddf06a26dab0e1ae00bf/vlib/os/os_windows.c.v#L289-L297

ttytm avatar May 05 '24 19:05 ttytm