Improve source generation both for Roslyn-based and for BT-based SG
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.5k
- Forks
- 124
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 9
Description
Summary
We should use IIncrementalSourceGenerator for CsWin32 not only because it's recommended but because NativeMethods.txt is expected to have relatively a large number of entries. Re-generating the whole source every time NativeMethods.txt or .json degrades the VS performance on large projects.
Plus, the BuildTask one has Inputs and Outputs @ GenerateCsWin32Code but once called, it generates the whole source.
Roslyn-based SG Design
[Generator]
public sealed class SourceGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var options = context.AdditionalTextsProvider
.Where(static file => Path.GetFileName(file.Path).EndsWith("NativeMethods.json", StringComparison.OrdinalIgnoreCase))
.Collect()
.Select(static (files, ct) => ParseOptions(files, ct));
var nativeMethods = context.AdditionalTextsProvider
.Where(static file => Path.GetFileName(file.Path).EndsWith("NativeMethods.txt", StringComparison.OrdinalIgnoreCase))
.Select(static (file, ct) => ReadNativeMethods(file, ct))
.Collect();
var buildProps = context.AnalyzerConfigOptionsProvider
.Select(static (provider, ct) => ReadBuildProperties(provider.GlobalOptions));
var inputs = context.CompilationProvider
.Combine(context.ParseOptionsProvider)
.Combine(options)
.Combine(nativeMethods)
.Combine(buildProps);
context.RegisterSourceOutput(inputs, static (spc, input) =>
{
// existing Execute body, but driven from immutable input models
Generate(spc, input);
});
}
}
BuildTask-based SG Design
string sourceText = unit.Value.GetText(Encoding.UTF8).ToString();
bool changed = WriteFileIfChanged(filePath, sourceText);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the Roslyn SourceGenerator initialization and existing Execute/Generate flow, then inspect the BuildTask path around GenerateCsWin32Code. Use the proposed incremental inputs and WriteFileIfChanged design as the target, and verify that unchanged NativeMethods.txt or .json inputs do not regenerate the whole source.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- build-system, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100