Issue with consuming DLL when using LangVersion set to 8.0 with .NET SDK 9
- Dominant language
- F#
- Stars
- 4.3k
- Forks
- 876
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 131
Description
Recently I changed a large F# code base to work with .NET SDK 9 and F# 9. To start with I didn't change `LangVersion` which gave me a lot of errors of the type:
```
FSC : error FS0229: Error opening binary file 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': Error reading/writing metadata for the F# compiled DLL 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll'. Was the DLL compiled with an earlier version of the F# compiler? (error: 'u_ty - 1/B, byte = 1').
FSC : error FS3160: Problem reading assembly 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': The exception has been reported. This internal exception should now be caught at an error recovery point on the stack. Original message: Error opening binary file 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': Error reading/writing metadata for the F# compiled DLL 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll'. Was the DLL compiled with an earlier version of the F# compiler? (error: 'u_ty - 1/B, byte = 1').)
```
**Repro steps**
Use .NET SDK 9.
Have a project that has `LangVersion` set to `8.0`:
````
8.0
net9.0
````
Have the following code in `Library.fs`:
```
module MyModule
let convert (lookup: 'a -> 'b) (id: 'a): unit =
lookup id
|> Option.ofObj
|> ignore
```
Have a project that has a project reference to the project above. It doesn't matter what `LangVersion` or `TargetFramework` that project has.
A zip-file with this example is attached. Running `dotnet build` results in errors.
[fsharp-error.zip](https://github.com/user-attachments/files/17749805/fsharp-error.zip)
**Expected behavior**
It is possible to build the projects without errors.
**Actual behavior**
I get errors of the following type:
```
FSC : error FS0229: Error opening binary file 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': Error reading/writing metadata for the F# compiled DLL 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll'. Was the DLL compiled with an earlier version of the F# compiler? (error: 'u_ty - 1/B, byte = 1').
FSC : error FS3160: Problem reading assembly 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': The exception has been reported. This internal exception should now be caught at an error recovery point on the stack. Original message: Error opening binary file 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll': Error reading/writing metadata for the F# compiled DLL 'C:\repos\fsharp-test\ClassLibrary1\obj\Debug\net8.0\ref\ClassLibrary1.dll'. Was the DLL compiled with an earlier version of the F# compiler? (error: 'u_ty - 1/B, byte = 1').)
```
**Known workarounds**
Set `LangVersion` to `9.0` or use .NET SDK 8
**Related information**
A similar problem was discussed in https://github.com/dotnet/fsharp/issues/14313.
This issue is somewhat different I think. Here you get errors even if the same target framework is used for both projects. It is only `LangVersion` that cause the problem.
With .NET SDK 8.0.403 it works with `LangVersion` set both to `7.0` and `8.0`.
I have a really large F# code base, and is only the kind of construct that I have in `Library.fs` that causes problems.
* Windows 11
* .NET SDK 9.0.100
Contributor guide
Assessment
This issue has not been assessed yet.