dotnet / dotnet/fsharp

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

Open
#11,966 6 comments 0 reactions 0 assignees View on GitHub
Area-FCS Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Hello,

> The bug was originally here https://github.com/fsharp/FSharp.Compiler.Service/issues/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 '.$FSharpCheckerResults' threw an exception.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at .$FSharpCheckerResults..cctor()
--- End of inner exception stack trace ---
at FSharp.Compiler.CodeAnalysis.FSharpCheckerResultsSettings.get_defaultFSharpBinariesDir()
at .$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 .$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 .$Service.GetProjectOptionsFromScript@871-1.Invoke(CancellationToken ct)
at .$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 '.$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]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.