Skip to content

Commit

Permalink
Track GeneratedFilesOutputDirectory in the IDE (#75311)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Oct 18, 2024
1 parent a98e8c0 commit 8d79420
Show file tree
Hide file tree
Showing 78 changed files with 942 additions and 638 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ protected override void ResolveEmbeddedFilesFromExternalSourceDirectives(

private protected override GeneratorDriver CreateGeneratorDriver(string baseDirectory, ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider analyzerConfigOptionsProvider, ImmutableArray<AdditionalText> additionalTexts)
{
return CSharpGeneratorDriver.Create(generators, additionalTexts, (CSharpParseOptions)parseOptions, analyzerConfigOptionsProvider, driverOptions: new GeneratorDriverOptions(disabledOutputs: IncrementalGeneratorOutputKind.Host) { BaseDirectory = baseDirectory });
return CSharpGeneratorDriver.Create(generators, additionalTexts, (CSharpParseOptions)parseOptions, analyzerConfigOptionsProvider, driverOptions: new GeneratorDriverOptions(disabledOutputs: IncrementalGeneratorOutputKind.Host, baseDirectory: baseDirectory));
}

private protected override void DiagnoseBadAccesses(TextWriter consoleOutput, ErrorLogger? errorLogger, Compilation compilation, ImmutableArray<Diagnostic> diagnostics)
Expand Down
6 changes: 5 additions & 1 deletion src/Compilers/CSharp/Test/EndToEnd/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,11 @@ class XAttribute : System.Attribute { }
ctx.RegisterSourceOutput(input, (spc, node) => { });
}));

GeneratorDriver driver = CSharpGeneratorDriver.Create(new ISourceGenerator[] { generator }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(
[generator],
parseOptions: parseOptions,
driverOptions: TestOptions.GeneratorDriverOptions);

driver = driver.RunGenerators(compilation);
var runResult = driver.GetRunResult().Results[0];

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Roslyn.Test.Utilities.TestGenerators;
Expand Down Expand Up @@ -1019,7 +1020,7 @@ class D
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);

var results = driver.GetRunResult();
Expand Down Expand Up @@ -1094,7 +1095,7 @@ class D
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);

var results = driver.GetRunResult();
Expand Down Expand Up @@ -1227,7 +1228,7 @@ class E
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);

var results = driver.GetRunResult();
Expand Down Expand Up @@ -1310,7 +1311,11 @@ class E
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: false));
GeneratorDriver driver = CSharpGeneratorDriver.Create(
[new IncrementalGeneratorWrapper(testGenerator)],
parseOptions: parseOptions,
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: false));

driver = driver.RunGenerators(compilation);

var results = driver.GetRunResult();
Expand Down Expand Up @@ -1397,7 +1402,7 @@ class E
GeneratorDriver driver = CSharpGeneratorDriver.Create(
new[] { new IncrementalGeneratorWrapper(testGenerator) },
parseOptions: parseOptions,
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);

var results = driver.GetRunResult();
Expand Down Expand Up @@ -1517,7 +1522,7 @@ class C
GeneratorDriver driver = CSharpGeneratorDriver.Create(
new[] { new IncrementalGeneratorWrapper(testGenerator) },
parseOptions: parseOptions,
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);
var results = driver.GetRunResult();
Assert.Collection(results.Results[0].TrackedSteps["Fields"],
Expand Down Expand Up @@ -1686,7 +1691,7 @@ class C
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);

// verify we ran the syntax transform once, but fed both outputs
Expand Down Expand Up @@ -1745,7 +1750,7 @@ class C
});
});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);
var results = driver.GetRunResult();

Expand Down Expand Up @@ -1799,7 +1804,7 @@ class C

});

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true));
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { new IncrementalGeneratorWrapper(testGenerator) }, parseOptions: parseOptions, driverOptions: TestOptions.GeneratorDriverOptions);
driver = driver.RunGenerators(compilation);
var results = driver.GetRunResult();

Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/Core/Portable/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Microsoft.CodeAnalysis.Comp
Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Microsoft.CodeAnalysis.Compilation! compilation, System.Func<Microsoft.CodeAnalysis.GeneratorFilterContext, bool>? generatorFilter, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.GeneratorDriver!
Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Microsoft.CodeAnalysis.Compilation! compilation, System.Threading.CancellationToken cancellationToken) -> Microsoft.CodeAnalysis.GeneratorDriver!
*REMOVED*Microsoft.CodeAnalysis.GeneratorDriver.RunGenerators(Microsoft.CodeAnalysis.Compilation! compilation, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.GeneratorDriver!
Microsoft.CodeAnalysis.GeneratorDriverOptions.BaseDirectory.get -> string?
Microsoft.CodeAnalysis.GeneratorDriverOptions.GeneratorDriverOptions(Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind disabledOutputs = Microsoft.CodeAnalysis.IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null) -> void
Microsoft.CodeAnalysis.GeneratorFilterContext
Microsoft.CodeAnalysis.GeneratorFilterContext.CancellationToken.get -> System.Threading.CancellationToken
Microsoft.CodeAnalysis.GeneratorFilterContext.Generator.get -> Microsoft.CodeAnalysis.ISourceGenerator!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Options passed to a <see cref="GeneratorDriver"/> during creation
/// Options passed to a <see cref="GeneratorDriver"/> during creation.
/// </summary>
public readonly struct GeneratorDriverOptions
{
public readonly IncrementalGeneratorOutputKind DisabledOutputs;

public readonly bool TrackIncrementalGeneratorSteps;

internal string? BaseDirectory { get; init; }
/// <summary>
/// Absolute path to directory that generated source file paths are rooted with, or null to use relative paths for the generated files.
/// Usually the project's output directory unless <see cref="CommandLineArguments.GeneratedFilesOutputDirectory"/> is specified.
/// </summary>
public string? BaseDirectory { get; }

public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs)
: this(disabledOutputs, false)
Expand All @@ -29,5 +32,24 @@ public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs, bo
DisabledOutputs = disabledOutputs;
TrackIncrementalGeneratorSteps = trackIncrementalGeneratorSteps;
}

