I have the following error from my Roslyn analyzer test case from the following project
https://github.com/Weingartner/Migrations.Json.Net
Expected collection to be empty, but found {Test0.cs(5,2): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.}.
The current metadata references in DiagnosticVerifier.Helper.cs are
private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
private static readonly MetadataReference SystemReference = MetadataReference.CreateFromFile(typeof(System.Uri).Assembly.Location);
private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
private static readonly MetadataReference SystemRuntimeReference = MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").Location);
private static readonly MetadataReference MigrationReference = MetadataReference.CreateFromFile(typeof(MigratableAttribute).Assembly.Location);
private static readonly MetadataReference SerializationReference = MetadataReference.CreateFromFile(typeof(DataMemberAttribute).Assembly.Location);
private static readonly MetadataReference JsonNetReference = MetadataReference.CreateFromFile(typeof(Newtonsoft.Json.JsonConvert).Assembly.Location);
One of the references
private static readonly MetadataReference MigrationReference =
MetadataReference
.CreateFromFile(typeof(MigratableAttribute).Assembly.Location);
refers to a subproject Weingartner.Json.Migration
this project is a dotnetstandard 2.0 project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Weingartner.Json.Migration</RootNamespace>
<AssemblyName>Weingartner.Json.Migration</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Weingartner.Json.Migration.Common_\Weingartner.Json.Migration.Common.csproj" />
</ItemGroup>
</Project>
However when I run the analyzer test case I get the runtime error that I should reference netstandard
Expected collection to be empty, but found {Test0.cs(5,2): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.}. at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message) at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) at FluentAssertions.Execution.GivenSelector
1.FailWith(String message, Object[] args) at FluentAssertions.Collections.CollectionAssertions
2.BeEmpty(String because, Object[] becauseArgs) at TestHelper.DiagnosticVerifier.CreateProject(String[] sources, String language) at TestHelper.DiagnosticVerifier.GetDocuments(String[] sources, String language) at TestHelper.DiagnosticVerifier.GetSortedDiagnostics(String[] sources, String language, DiagnosticAnalyzer analyzer) at TestHelper.DiagnosticVerifier.VerifyDiagnostics(String[] sources, String language, DiagnosticAnalyzer analyzer, DiagnosticResult[] expected) at TestHelper.DiagnosticVerifier.VerifyCSharpDiagnostic(String source, DiagnosticResult[] expected) at Weingartner.Json.Migration.Roslyn.Spec.DataContractAnalyzerSpec.ShouldCreateDiagnosticIfMigratableTypeDoesntHaveDataContractAttributeSet()
My guess is I'm missing something at the point I create the project. I can't see an API to specify which version of the framework I'm targeting but the API is quite large so maybe I've missed something.
private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
{
string fileNamePrefix = DefaultFilePathPrefix;
string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;
var projectId = ProjectId.CreateNewId(debugName: TestProjectName);
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference)
.AddMetadataReference(projectId, SystemRuntimeReference)
.AddMetadataReference(projectId, MigrationReference)
.AddMetadataReference(projectId, SerializationReference)
.AddMetadataReference(projectId, JsonNetReference);
var compilationOptions = solution
.GetProject(projectId)
.CompilationOptions
.WithOutputKind(OutputKind.DynamicallyLinkedLibrary);
solution = solution
.WithProjectCompilationOptions(projectId, compilationOptions);
int count = 0;
foreach (var source in sources)
{
var newFileName = fileNamePrefix + count + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
count++;
}
var project = solution.GetProject(projectId);
var diangostics = project.GetCompilationAsync().Result.GetDiagnostics();
diangostics.Should().BeEmpty();
return project;
}