Skip to content

System.Text.Json code gen: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0 #58502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jericho opened this issue Sep 1, 2021 · 10 comments
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@Jericho
Copy link

Jericho commented Sep 1, 2021

I upgraded to the latest daily System.Text.Json nuget package (6.0.0-rc.2.21451.3) and my project no longer builds.

Visual Studio 2022 is really not helpful. It simply says:

Rebuild started...
1>------ Rebuild All started: Project: MyLibrary, Configuration: Debug Any CPU ------
Restored D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj (in 113 ms).
Restored D:\_build\MyLibrary\Source\MyLibrary.UnitTests\MyLibrary.UnitTests.csproj (in 139 ms).
1>D:\_build\MyLibrary\Source\MyLibrary\Utilities\MyJsonSerializerContext.cs(180,25,180,56): error CS0534: 'MyJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>Done building project "MyLibrary.csproj" -- FAILED.
2>------ Rebuild All started: Project: MyLibrary.UnitTests, Configuration: Debug Any CPU ------
2>CSC : error CS0006: Metadata file 'D:\_build\MyLibrary\Source\MyLibrary\bin\Debug\net5.0\ref\MyLibrary.dll' could not be found
2>Done building project "MyLibrary.UnitTests.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========

However, if I invoke dotnet.exe build from command prompt, I get a more helpful message:

CSC : error CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Users\desau\.nuget\packages\system.text.json\6.0.0-rc.2.21451.3\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj]

I added <PackageReference Include="Microsoft.CodeAnalysis" Version="4.0.0-3.final" /> to my csproj but unfortunately it didn't help.

@ghost ghost added area-Infrastructure-libraries untriaged New issue has not been triaged by the area owner labels Sep 1, 2021
@ghost
Copy link

ghost commented Sep 1, 2021

Tagging subscribers to this area: @Anipik, @safern, @ViktorHofer
See info in area-owners.md if you want to be subscribed.

Issue Details

I upgraded to the latest daily System.Text.Json nuget package (6.0.0-rc.2.21451.3) and my project no longer builds.

Visual Studio 2022 is really not helpful. It simply says:

Rebuild started...
1>------ Rebuild All started: Project: MyLibrary, Configuration: Debug Any CPU ------
Restored D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj (in 113 ms).
Restored D:\_build\MyLibrary\Source\MyLibrary.UnitTests\MyLibrary.UnitTests.csproj (in 139 ms).
1>D:\_build\MyLibrary\Source\MyLibrary\Utilities\MyJsonSerializerContext.cs(180,25,180,56): error CS0534: 'MyJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>Done building project "MyLibrary.csproj" -- FAILED.
2>------ Rebuild All started: Project: MyLibrary.UnitTests, Configuration: Debug Any CPU ------
2>CSC : error CS0006: Metadata file 'D:\_build\MyLibrary\Source\MyLibrary\bin\Debug\net5.0\ref\MyLibrary.dll' could not be found
2>Done building project "MyLibrary.UnitTests.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========

However, if I invoke dotnet.exe build from command prompt, I get a more helpful message:

CSC : error CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Users\desau\.nuget\packages\system.text.json\6.0.0-rc.2.21451.3\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj]

I added <PackageReference Include="Microsoft.CodeAnalysis" Version="4.0.0-3.final" /> to my csproj but unfortunately it didn't help.

Author: Jericho
Assignees: -
Labels:

area-Infrastructure-libraries, untriaged

Milestone: -

@ghost
Copy link

ghost commented Sep 1, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

I upgraded to the latest daily System.Text.Json nuget package (6.0.0-rc.2.21451.3) and my project no longer builds.

Visual Studio 2022 is really not helpful. It simply says:

Rebuild started...
1>------ Rebuild All started: Project: MyLibrary, Configuration: Debug Any CPU ------
Restored D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj (in 113 ms).
Restored D:\_build\MyLibrary\Source\MyLibrary.UnitTests\MyLibrary.UnitTests.csproj (in 139 ms).
1>D:\_build\MyLibrary\Source\MyLibrary\Utilities\MyJsonSerializerContext.cs(180,25,180,56): error CS0534: 'MyJsonSerializerContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
1>Done building project "MyLibrary.csproj" -- FAILED.
2>------ Rebuild All started: Project: MyLibrary.UnitTests, Configuration: Debug Any CPU ------
2>CSC : error CS0006: Metadata file 'D:\_build\MyLibrary\Source\MyLibrary\bin\Debug\net5.0\ref\MyLibrary.dll' could not be found
2>Done building project "MyLibrary.UnitTests.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========

However, if I invoke dotnet.exe build from command prompt, I get a more helpful message:

CSC : error CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Users\desau\.nuget\packages\system.text.json\6.0.0-rc.2.21451.3\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.. [D:\_build\MyLibrary\Source\MyLibrary\MyLibrary.csproj]

I added <PackageReference Include="Microsoft.CodeAnalysis" Version="4.0.0-3.final" /> to my csproj but unfortunately it didn't help.

Author: Jericho
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

I tried adding System.Text.Json v6.0.0-rc.2.21451.3 to a source gen project but it seems to run without issue in both VS2022 and the CLI. My sdk version is 6.0.100-rc.2.21426.20. Would it be possible to share

  • your sdk version,
  • your Visual Studio version and
  • the reproducing project configuration?

@Jericho
Copy link
Author

Jericho commented Sep 2, 2021

Steps to repro

Step 1: create a new console application in VS 2022 and select net5.0

Step 2: add a reference to System.Text.Json 6.0.0-rc.2.21451.3

Confirm that your csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="6.0.0-rc.2.21451.3" />
  </ItemGroup>

</Project>

Step 3: replace the content of progem.cs with the following C# code:

using System;
using System.Text.Json.Serialization;

namespace GH58502
{
    public static class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }

[JsonSerializable(typeof(ClassA))]
    internal partial class MyJsonSerializerContext : JsonSerializerContext
    {
    }

    public class ClassA
    {
        [JsonPropertyName("id")]
        public string Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; }
    }
}

Step 4: add global.json with the following content:

  "sdk": {
    "version": "5.0.300",
    "rollForward": "latestFeature"
  }
}

Step 5: build from within VS 2022. You should see the following:
image

Alternatively, run the following command from command prompt: dotnet.exe build. You should see the following:
image

My environment
❯ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.400
 Commit:    ...snip...

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.400\

Host (useful for support):
  Version: 6.0.0-preview.7.21377.19
  Commit:  ...snip...

.NET SDKs installed:
  2.1.816 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]
  3.1.400 [C:\Program Files\dotnet\sdk]
  3.1.411 [C:\Program Files\dotnet\sdk]
  5.0.103 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]
  5.0.205 [C:\Program Files\dotnet\sdk]
  5.0.302 [C:\Program Files\dotnet\sdk]
  5.0.400 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]
Other steps I took to investigate
  1. Try to build in VS 2019
    Same symptoms as you can see below:
    image

  2. Remove global.json and switch to net6.0
    The result is the same: my app will not build due to CS0534 but the CS8032 warning is not reported as you can see below:
    image

@eiriktsarpalis
Copy link
Member

I can reproduce the error using the steps that you provided, however the error went away as soon as I removed the global.json file. Curious, what is the output of dotnet --info once you remove global.json on your machine?

I'm not sure if source generation is meant to be supported using v5 SDKs. @layomia or @eerhardt might know.

@eerhardt
Copy link
Member

eerhardt commented Sep 3, 2021

@Jericho - I know this isn't ideal, but try to set <LangVersion>preview</LangVersion> in your .csproj. Then using the 6.0 SDK should work.

This is because we moved to the "incremental source generator API", and initially incremental generators need preview langversion. See dotnet/roslyn#55866.

Note this restriction has been lifted in the latest Roslyn.

Regarding VS 2019: the latest packages don't work with older Roslyn. I am working on that with #58536.

@Jericho
Copy link
Author

Jericho commented Sep 5, 2021

I have removed global.json, switched to net6.0 and also added LangVersion in my csproj and I confirm that I can build.

@eerhardt thank you for the links to other issues. Do I understand 58536 correctly that using Json code generator will prevent me from multi-targeting?

@eerhardt
Copy link
Member

eerhardt commented Sep 7, 2021

Do I understand 58536 correctly that using Json code generator will prevent me from multi-targeting?

No that isn't the case. In #58536, the term "multi-targeting" is referring to which version of the Roslyn compiler is being targeted by the source Json code generator. If it only targets the latest Roslyn version, it means the Json code generator won't be able to work in VS 2019, which has an older Roslyn version.

User projects can target multiple TFMs (netstandard2.0, net5.0, etc) just the same as they were previously with no issues.

@eiriktsarpalis
Copy link
Member

@eerhardt it looks like the question has been answered and the underlying issues are being addressed. Can we close this issue?

@eerhardt eerhardt closed this as completed Sep 7, 2021
@Jericho
Copy link
Author

Jericho commented Sep 7, 2021

@eerhardt thank you for the clarification

@ghost ghost locked as resolved and limited conversation to collaborators Oct 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants