Skip to content

Could not load file or assembly 'System.IO.Pipelines' in Visual Studio 2022 when using System.Text.Json #80811

@antonGritsenko

Description

@antonGritsenko

Version Used:
4.14.0

Steps to Reproduce:

Config of the generator project:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <IsRoslynComponent>true</IsRoslynComponent>
    <LangVersion>9.0</LangVersion>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <IncludeBuildOutput>false</IncludeBuildOutput>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
    <PackageReference Include="System.Text.Json" Version="9.0.10" PrivateAssets="all" GeneratePathProperty="true" />
  </ItemGroup>

  <ItemGroup>
    <None Include="$(PkgSystem_Text_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>

  
  <PropertyGroup>
    <!-- https://github.com/dotnet/roslyn/discussions/47517-->
    <GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
    <AssemblyName>CmdletsGenerator</AssemblyName>
  </PropertyGroup>

  <Target Name="GetDependencyTargetPaths">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="$(PkgSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" IncludeRuntimeDependency="false" />
    </ItemGroup>
  </Target>
</Project>

Consumed project is net9.0, LibraryProject.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
    <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <EnforceExtendedAnalyzerRules>none</EnforceExtendedAnalyzerRules>
    <EnableInterceptor>true</EnableInterceptor>
  </PropertyGroup>

  <ItemGroup>
    <CompilerVisibleProperty Include="IntermediateOutputPath" />
    <CompilerVisibleProperty Include="CompilerGeneratedFilesOutputPath" />
    <CompilerVisibleProperty Include="EnforceExtendedAnalyzerRules" />
  </ItemGroup>
  
  
  <ItemGroup>
    <Compile Remove="Generated\**\*.cs" />
    <EmbeddedResource Remove="Generated\**\*.cs" />
    <None Remove="Generated\**\*.cs" />
  </ItemGroup>
  <ItemGroup>
    <AdditionalFiles Include="..\..\..\..\angular\src\app\proxy\generate-proxy.json" Link="generate-proxy.json" />
  </ItemGroup>


  

  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\CmdletsGenerator\CmdletsGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>
  <ItemGroup>
    <None Update="Cmdlets.psd1">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

I have very weird issue with the Visual Studio 2022. I'm on version 17.14.17.

dotnet build\dotnet publish on consumed project work always without any issues.

Building consumed project from Visual Studio mostly (see more below) throw the error on first line where Json used:

[Generator]
public class CmdLetsGenerator : IIncrementalGenerator
{
	public void Initialize(IncrementalGeneratorInitializationContext context)
	{
//                       ... other code to init 
			context.RegisterSourceOutput(input.Combine(options), (SourceProductionContext ctx, (string, GeneratorOptions) input) =>
			{
				try
				{
					var (jsonText, options) = input; <-- exception here
				}
				catch (Exception ex)
				{
					ctx.ReportDiagnostic(Diagnostic.Create(
						new DiagnosticDescriptor("CMMGEN001", "Cmdlet generation failed", ex.Message, "Generation", DiagnosticSeverity.Error, true),
						Location.None));
				}

	}
}

The worst thing that sometimes it works, but mostly not. I've tried to find when it works and when it not, but this is really looks like random. Sometimes it works once after some changes in the code of generator project, sometime just once after 10 re-tries. But mostly it throws the error.
Restarting VS, unload-load project and other magic things can help sometimes, but mostly they are not.

Again, dotnet build\publish works always. Tries about 20 times in a row just be sure.

One more evidence: in the consumed project generator marked as Ignored, but files appear (after dotnet build or success build in VS)

Diagnostic Id:
Error:
Could not load file or assembly 'System.IO.Pipelines, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions