Description
This issue documents how to enable the use of type providers when using a .NET SDK 2.0 "new style" project file.
This also allows the use of cross-targeting type providers as part of .NET Core 2.0 app programming or .NET Standard 2.0 library programming.
If you don't follow one of the workarounds below you may get a silent failure or RunFsc.cmd failed
from the F# compiler when executing dotnet build
or "StackOverflowException", if your project references a type provider.
What you need to do
-
Install an F# Compiler that runs on .NET Framework or Mono, see Linux, Windows, OSX. It is likely you already have one installed.
-
Add the file
fsc.props
to the root of your project and import it into each of your project files that reference a type provider using something along the lines of:<Import Project="fsc.props" />
,
The F# compiler will then run using .NET Framework and/or Mono.
Alternative Workaround
On Windows:
- Install Visual Studio 2017 15.3 or later
- Use "msbuild.exe" from a Visual Studio 2017 command line prompt to build the project instead of "dotnet build"
On Linux/OSX/Xamarin/Mono:
- Install Mono 5.0 or later
- Use "msbuild" to build the project instead of "dotnet build"
Explanation: when you compile your project using msbuild.exe
the F# support in the .NET SDK will assume .NET Framework execution of the F# compiler is required. This workaround is also compatible with using an fsc.props
file as mentioned above.
Examples where the problem occurs
Example 1 (building a .NET 4.6.1 application that uses FSharp.Data)
dotnet new console -o -lang F# --target-framework-override net461 app3
cd app3
dotnet add package FSharp.Data
dotnet restore
dotnet build
Example 2 (building a .NET Core 2.0 application that uses FSharp.Data)
dotnet new console -o -lang F# app4
cd app4
dotnet add package FSharp.Data
dotnet restore
dotnet build
Expected behaviour is compilation success, or at least nice error point to workaround. Actual behavior is a silent compilation failure, and dotnet build /v:d
shows l Process is terminating due to StackOverflowException. Done executing task "Fsc" -- FAILED.
Explanation
As explained in this issue, type providers are not yet supported when the F# compiler runs using .NET Core. By default, the .NET SDK tooling runs the F# compiler using .NET Core.
The workaround is to have the .NET SDK tooling use an F# Compiler running on the .NET Framework or Mono, regardless of which version of .NET you're targeting. By doing this, the type provider is hosted and executed using the .NET Framework. This technique applies even if you are doing .NET Core programming (for cross-targeting type providers), or building a .NET Standard 2.0 library. The F# Compiler cross-generates code for the correct target regardless of how it executes.
Although this workaround may seem "wrong" if targeting .NET Core, it has advantages:
- the use of .NET Framework/Mono is only at compile-time
- the technique stress-tests the cross-targeting capabilities of the type provider
- the technique unblocks TP authors to test and adjust TPs to be cross-targeting
In the future, the plan of record is to allow .NET Standard 2.0 type providers to be used when the F# compiler is running on .NET Core. This is an interim workaround for those who wish to unblock the testing and use of type providers for .NET SDK project files and .NET Core/.NET Standard 2.0 programming.