dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

Source Generator fails after updating to .NET 5 RC 1 with Method 'Initialize' missing #47699

Closed trampster closed 3 years ago

trampster commented 3 years ago

Version Used: 5.0.100-rc.1.20452.10

Steps to Reproduce:

  1. Clone https://github.com/trampster/JsonSrcGen
  2. cd UnitTests
  3. dotnet-sdk.dotnet test

Expected Behavior: Generates source code for json

Actual Behavior:

CSC : warning CS8032: An instance of analyzer JsonSrcGen.Generator.JsonGenerator cannot be created from /home/daniel/Work/JsonSrcGen/JsonSrcGen.Generator/bin/Debug/netstandard2.1/JsonSrcGen.Generator.dll : Method 'Initialize' in type 'JsonSrcGen.Generator.JsonGenerator' from assembly 'JsonSrcGen.Generator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.. [/home/daniel/Work/JsonSrcGen/UnitTests/UnitTests.csproj]

The Method 'Initialize' exists, and it hasn't changed since the code was working with the Previews.

Youssef1313 commented 3 years ago

Try changing the Initialize method argument to GeneratorInitializationContext instead of InitializationContext.

See https://github.com/dotnet/roslyn/blob/4602d5d227f19c59665deb3583d7c2abfe8e8611/docs/features/source-generators.cookbook.md#breaking-changes

trampster commented 3 years ago

error CS0246: The type or namespace name 'GeneratorInitializationContext' could not be found (are you missing a using directive or an assembly reference?)

ISourceGenerator requires the old names, unless I also need to update my nuget references:

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0-3.20207.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0-beta2.final" PrivateAssets="all" />
b-straub commented 3 years ago

see #47681

add the following key to your NuGet.config: <add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" /> and update Microsoft.CodeAnalysis.CSharp to 3.8.0-4.20464.1.

trampster commented 3 years ago

I don't have a package reference to Microsoft.CodeAnalysis.CSharp at all, or do you mean Microsoft.CodeAnalysis.CSharp.Workspaces?

trampster commented 3 years ago

I updated my NuGet.config found in ~/.config/NuGet

And added a PackageReference

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-4.20464.1" PrivateAssets="all" />

But that just resulted in this error

/home/daniel/Work/JsonSrcGen/JsonSrcGen.Generator/JsonSrcGen.Generator.csproj : error NU1102: Unable to find package Microsoft.CodeAnalysis.CSharp with version (>= 3.8.0-4.20464.1) [/home/daniel/Work/JsonSrcGen/UnitTests/UnitTests.csproj] /home/daniel/Work/JsonSrcGen/JsonSrcGen.Generator/JsonSrcGen.Generator.csproj : error NU1102: - Found 214 version(s) in https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json [ Nearest version: 3.7.0-3.20271.4 ] [/home/daniel/Work/JsonSrcGen/UnitTests/UnitTests.csproj] /home/daniel/Work/JsonSrcGen/JsonSrcGen.Generator/JsonSrcGen.Generator.csproj : error NU1102: - Found 97 version(s) in nuget.org [ Nearest version: 3.8.0-2.final ] [/home/daniel/Work/JsonSrcGen/UnitTests/UnitTests.csproj]

b-straub commented 3 years ago

You must add the package source for the dotnet-tools preview packages. https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json you can either do this in VS globally or by project or solution scope. nuget.config reference

b-straub commented 3 years ago

I don't have a package reference to Microsoft.CodeAnalysis.CSharp at all, or do you mean Microsoft.CodeAnalysis.CSharp.Workspaces?

Both will work, Microsoft.CodeAnalysis.CSharp.Workspaces depends on Microsoft.CodeAnalysis.CSharp. Means updating Microsoft.CodeAnalysis.CSharp.Workspaces should do the trick as well.

trampster commented 3 years ago

OK I have it working,

My generator .csproj has the following line:

<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>

Which had to be changed to:

<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>

Note the difference is dotnett5 -> dotnet-tools

Here is my working project for for anyone else who runs into this:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

  <PropertyGroup>
    <RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-4.20464.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="..\JsonSrcGen\StringBuilderExtension.cs" Link="StringBuilderExtension.cs" />
  </ItemGroup>

</Project>

See this commit for the full fix: https://github.com/trampster/JsonSrcGen/commit/b9bffdabb71c61c33246cca19a2c6fe9992579b1

I do have a question though, will this PackageSource be required in the .NET 5 full release or is just a work around to a temporary problem?

jasonmalinowski commented 3 years ago

@chsienki Do we have public packages now with the changed APIs that people can reference?

chsienki commented 3 years ago

We're just waiting on the 16.8p3 packages to be released. We're in a slightly odd situation where a few things are out of sync :/

chsienki commented 3 years ago

Version 3.8.0-3.final is now available on the public feeds https://www.nuget.org/packages/Microsoft.CodeAnalysis/3.8.0-3.final

chsienki commented 3 years ago

Closing this, as it should be resolved with the public packages. Please let us know if you're not seeing the expected resolution.

glennawatson commented 3 years ago

The public packages have some warnings

1>C:\Users\Glenn\source\repos\ReactiveMarbles.ObservableEvents\ReactiveMarbles.ObservableEvents.SourceGenerator\ReactiveMarbles.ObservableEvents.SourceGenerator.csproj : warning NU1603: Microsoft.CodeAnalysis.Common 3.8.0-3.final depends on System.Collections.Immutable (>= 5.0.0-preview.8.20371.14) but System.Collections.Immutable 5.0.0-preview.8.20371.14 was not found. An approximate best match of System.Collections.Immutable 5.0.0-preview.8.20407.11 was resolved.
1>C:\Users\Glenn\source\repos\ReactiveMarbles.ObservableEvents\ReactiveMarbles.ObservableEvents.SourceGenerator\ReactiveMarbles.ObservableEvents.SourceGenerator.csproj : warning NU1603: Microsoft.CodeAnalysis.Common 3.8.0-3.final depends on System.Reflection.Metadata (>= 5.0.0-preview.8.20371.14) but System.Reflection.Metadata 5.0.0-preview.8.20371.14 was not found. An approximate best match of System.Reflection.Metadata 5.0.0-preview.8.20407.11 was resolved.

Seems they are relying on unreleased nuget packages.

Can open an issue if it makes sense to do so.

jasonmalinowski commented 3 years ago

@333fred The issue above is fixed by your further upgrading of packages, I'd hope?

333fred commented 3 years ago

Yes, that will be fixed in RC2/16.8p4. The change didn't quite make RC1/16.8p3