Skip to content

FSharp.Compiler.Service: TypeInitializationException in GetProjectOptionsFromScript in published console application #11966

@MortalFlesh

Description

@MortalFlesh

Hello,

The bug was originally here fsharp/fsharp-compiler-docs#965

I have a project which uses a FSharp.Compiler.Service to parse .fsx files as a Domain specification and then uses results to check whether a use cases are correct (https://github.com/TypedUseCase/).

Recently I've came across this bug (not sure when it happened and why - I changed a lot after a last successful run - dotnet core, dependencies, ...).

Problem is in a self-contained published binary (after dotnet publish) - if I run a code by just dotnet run it works fine.

In a published code run I get this error on GetProjectOptionsFromScript call:

System.TypeInitializationException: The type initializer for '<StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults' threw an exception.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at <StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults..cctor()
   --- End of inner exception stack trace ---
   at FSharp.Compiler.CodeAnalysis.FSharpCheckerResultsSettings.get_defaultFSharpBinariesDir()
   at <StartupCode$FSharp-Compiler-Service>.$Service.e@822-10(BackgroundCompiler _, FSharpOption`1 useSdkRefs, FSharpOption`1 useFsiAuxLib, ISourceText sourceText, FSharpOption`1 sdkDirOverride, FSharpOption`1 previewEnabled, FSharpOption`1 otherFlags, FSharpOption`1 optionsStamp, FSharpOption`1 loadedTimeStamp, String filename, FSharpOption`1 assumeDotNetFramework, CompilationThreadToken ctok, ErrorScope _arg36)
   at <StartupCode$FSharp-Compiler-Service>.$Service.f@826-139(BackgroundCompiler _, FSharpOption`1 useSdkRefs, FSharpOption`1 useFsiAuxLib, ISourceText sourceText, FSharpOption`1 sdkDirOverride, FSharpOption`1 previewEnabled, FSharpOption`1 otherFlags, FSharpOption`1 optionsStamp, FSharpOption`1 loadedTimeStamp, String filename, FSharpOption`1 assumeDotNetFramework, CompilationThreadToken ctok, Unit unitVar)
   at <StartupCode$FSharp-Compiler-Service>.$Service.GetProjectOptionsFromScript@871-1.Invoke(CancellationToken ct)
   at <StartupCode$FSharp-Compiler-Service>.$Reactor.EnqueueAndAwaitOpAsync@220-2.Invoke(CompilationThreadToken ctok)
--- End of stack trace from previous location ---
   at Microsoft.FSharp.Control.AsyncResult`1.Commit()
   at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInAnotherThread[a](CancellationToken token, FSharpAsync`1 computation, FSharpOption`1 timeout)
   at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T](CancellationToken cancellationToken, FSharpAsync`1 computation, FSharpOption`1 timeout)
   at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T](FSharpAsync`1 computation, FSharpOption`1 timeout, FSharpOption`1 cancellationToken)
   at Program.DomainResolverLibrary.parseAndCheck(FSharpChecker checker, String file, String contents)

Repro steps

I've created a simple repo with a minimal code, which shows the error.
https://github.com/MortalFlesh/fscs-bug-example

There are also github actions with the runtime example:
https://github.com/MortalFlesh/fscs-bug-example/actions/runs/1127856732

I've described more details in the repo readme - I can copy it here, if it is needed.

Expected behavior

It should return a Project options as normal.

Actual behavior

In published code, it fails on The type initializer for '<StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults' threw an exception.

Known workarounds

I don't know any workarounds.

Related information

.NET SDK (reflecting any global.json):
 Version:   5.0.400
 Commit:    d61950f9bf

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  11.0
 OS Platform: Darwin
 RID:         osx.11.0-x64
 Base Path:   /usr/local/share/dotnet/sdk/5.0.400/

Host (useful for support):
  Version: 6.0.0-preview.4.21253.7
  Commit:  bfd6048a60

.NET SDKs installed:
  5.0.103 [/usr/local/share/dotnet/sdk]
  5.0.200 [/usr/local/share/dotnet/sdk]
  5.0.300 [/usr/local/share/dotnet/sdk]
  5.0.301 [/usr/local/share/dotnet/sdk]
  5.0.302 [/usr/local/share/dotnet/sdk]
  5.0.400 [/usr/local/share/dotnet/sdk]
  6.0.100-preview.4.21255.9 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.4.21253.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.4.21253.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FCSBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions