Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make host outputs public #74750

Merged
merged 16 commits into from
Aug 27, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3085,7 +3085,7 @@ class C { }
// NOTE: adding new output types will cause this test to fail. Update above as needed.
foreach (IncrementalGeneratorOutputKind kind in Enum.GetValues(typeof(IncrementalGeneratorOutputKind)))
{
if (kind == IncrementalGeneratorOutputKind.None)
if (kind == IncrementalGeneratorOutputKind.None || kind == IncrementalGeneratorOutputKind.Host)
chsienki marked this conversation as resolved.
Show resolved Hide resolved
continue;

if (disabledOutput.HasFlag((IncrementalGeneratorOutputKind)kind))
Expand Down Expand Up @@ -4554,6 +4554,43 @@ public void GeneratorDriver_Makes_HostOutputs_MultipleGenerators()
);
}

[Fact]
public void GeneratorDriver_Makes_HostOutputs_MultipleGenerators_SameName()
{
var generator1 = new PipelineCallbackGenerator((ctx) => { ctx.RegisterHostOutput(ctx.CompilationProvider, (hostCtx, c) => { hostCtx.AddOutput("gen", "value1"); }); });
var generator2 = new PipelineCallbackGenerator2((ctx) => { ctx.RegisterHostOutput(ctx.CompilationProvider, (hostCtx, c) => { hostCtx.AddOutput("gen", "value2"); }); });

var parseOptions = CSharpParseOptions.Default;
var compilation = CreateCompilation("class Compilation1{}", parseOptions: parseOptions);
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator1.AsSourceGenerator(), generator2.AsSourceGenerator()], parseOptions: parseOptions);
driver = driver.RunGenerators(compilation);
var runResult = driver.GetRunResult();

Assert.Collection(runResult.Results,
(r) => { var result = Assert.Single(r.HostOutputs); Assert.Equal("gen", result.Key); Assert.Equal("value1", result.Value); },
(r) => { var result = Assert.Single(r.HostOutputs); Assert.Equal("gen", result.Key); Assert.Equal("value2", result.Value); }
);
}

[Fact]
public void GeneratorDriver_HostOutputs_Throws()
{
var generator = new PipelineCallbackGenerator((ctx) => { ctx.RegisterHostOutput(ctx.CompilationProvider, (hostCtx, c) => { throw new InvalidOperationException("failed"); }); });

var parseOptions = CSharpParseOptions.Default;
var compilation = CreateCompilation("class Compilation1{}", parseOptions: parseOptions);
GeneratorDriver driver = CSharpGeneratorDriver.Create([generator.AsSourceGenerator()], parseOptions: parseOptions);

driver = driver.RunGenerators(compilation);
var runResult = driver.GetRunResult();
var result = Assert.Single(runResult.Results);

Assert.Empty(result.HostOutputs);
var exception = Assert.IsType<InvalidOperationException>(result.Exception);
Assert.Equal("failed", exception.Message);
}


#pragma warning restore RSEXPERIMENTAL004 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
}
25 changes: 25 additions & 0 deletions src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -10657,6 +10657,31 @@
Assert.Equal(absPath, parsedArgs.GeneratedFilesOutputDirectory)
End Sub

<Fact>
Public Sub Compiler_DoesNot_RunHostOutputs()
Dim dir = Temp.CreateDirectory()
Dim src = dir.CreateFile("temp.vb").WriteAllText("
Class C
End Class")
Dim hostOutputRan As Boolean = False
Dim sourceOutputRan As Boolean = False
Dim generator = New PipelineCallbackGenerator(Sub(ctx)
ctx.RegisterHostOutput(ctx.CompilationProvider, Sub(hostCtx, value)
hostOutputRan = True

Check failure on line 10670 in src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb

View check run for this annotation

Azure Pipelines / roslyn-CI (Unix_Build Build_Unix_Debug)

src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb#L10670

src/Compilers/VisualBasic/Test/CommandLine/CommandLineTests.vb(10670,63): error RSEXPERIMENTAL004: (NETCORE_ENGINEERING_TELEMETRY=Build) 'Public Overloads Sub RegisterHostOutput(Of Microsoft.CodeAnalysis.Compilation)(source As Microsoft.CodeAnalysis.IncrementalValueProvider(Of Microsoft.CodeAnalysis.Compilation), action As System.Action(Of Microsoft.CodeAnalysis.HostOutputProductionContext, Microsoft.CodeAnalysis.Compilation))' is for evaluation purposes only and is subject to change or removal in future updates. (https://github.com/dotnet/roslyn/issues/74753)
hostCtx.AddOutput("output", "value")
End Sub)
ctx.RegisterSourceOutput(ctx.CompilationProvider, Sub(spc, po)
sourceOutputRan = True
spc.AddSource("output.vb", "'value")
End Sub)
End Sub)
VerifyOutput(dir, src, includeCurrentAssemblyAsAnalyzerReference:=False, generators:={generator.AsSourceGenerator()})
Assert.[False](hostOutputRan)
Assert.[True](sourceOutputRan)
CleanupAllGeneratedFiles(src.Path)
Directory.Delete(dir.Path, True)
End Sub

<Fact>
Public Sub ExperimentalWithWhitespaceDiagnosticID_WarnForInvalidDiagID()
Dim dir = Temp.CreateDirectory()
Expand Down
Loading