/// <summary>
/// Creates <see cref="GeneratorDriverOptions"/>.
/// </summary>
/// <param name="disabledOutputs"></param>
/// <param name="trackIncrementalGeneratorSteps"></param>
/// <param name="baseDirectory">Absolute path to the base directory used for file paths of generated files.</param>
/// <exception cref="ArgumentException"><paramref name="baseDirectory"/> is not an absolute path.</exception>
public GeneratorDriverOptions(IncrementalGeneratorOutputKind disabledOutputs = IncrementalGeneratorOutputKind.None, bool trackIncrementalGeneratorSteps = false, string? baseDirectory = null)
{
if (baseDirectory != null && !PathUtilities.IsAbsolute(baseDirectory))
{
throw new ArgumentException(message: CodeAnalysisResources.AbsolutePathExpected, nameof(baseDirectory));
}

DisabledOutputs = disabledOutputs;
TrackIncrementalGeneratorSteps = trackIncrementalGeneratorSteps;
BaseDirectory = baseDirectory;
}
}
}
3 changes: 3 additions & 0 deletions src/Compilers/Test/Utilities/CSharp/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using Roslyn.Test.Utilities;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Test.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Test.Utilities
{
Expand Down Expand Up @@ -100,6 +101,8 @@ public static class TestOptions

public static readonly EmitOptions NativePdbEmit = EmitOptions.Default.WithDebugInformationFormat(DebugInformationFormat.Pdb);

public static readonly GeneratorDriverOptions GeneratorDriverOptions = new GeneratorDriverOptions(trackIncrementalGeneratorSteps: true, baseDirectory: TempRoot.Root);

public static CSharpParseOptions WithStrictFeature(this CSharpParseOptions options)
{
return options.WithFeatures(options.Features.Concat(new[] { new KeyValuePair<string, string>("strict", "true") }));
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/Test/Utilities/VisualBasic/TestOptions.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Imports System.Runtime.CompilerServices
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Imports Roslyn.Test.Utilities

Expand Down Expand Up @@ -37,6 +38,8 @@ Public Class TestOptions
Public Shared ReadOnly SigningDebugDll As VisualBasicCompilationOptions = DebugDll.WithStrongNameProvider(SigningTestHelpers.DefaultDesktopStrongNameProvider)
Public Shared ReadOnly SigningDebugExe As VisualBasicCompilationOptions = DebugExe.WithStrongNameProvider(SigningTestHelpers.DefaultDesktopStrongNameProvider)
Public Shared ReadOnly SigningReleaseModule As VisualBasicCompilationOptions = ReleaseModule.WithStrongNameProvider(SigningTestHelpers.DefaultDesktopStrongNameProvider)

Public Shared ReadOnly GeneratorDriverOptions As GeneratorDriverOptions = New GeneratorDriverOptions(trackIncrementalGeneratorSteps:=True, baseDirectory:=TempRoot.Root)
End Class

Friend Module TestOptionExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Sub

Private Protected Overrides Function CreateGeneratorDriver(baseDirectory As String, parseOptions As ParseOptions, generators As ImmutableArray(Of ISourceGenerator), analyzerConfigOptionsProvider As AnalyzerConfigOptionsProvider, additionalTexts As ImmutableArray(Of AdditionalText)) As GeneratorDriver
Return VisualBasicGeneratorDriver.Create(generators, additionalTexts, DirectCast(parseOptions, VisualBasicParseOptions), analyzerConfigOptionsProvider, driverOptions:=New GeneratorDriverOptions(disabledOutputs:=IncrementalGeneratorOutputKind.Host) With {.BaseDirectory = baseDirectory})
Return VisualBasicGeneratorDriver.Create(generators, additionalTexts, DirectCast(parseOptions, VisualBasicParseOptions), analyzerConfigOptionsProvider,
driverOptions:=New GeneratorDriverOptions(disabledOutputs:=IncrementalGeneratorOutputKind.Host, baseDirectory:=baseDirectory))
End Function

Private Protected Overrides Sub DiagnoseBadAccesses(consoleOutput As TextWriter, errorLogger As ErrorLogger, compilation As Compilation, diagnostics As ImmutableArray(Of Diagnostic))
Expand Down
Loading

0 comments on commit 8d79420

Please sign in to comment.