Open alexrp opened 3 years ago
Similar issue is encountered in mono, but it doesn't involve EnforceCodeStyleInBuild.
For reference: https://github.com/dotnet/roslyn/issues/44000
@jcouv can you take a look? Appears to be related to reference assemblies.
Isolated a repro. See below. This happens with /refonly
when an analyzer pulls on diagnostics.
It must be repro'ed as a command-line test to hit CommonCompiler.CompileAndEmit
, which first emits the output (without emitting method bodies), then marks the event queue as completed (PromiseNotToEnqueue
), then let's the analyzer process the "compilation completed" event. When the analyzer calls GetDiagnostics
, that binds method bodies which attempts to queue some events (which is disallowed by now).
From discussion with Aleksey, I should understand what is the event that is triggering the exception, to see whether it should have been reported during declaration/emit phase or not. Then there is the question of whether we should run analyzers in /refonly
compilation.
PS: also need to consider whether source generator might be impacted.
[Fact]
public void RefAssembly_AnalyzerGetsDiagnostics()
{
var errorLog = Temp.CreateFile();
var dir = Temp.CreateDirectory();
var srcFile = dir.CreateFile("source.cs").WriteAllText(@"
using System;
class Program
{
static void Main()
{
Console.WriteLine(42);
}
}
");
var csc = CreateCSharpCompiler(
null,
workingDirectory: dir.Path,
args: new[] { "/errorlog:" + errorLog.Path, "/warnaserror+", "/nologo", "/t:library", "/refonly", "source.cs" },
analyzers: ImmutableArray.Create<DiagnosticAnalyzer>(new RefAssembly_AnalyzerGetsDiagnostics_Analyzer()));
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var exitCode = csc.Run(outWriter);
Assert.Empty(outWriter.ToString());
Assert.Equal(0, exitCode);
CleanupAllGeneratedFiles(srcFile.Path);
CleanupAllGeneratedFiles(errorLog.Path);
CleanupAllGeneratedFiles(dir.Path);
}
private class RefAssembly_AnalyzerGetsDiagnostics_Analyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_descriptor1, s_descriptor2);
private static readonly DiagnosticDescriptor s_descriptor1 = new DiagnosticDescriptor(id: "CA9999_NullabilityPrinter", title: "CA9999_NullabilityPrinter", messageFormat: "Nullability of '{0}' is '{1}':'{2}'. Speculative flow state is '{3}'", category: "Test", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true);
private static readonly DiagnosticDescriptor s_descriptor2 = new DiagnosticDescriptor(id: "CA9998_NullabilityPrinter", title: "CA9998_NullabilityPrinter", messageFormat: "Declared nullability of '{0}' is '{1}'", category: "Test", defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true);
public override void Initialize(AnalysisContext context)
{
context.RegisterSemanticModelAction(context =>
{
var diag = context.SemanticModel.GetDiagnostics();
});
}
}
Using refonly-repro
branch
I see now how I broke the compiler on some of my reference only projects as well.
I think what has to happen is to not use analyzers on my reference only projects at all, but I wanted to have them at least use stylecop to enforce docs on public members on the reference only version of the code.
Version Used:
Steps to Reproduce:
Expected Behavior:
Compiles without errors.
Actual Behavior: