fsprojects / FAKE

FAKE - F# Make
https://fake.build
Other
1.28k stars 581 forks source link

Basic usage of CreateProcess throws InvalidOperationException in .NET4.7.2 #2670

Closed knocte closed 1 year ago

knocte commented 2 years ago

Description

Basic usage of CreateProcess throws exception in .NET4.7.2. There seems to be a problem with how FAKE.Core.Process implementation makes use of the StandardOutputEncoding API in .NET4.x (as opposed to .NET Core/5/newer).

Repro steps

  1. Create a new F# 4.7.2 project.
  2. Reference Fake.Core.Process nuget package (at the time of writing this, last version is 5.22.0).
  3. Write some basic code to make use of CreateProcess, e.g.:
open System

open Fake.Core

[<EntryPoint>]
let main argv =
    CreateProcess.fromRawCommand "/bin/date" []
    |> Proc.run // start with the above configuration
    |> ignore // ignore exit code
    0 // return an integer exit code
  1. Compile and run the code.

Expected behavior

It should just print the date.

Actual behavior

It throws this exception:

Unhandled Exception:
System.Exception: Start of process '/bin/date' failed. ---> System.InvalidOperationException: StandardOutputEncoding is only supported when standard output is redirected.
  at System.Diagnostics.Process.StartWithCreateProcess (System.Diagnostics.ProcessStartInfo startInfo) [0x0001a] in <81c889a3fdaf408aacac8a870695982b>:0 
  at System.Diagnostics.Process.Start () [0x0003a] in <81c889a3fdaf408aacac8a870695982b>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
  at Fake.Core.Process+processStarter@415.Invoke (Fake.Core.RawCreateProcess c, System.Diagnostics.Process p) [0x00111] in <34f08ec40d37a4db8166ddbaf329b817>:0 
   --- End of inner exception stack trace ---
  at Fake.Core.Process+processStarter@415.Invoke (Fake.Core.RawCreateProcess c, System.Diagnostics.Process p) [0x0015c] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Fake.Core.RawProc.start@207 (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] startProcessRaw, Fake.Core.RawCreateProcess c, System.Diagnostics.ProcessStartInfo p, Fake.Core.StreamSpecs streamSpec, System.Diagnostics.Process toolProcess, System.Threading.CancellationTokenSource tok, Microsoft.FSharp.Core.FSharpRef`1[T] isStarted, Microsoft.FSharp.Core.FSharpRef`1[T] readOutputTask, Microsoft.FSharp.Core.FSharpRef`1[T] readErrorTask, Microsoft.FSharp.Core.FSharpRef`1[T] redirectStdInTask, Microsoft.FSharp.Core.Unit unitVar0) [0x0001f] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Fake.Core.RawProc+syncStart@268.Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00001] in <34f08ec40d37a4db8166ddbaf329b817>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.AsyncPrimitives.QueueAsyncAndWaitForResultSynchronously[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000de] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x00018] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x00075] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Fake.Core.Proc.run[a] (Fake.Core.CreateProcess`1[TRes] c) [0x00005] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Program.main (System.String[] argv) [0x0000f] in <62734956d6c15739a745038356497362>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Start of process '/bin/date' failed. ---> System.InvalidOperationException: StandardOutputEncoding is only supported when standard output is redirected.
  at System.Diagnostics.Process.StartWithCreateProcess (System.Diagnostics.ProcessStartInfo startInfo) [0x0001a] in <81c889a3fdaf408aacac8a870695982b>:0 
  at System.Diagnostics.Process.Start () [0x0003a] in <81c889a3fdaf408aacac8a870695982b>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
  at Fake.Core.Process+processStarter@415.Invoke (Fake.Core.RawCreateProcess c, System.Diagnostics.Process p) [0x00111] in <34f08ec40d37a4db8166ddbaf329b817>:0 
   --- End of inner exception stack trace ---
  at Fake.Core.Process+processStarter@415.Invoke (Fake.Core.RawCreateProcess c, System.Diagnostics.Process p) [0x0015c] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Fake.Core.RawProc.start@207 (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] startProcessRaw, Fake.Core.RawCreateProcess c, System.Diagnostics.ProcessStartInfo p, Fake.Core.StreamSpecs streamSpec, System.Diagnostics.Process toolProcess, System.Threading.CancellationTokenSource tok, Microsoft.FSharp.Core.FSharpRef`1[T] isStarted, Microsoft.FSharp.Core.FSharpRef`1[T] readOutputTask, Microsoft.FSharp.Core.FSharpRef`1[T] readErrorTask, Microsoft.FSharp.Core.FSharpRef`1[T] redirectStdInTask, Microsoft.FSharp.Core.Unit unitVar0) [0x0001f] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Fake.Core.RawProc+syncStart@268.Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00001] in <34f08ec40d37a4db8166ddbaf329b817>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.AsyncPrimitives.QueueAsyncAndWaitForResultSynchronously[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000de] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x00018] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x00075] in <bd23e21aaecbcc7d8c8e86759c064403>:0 
  at Fake.Core.Proc.run[a] (Fake.Core.CreateProcess`1[TRes] c) [0x00005] in <34f08ec40d37a4db8166ddbaf329b817>:0 
  at Program.main (System.String[] argv) [0x0000f] in <62734956d6c15739a745038356497362>:0

Known workarounds

Use .NET5 instead of .NET4.7.2.

Related information