Process.start being used to launch browser but is now deprecated

208 Views Asked by At

If I go through the "How to start" guide for Saturn:

https://saturnframework.org/tutorials/how-to-start.html

when I run the last step:

dotnet fake build -t run

the webserver does indeed appear to start. I can go to http://localhost:8085/books and see the resulting application.

However, if I ctrl-c at the console to stop the webserver, I notice the following:

enter image description here

If I look at build.fsx, it contains the following line:

Process.start (fun i -> { i with FileName = "http://localhost:8085" }) |> ignore

within the Run target:

Target.create "Run" (fun _ ->
  let server = async {
    DotNet.exec (fun p -> { p with WorkingDirectory = appPath } ) "watch" "run" |> ignore
  }
  let browser = async {
    Thread.Sleep 5000
    Process.start (fun i -> { i with FileName = "http://localhost:8085" }) |> ignore
  }

  [ server; browser]
  |> Async.Parallel
  |> Async.RunSynchronously
  |> ignore
)

So it looks like the intention of the Run target is to start a browser for us.

The warning in the output above indicates:

Warning FS0044: This construct is deprecated. use the CreateProcess APIs instead.

The Just start a process page in the Fake documentation has some examples. Going off those, I used the changed build.fsx to use the following line:

CreateProcess.fromRawCommand "cmd.exe" [ "/C"; "start http://localhost:8085" ] |> Proc.run |> ignore

And that does indeed seem to work.

Couple of questions:

  • Is that the recommended way to achieve something like this?
  • The above can only be expected to work on Windows. Is there a cross-platform way to open a URL with a browser using Fake?

Note

If you decide to go through the Saturn "How to start" guide, I recommend that you remove the file global.json after step 3. The default one currently looks for a particular version of .NET SDK. There is a pull-request open to resolve this.

Thanks!

1

There are 1 best solutions below

2
On