admin管理员组文章数量:1307026
I am trying to use SyntaxGenerator, which is part of the Microsoft.CodeAnalysis.Workspaces
package, in a source generator as an alternative to StringBuilder and SyntaxFactory. When I try to build however, I get the following error:
Generator 'IOExtensionsGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces, Version=4.12.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.'.
My project hierarchy is split up in 3 parts: the core, the source generators, and the source generator workspace. Due to limitations imposed by Microsoft, you cannot reference Microsoft.CodeAnalysis.Workspaces
inside a source generator project, hence the partitioning.
Cubusky.Core.csproj
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup>
<ProjectReference Include="..\Cubusky.Core.SourceGenerators\Cubusky.Core.SourceGenerators.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
<ProjectReference Include="..\Cubusky.Core.SourceGenerators.Workspaces\Cubusky.Core.SourceGenerators.Workspaces.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
</ItemGroup>
</Project>
Cubusky.Core.SourceGenerators.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>
<!-- The following libraries include the source generator interfaces and types we need -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="*" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="*" PrivateAssets="all" />
</ItemGroup>
<!-- This ensures the library will be packaged as a source generator when we use `dotnet pack` -->
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cubusky.Core.SourceGenerators.Workspaces\Cubusky.Core.SourceGenerators.Workspaces.csproj" />
</ItemGroup>
</Project>
Cubusky.Core.SourceGenerators.Workspaces.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<!-- The following libraries include the source generator interfaces and types we need -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="*" PrivateAssets="all" />
</ItemGroup>
</Project>
What should I change in which csproj files in order to fix the error I'm getting?
What I've tried
For kicks, I tried changing the project reference in my Cubusky.Core.SourceGenerators.csproj
to unreference the output assembly:
<ProjectReference Include="..\Cubusky.Core.SourceGenerators.Workspaces\Cubusky.Core.SourceGenerators.Workspaces.csproj" ReferenceOutputAssembly="false" />
That worked, but I (obviously) need to reference the output in order to use the SyntaxGenerator. I couldn't find a way to unreference the output assembly from Cubusky.Core.csproj
.
I also tried adding a reference the same Workspace package in Cubusky.Core.csproj
, but that did nothing.
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="*" PrivateAssets="all" />
To reproduce
For anyone looking to reproduce this issue, create a solution with 3 projects structured like the above.
In your SourceGenerators.Workspaces.csproj
, create a file with the following code:
public static class MyWorkspace
{
public static void Generate(SourceProductionContext context, SyntaxNode node)
{
// This line of code triggers the error.
var generator = SyntaxGenerator.GetGenerator(new AdhocWorkspace(), LanguageNames.CSharp);
}
}
In your SourceGenerators.csproj
, create a file with the following code:
[Generator]
public class MyGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<SyntaxNode> providers = // get providers
context.RegisterSourceOutput(providers, MyWorkspace.Generate);
}
}
本文标签: cCould not load file or assembly 39MicrosoftCodeAnalysisWorkspaces39Stack Overflow
版权声明:本文标题:c# - Could not load file or assembly 'Microsoft.CodeAnalysis.Workspaces' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741828538a2399791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论