Skip to content

roslyn Code generators having issues when using external dependencies,. ex: Newtonsoft.Json #56216

@edwin-alvarez

Description

@edwin-alvarez

Version Used:
3.11.0

Steps to Reproduce:

  1. I created a generator project and added Newtonsoft.Json to it
  2. my demo app does not has any reference to Newtonsoft.Json
  3. ran the generator and it fails generation because Newtonsoft.Json is not found or one of its dependencies
  4. sometimes this works, but I haven't figure out yet, then it actually works.

NOTE:
I tried with the default System.Text.Json, and that's also failing.

Expected Behavior:
as mentioned in the github, we should be able to include external dlls
https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#use-functionality-from-nuget-packages

Actual Behavior:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Description></Description>
    <AssemblyVersion>0.1</AssemblyVersion>
    <FileVersion>0.1</FileVersion>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Generates a package at build -->
    <IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" PrivateAssets="all" GeneratePathProperty="true" />

    <!-- Package the generator in the analyzer directory of the nuget package -->
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

    <!-- Package the Newtonsoft.Json dependency alongside the generator assembly -->
    <None Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
	
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\StandardSDK.Attributes\StandardSDK.Attributes.csproj" />
  </ItemGroup>

</Project>

Exception:

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files/Microsoft Visual Studio/2022/Preview/MSBuild/Current/Bin/Roslyn/
LOG: Initial PrivatePath = NULL
Calling assembly : StandardSDKAnalyzers, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\Roslyn\VBCSCompiler.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Preview/MSBuild/Current/Bin/Roslyn/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Preview/MSBuild/Current/Bin/Roslyn/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Preview/MSBuild/Current/Bin/Roslyn/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio/2022/Preview/MSBuild/Current/Bin/Roslyn/Newtonsoft.Json/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Users/[user]/AppData/Local/Temp/VBCSCompiler/AnalyzerAssemblyLoader/1b1099df6f184afdae38c7566ffa98e4/5/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/[user]/AppData/Local/Temp/VBCSCompiler/AnalyzerAssemblyLoader/1b1099df6f184afdae38c7566ffa98e4/5/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/[user]/AppData/Local/Temp/VBCSCompiler/AnalyzerAssemblyLoader/1b1099df6f184afdae38c7566ffa98e4/5/Newtonsoft.Json.EXE.
LOG: Attempting download of new URL file:///C:/Users/[user]/AppData/Local/Temp/VBCSCompiler/AnalyzerAssemblyLoader/1b1099df6f184afdae38c7566ffa98e4/5/Newtonsoft.Json/Newtonsoft.Json.EXE.

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions