6

UPD.:

I want to invoke F# compiler (i.e. fsc) from .NET Core SDK directly.

I know about dotnet build & co, but I don't want to involve them when I only need to compile a simple problem, i.e. in cases when fsc file.fs would be enough.

I've tried to search in .NET Core SDK (on my Mac, it was in /usr/local/share/dotnet/sdk/2.2.102/FSharp) and found a fsc.exe file there. Unfortunately, when I try to start it with dotnet /usr/local/share/dotnet/sdk/2.2.102/FSharp/fsc.exe, it gives me an error:

error FS0193: Could not load file or assembly 'Microsoft.Build.Tasks.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Env.: macOS, .NET Core 2.2

isnullxbh
  • 807
  • 13
  • 20
  • If you votes in the negative - explain it, please. I don't see why. – isnullxbh Jul 01 '19 at 12:55
  • Obviously you need to run it with `dotnet fsc.exe`. But after I've done that, it started to complain about `Microsoft.Build.Tasks.v4.0`. I don't know how to proceed. – ForNeVeR Jul 01 '19 at 13:13
  • @ForNeVeR, but I would like to use fsc like the javac. It seems it is not possible without some hacks. – isnullxbh Jul 01 '19 at 13:18
  • @isnullxbh Note that F# doesn't do separate compilation (ie compiling several `.fs` files separately and then linking the results together into an output binary) so there isn't much point in calling `fsc` by hand. – Tarmil Jul 01 '19 at 13:25
  • 1
    The steps for installing fsc on MacOs are pretty well [documented on the F# website](https://fsharp.org/use/mac/). Have you tried the steps listed there? – Jamie Taylor Jul 01 '19 at 15:16

3 Answers3

5

You probably don't want to call fsc directly, since dotnet build and fsproj is the preferred way of building projects.

If you must though, then it can be found in the SDK files:

dotnet /usr/share/dotnet/sdk/5.0.100/FSharp/fsc.exe

Use dotnet --list-sdks to find the full path to your SDK.

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
3

After install dotnet core (https://dotnet.microsoft.com/download) you should be able to build a F# project from the command line.

cd my-fs-project
dotnet build
dotnet run
  • Thanks for your help! Can I use `fsc` without `dotnet`, like the java compiler (javac)? – isnullxbh Jul 01 '19 at 13:21
  • 3
    You probably can, but people who use it primarily use dotnet. Your ability to get help will be less than if you follow the crowd. Trying to recreate your experience in java will likely be a mistake. Java is the best java, F# is the best F#. – VoronoiPotato Jul 01 '19 at 13:51
  • This is the correct way to build F# for most use cases but it does not actually answer the question. – sdgfsdh Feb 05 '20 at 17:42
2

I compile using fsc on Linux in following way.

From .NET 6 (I'm on Gentoo, but you can infer path)

dotnet /opt/dotnet-sdk-bin-6.0/sdk/6.0.101/FSharp/fsc.dll test.fs --targetprofile:netcore --target:exe --out:test.exe

from locally build fsc

artifacts/bin/fsc/Debug/net5.0/fsc test.fs --targetprofile:netcore --target:exe --out:test.exe

Then I manually create test.runtimeconfig.json with following content

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    },
    "rollForwardOnNoCandidateFx": 2
  }
}

Then I was able to run application using dotnet test.exe.

codevision
  • 5,165
  • 38
  • 50
  • Thanks for the answer! I don't have fsc (as a separate executable file) on my machine. Can you run "which fsc" and attach its output? – isnullxbh Jan 23 '22 at 10:26
  • @isnullxbh Honesly I'm on wierd distro, and I use locally build FSC. I add instruction how to build using stock .NET 6. You probably should use paths from sibling answer https://stackoverflow.com/a/65113119/354473 – codevision Jan 24 '22 at 11:16