Skip to content

Commit

Permalink
Fix bug in none host (#98)
Browse files Browse the repository at this point in the history
* Fix bug in none host

The `SolutionReader` was not promptly putting generated files into the
`Project` when the none host was enabled. Looking into this made me
realize the code wasn't treating none host strongly enough in a few
places so fixed those as well.

* fixed tests
  • Loading branch information
jaredpar authored Dec 21, 2023
1 parent 77909a2 commit 76c9fd3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ public void NoneHostGeneratedFilesShouldBeLast()
}

[Fact]
public void NoneHostAddsFakeGeneratorForGeneratedSource()
public void NoneHostHasNoGenerators()
{
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, BasicAnalyzerHostOptions.None);
var data = reader.ReadCompilationData(0);
var compilation1 = data.Compilation;
var compilation2 = data.GetCompilationAfterGenerators();
Assert.NotSame(compilation1, compilation2);
Assert.Single(data.AnalyzerReferences);
Assert.Same(compilation1, compilation2);
Assert.Empty(data.AnalyzerReferences);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void GeneratedTextExcludeAnalyzers()
Assert.True(foundPath);
var analyzers = Directory.GetFiles(Path.Combine(tempPath, "analyzers"), "*.dll", SearchOption.AllDirectories).ToList();
Assert.Equal(7, analyzers.Count);
Assert.Empty(analyzers);
}, runBuild: false);
}

Expand Down
32 changes: 24 additions & 8 deletions src/Basic.CompilerLog.UnitTests/SolutionReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,35 @@ public SolutionReaderTests(ITestOutputHelper testOutputHelper, CompilerLogFixtur
Fixture = fixture;
}

[Theory]
[InlineData(BasicAnalyzerKind.Default)]
[InlineData(BasicAnalyzerKind.None)]
public async Task DocumentsHaveGeneratedTextWithAnalyzers(BasicAnalyzerKind kind)
[Fact]
public async Task DocumentsGeneratedDefaultHost()
{
var host = new BasicAnalyzerHostOptions(kind);
var host = new BasicAnalyzerHostOptions(BasicAnalyzerKind.Default);
using var reader = SolutionReader.Create(Fixture.ConsoleComplogPath.Value, host);
var workspace = new AdhocWorkspace();
var solution = workspace.AddSolution(reader.ReadSolutionInfo());
var project = solution.Projects.Single();
Assert.NotEmpty(project.AnalyzerReferences);
var docs = await project.GetSourceGeneratedDocumentsAsync();
var doc = docs.First(x => x.Name == "RegexGenerator.g.cs");
Assert.NotNull(doc);
var docs = project.Documents.ToList();
var generatedDocs = (await project.GetSourceGeneratedDocumentsAsync()).ToList();
Assert.Null(docs.FirstOrDefault(x => x.Name == "RegexGenerator.g.cs"));
Assert.Single(generatedDocs);
Assert.NotNull(generatedDocs.First(x => x.Name == "RegexGenerator.g.cs"));
}

[Fact]
public async Task DocumentsGeneratedNoneHost()
{
var host = new BasicAnalyzerHostOptions(BasicAnalyzerKind.None);
using var reader = SolutionReader.Create(Fixture.ConsoleComplogPath.Value, host);
var workspace = new AdhocWorkspace();
var solution = workspace.AddSolution(reader.ReadSolutionInfo());
var project = solution.Projects.Single();
Assert.Empty(project.AnalyzerReferences);
var docs = project.Documents.ToList();
var generatedDocs = (await project.GetSourceGeneratedDocumentsAsync()).ToList();
Assert.Equal(5, docs.Count);
Assert.Equal("RegexGenerator.g.cs", docs.Last().Name);
Assert.Empty(generatedDocs);
}
}
30 changes: 28 additions & 2 deletions src/Basic.CompilerLog.Util/Basic.CompilerLog.Util.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Basic.CompilerLog.Util/BasicAnalyzerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public enum BasicAnalyzerKind

/// <summary>
/// Analyzers and generators from the original are not loaded at all. In the case
/// the original build had generated files they will be added through an in
/// memory analyzer that just adds them directly.
/// the original build had generated files they are just added directly to the
/// compilation.
/// </summary>
/// <remarks>
/// This option avoids loading third party analyzers and generators.
Expand Down
9 changes: 8 additions & 1 deletion src/Basic.CompilerLog.Util/CompilerLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public CompilationData ReadCompilationData(CompilerCall compilerCall)

var hashAlgorithm = rawCompilationData.ChecksumAlgorithm;
var sourceTextList = new List<(SourceText SourceText, string Path)>();
var generatedTextList = new List<(SourceText SourceText, string Path)>();
var analyzerConfigList = new List<(SourceText SourceText, string Path)>();
var additionalTextList = new List<AdditionalText>();

Expand All @@ -270,7 +271,10 @@ public CompilationData ReadCompilationData(CompilerCall compilerCall)
sourceTextList.Add((GetSourceText(rawContent.ContentHash, hashAlgorithm), rawContent.FilePath));
break;
case RawContentKind.GeneratedText:
// Handled when creating the analyzer host
if (BasicAnalyzerHostOptions.ResolvedKind == BasicAnalyzerKind.None)
{
generatedTextList.Add((GetSourceText(rawContent.ContentHash, hashAlgorithm), rawContent.FilePath));
}
break;
case RawContentKind.AnalyzerConfig:
analyzerConfigList.Add((GetSourceText(rawContent.ContentHash, hashAlgorithm), rawContent.FilePath));
Expand Down Expand Up @@ -318,6 +322,9 @@ public CompilationData ReadCompilationData(CompilerCall compilerCall)
}
}

// Generated source code should appear last to match the compiler behavior.
sourceTextList.AddRange(generatedTextList);

var emitData = new EmitData(
rawCompilationData.AssemblyFileName,
rawCompilationData.XmlFilePath,
Expand Down
13 changes: 7 additions & 6 deletions src/Basic.CompilerLog.Util/ExportUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ void WriteReferences()

void WriteAnalyzers()
{
if (!IncludeAnalyzers)
{
return;
}

foreach (var analyzer in data.Analyzers)
{
using var analyzerStream = Reader.GetAssemblyStream(analyzer.Mvid);
var filePath = builder.AnalyzerDirectory.WriteContent(analyzer.FilePath, analyzerStream);

if (IncludeAnalyzers)
{
var arg = $@"/analyzer:""{PathUtil.RemovePathStart(filePath, builder.DestinationDirectory)}""";
commandLineList.Add(arg);
}
var arg = $@"/analyzer:""{PathUtil.RemovePathStart(filePath, builder.DestinationDirectory)}""";
commandLineList.Add(arg);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostNone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ internal BasicAnalyzerHostNone(bool readGeneratedFiles, ImmutableArray<(SourceTe
{
ReadGeneratedFiles = readGeneratedFiles;
GeneratedSourceTexts = generatedSourceTexts;
AnalyzerReferencesCore = readGeneratedFiles && generatedSourceTexts.Length == 0
? ImmutableArray<AnalyzerReference>.Empty
: ImmutableArray.Create<AnalyzerReference>(new NoneAnalyzerReference(readGeneratedFiles, generatedSourceTexts));
AnalyzerReferencesCore = ImmutableArray<AnalyzerReference>.Empty;

if (!ReadGeneratedFiles)
{
AddDiagnostic(Diagnostic.Create(CannotReadGeneratedFiles, Location.None));
}
}

protected override void DisposeCore()
Expand Down
11 changes: 10 additions & 1 deletion src/Basic.CompilerLog.Util/SolutionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public ProjectInfo ReadProjectInfo(int index)
var documents = new List<DocumentInfo>();
var additionalDocuments = new List<DocumentInfo>();
var analyzerConfigDocuments = new List<DocumentInfo>();
var generatedFiles = new List<DocumentInfo>();

foreach (var tuple in rawCompilationData.Contents)
{
Expand All @@ -81,7 +82,12 @@ public ProjectInfo ReadProjectInfo(int index)
Add(documents);
break;
case RawContentKind.GeneratedText:
// Handled when creating analyzer host.
// When the host has generators these will be added as generators are run. If the host
// doesn't have generators then we need to add them as documents now.
if (Reader.BasicAnalyzerHostOptions.Kind == BasicAnalyzerKind.None)
{
Add(generatedFiles);
}
break;
case RawContentKind.AdditionalText:
Add(additionalDocuments);
Expand Down Expand Up @@ -115,6 +121,9 @@ void Add(List<DocumentInfo> list)
}
}

// Generated files should appear last
documents.AddRange(generatedFiles);

// https://github.com/jaredpar/complog/issues/24
// Need to store project reference information at the builder point so they can be properly repacked
// here and setup in the Workspace
Expand Down

0 comments on commit 76c9fd3

Please sign in to comment.