Skip to content

Script does not compile with FSharp.Compiler.Service while dotnet fsi runs it just fine #17159

@kMutagene

Description

@kMutagene

Hi there, i am new to working with the compiler directly, so please bear with me if this issue does not contain all relevant information.

My basic use case is that i want to use the FSharp.Compiler.Service nuget package to verify whether the content of a script contains valid F# code. To do that, i followed this tutorial.

this compilation fails, while just running the script via dotnet fsi works without issues (meaning that there is actually nothing wrong with the code)

Repro steps

Above tutorial basically means calling

fsc.exe -o ./test.dll -a test.fsx

wrong, see below, i just thought it is also relevant that calling fsc.exe directly also fails.

The respective F# code using FSharp.Compiler.Service looks like this (i am using this in a xUnit test project):

type Assert with

    static member ScriptCompiles (scriptPath: string) =
        let t = Path.GetTempFileName()
        let tempPath = Path.ChangeExtension(t, ".dll")
        let checker = FSharpChecker.Create()
        let errors, exitCode =
            checker.Compile([| "fsc.exe"; "-o"; tempPath; "-a"; scriptPath |]) 
            |> Async.RunSynchronously
        Assert.Empty(errors)
        Assert.Equal(0, exitCode)

try to compile a script with this content (i removed much of the original content trying to narrow this issue down):

#r "nuget: ARCExpect, 2.0.0"

open ARCExpect
open System.IO

// Input:
let _ = Directory.GetCurrentDirectory()

printfn "yay"

Expected behavior

Script compiles (exitcode is 0 and error list is empty)

Actual behavior

this results in the following error for both, compiling via cli or compiling programmatically:

error FS0193: The module/namespace 'System.IO' from compilation unit 'System.Runtime' did not contain the namespace, module or type 'Directory'

but running it via dotnet fsi just works fine (also both via cli or programmatically):

❯ dotnet fsi ./test.fsx
yay

Known workarounds

use dotnet fsi to "verify" the script, however this is not a good workaround since this actually executes the code, but i just want to know whether it could run

Related information

Provide any related information (optional):

  • Win 11
  • ❯ dotnet --version
    8.0.300
    
  • Editing Tools (e.g. Visual Studio Version, Visual Studio): script shows no errors when opening in either of those
  • <PackageReference Include="FSharp.Compiler.Service" Version="[43.8.300]" />

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions