Skip to content
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

JsonSourceGenerator cannot be created in VS2019 preview #21006

Closed
SimonCropp opened this issue Sep 15, 2021 · 11 comments
Closed

JsonSourceGenerator cannot be created in VS2019 preview #21006

SimonCropp opened this issue Sep 15, 2021 · 11 comments
Labels
Area-VS untriaged Request triage from a team member

Comments

@SimonCropp
Copy link
Contributor

SimonCropp commented Sep 15, 2021

Describe the bug

Updated to dotnet 6 sdk rc1 ( 6.0.100-rc.1.21458.32). Now VS (16.11.3 Preview 1.0) fails with:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS8032	An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from
C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.0-rc.1.21451.13\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'
or one of its dependencies. The system cannot find the file specified..	Verify (netstandard2.0)	C:\Code\VerifyTests\Verify\src\Verify\CSC	1	Active

Does not occur when running dotnet build from the command line

It is also strange since i do not directly reference the System.Text.Json nuget.

To Reproduce

I am able to reproduce by building this https://github.com/VerifyTests/Verify/tree/WithGenFailure

Further technical details

dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21458.32
 Commit:    d7c22323c4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.1.21458.32\

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

.NET SDKs installed:
  5.0.203 [C:\Program Files\dotnet\sdk]
  5.0.300 [C:\Program Files\dotnet\sdk]
  5.0.401 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21458.32 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

VS info:

Microsoft Visual Studio Professional 2019 Preview
Version 16.11.3 Preview 1.0
VisualStudio.16.Preview/16.11.3-pre.1.0+31702.278
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Professional

Visual C++ 2019   00435-20310-39075-AA662
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019   16.11.75.64347
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.11.75.64347
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.11.75.64347
Azure App Service Tools v3.0.0

C# Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

JetBrains ReSharper 2021.2.1   Build 212.0.20210826.105310
JetBrains ReSharper package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper. Copyright © 2021 JetBrains, Inc.

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.2
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.10.15 (552afdf)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   5.11.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Razor (ASP.NET Core)   16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   16.0.62107.28140
Microsoft SQL Server Data Tools

TypeScript Tools   16.0.30526.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   16.11.0-beta.21322.6+488cc578cafcd261d90d748d8aaa7b8b091232dc
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.11.000.175 (d16-11@e140e23)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.11.0.17 (remotes/origin/11e0001f0b17269345e80b58fb3adf1ba4efe2cd@11e0001f0)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.10.5 (355b57a)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Workaround

<Target Name="DisableAnalyzers"
        BeforeTargets="CoreCompile">
  <ItemGroup>
    <Analyzer Remove="@(Analyzer)"
              Condition="'%(Filename)' == 'System.Text.Json.SourceGeneration'"/>
  </ItemGroup>
</Target>
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-VS untriaged Request triage from a team member labels Sep 15, 2021
@SimonCropp

This comment has been minimized.

@odhanson
Copy link
Member

+1 on this. Trying to bump up our build to use RC1 and getting the following errors:

        CSC : error CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.0-rc.1.21451.13\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' or one of its dependencies. The system cannot find the file specified.. [D:\PQExcel\src\Tests\Excel.NativeBootstrapper\Microsoft.Mashup.Client.Excel.NativeBootstrapper.Tests.csproj]

We don't even use System.Text.Json.SourceGeneration.dll in our code. Disabling the Analyzers is not an acceptable workaround as we will not be in compliance.

@KalleOlaviNiemitalo
Copy link
Contributor

I imagine #20793 and dotnet/runtime#58446 could help with this. On the other hand, #20987 would add a warning.

@pranavkm
Copy link
Contributor

FYI @layomia

@eerhardt
Copy link
Member

eerhardt commented Sep 15, 2021

As mentioned in #20987, in order to target net6.0 you need to use VS 2022. Using VS 2019 is not supported.

One reason it is not supported is because the version of the compiler you get in VS 2019 is the version of Roslyn that shipped with .NET 5.0 - Roslyn version 3.11. That is why you are seeing that error:

An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.0-rc.1.21451.13\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' or one of its dependencies. The system cannot find the file specified.

The source generators that ship with net6.0 depend on Roslyn 4.0. Trying to load them in VS 2019 doesn't work.

There are other features in net6.0 that won't work in VS 2019 as well - for example workloads, new language features like global usings, file-scoped namespaces, new interpolated string features, etc.

@eerhardt
Copy link
Member

A more targeted workaround for this specific issue would be:

  <Target Name="_RemoveFrameworkReferenceAnalyzers"
          AfterTargets="ResolveTargetingPackAssets">
    <ItemGroup>
      <Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.FrameworkReferenceName)' != ''" />
    </ItemGroup>
  </Target>

Which will only remove the Json source generator, and not other analyzers (which are pretty important, since they help you write correct code).

But this is only a band-aid to the issue. The real solution is to use VS 2022.

@SimonCropp
Copy link
Contributor Author

firstly, thanks to all for the quick responses and help.

@eerhardt

But this is only a band-aid to the issue. The real solution is to use VS 2022.

Is this a temporary state that will be fixed, or is the intent that net6 will only be supported on VS2022

@SimonCropp
Copy link
Contributor Author

@odhanson try this work around for the one Analyzer

<Target Name="DisableAnalyzers"
        BeforeTargets="CoreCompile">
  <ItemGroup>
    <Analyzer Remove="@(Analyzer)"
              Condition="'%(Filename)' == 'System.Text.Json.SourceGeneration'"/>
  </ItemGroup>
</Target>

@SimonCropp
Copy link
Contributor Author

can anyone explain why System.Text.Json.SourceGeneration is even bein executed when i dont use System.Text.Json?

@eerhardt
Copy link
Member

Is this a temporary state that will be fixed, or is the intent that net6 will only be supported on VS2022

The intent is that net6 will only be supported on VS 2022. See https://devblogs.microsoft.com/dotnet/announcing-net-6-rc1/:

.NET 6 RC1 has been tested and is supported with Visual Studio 2022 Preview 4.

And #20987, which will add an explicit warning in RC2 that says "NETSDK1182: Targeting .NET 6.0 in Visual Studio 2019 is not supported."

can anyone explain why System.Text.Json.SourceGeneration is even bein executed when i dont use System.Text.Json?

When you target net6.0, there are a bunch of libraries that automatically get included by default. System.Text.Json is one of those libraries. You don't need to add a PackageReference to use the APIs in it. The same goes for other "core" libraries, like Console and HttpClient.

System.Text.Json shipped a new source generator in net6.0 that comes referenced by default as well. Check out https://github.com/dotnet/designs/blob/main/accepted/2021/InboxSourceGenerators.md to learn more about these "Inbox Source Generators".

@SimonCropp
Copy link
Contributor Author

@eerhardt thanks for the clarifications. Given the issue as described isnt a supported scenario, i will close this one

SimonCropp added a commit to VerifyTests/Verify that referenced this issue Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-VS untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

5 participants