Skip to content

Commit b960f64

Browse files
authored
Add documentation about contributing to config binder generator (#91954)
* Add documentation about contributing to config binder generator * Address feedback and make misc improvements
1 parent 9ee3b6e commit b960f64

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/libraries/Microsoft.Extensions.Configuration.Binder/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Provides the functionality to bind an object to data in configuration providers for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration/).
44

5-
Documentation can be found at https://learn.microsoft.com/dotnet/core/extensions/configuration
5+
Documentation can be found at https://learn.microsoft.com/dotnet/core/extensions/configuration.
66

77
## Contribution Bar
88
- [x] [We consider new features, new APIs, bug fixes, and performance changes](../README.md#contribution-bar)
@@ -89,3 +89,42 @@ You can include a configuration file using a code like this in your `.csproj` fi
8989
</Content>
9090
</ItemGroup>
9191
```
92+
93+
## Contributing changes to the source generator
94+
95+
Along with the binder assembly that uses reflection, we ship a source generator assembly that provides a reflection and AOT compatible implementation. It works by intercepting regular binding calls and redirecting them to new routines that execute strongly typed binding logic.
96+
97+
### Worfklow
98+
99+
- [Build the .NET libraries and runtime assemblies](https://github.com/dotnet/runtime/tree/7ed33d80c92fa0b7ae740df60a460e984f2f442b#how-can-i-contribute)
100+
- Make the generator changes in the `gen\` directory
101+
- Validate the changes by running the source generator tests in `tests\SourceGeneratorTests`
102+
103+
### Testing
104+
105+
Here's a general command for running the the generator tests. They assume that you're making the changes in a Windows OS and running the from the `tests\SourceGeneratorTests\` directory.
106+
107+
```ps
108+
dotnet build /t:test
109+
```
110+
111+
If applicable, add new tests to validate your contribution. See [this documentation](https://github.com/dotnet/runtime/blob/7ed33d80c92fa0b7ae740df60a460e984f2f442b/docs/workflow/README.md#full-instructions-on-building-and-testing-the-runtime-repo) for details.
112+
113+
#### Stale generator bits
114+
115+
Sometimes the SDK uses stale bits of the generator. This can lead to unexpected test behavior or failures. Killing `dotnet.exe` processes usually solves the issue, e.g. with `taskkill /F /IM dotnet.exe /T`.
116+
117+
#### Updating baselines
118+
119+
Some contributions might change the logic emitted by the generator. We maintain baseline [source files](https://github.com/dotnet/runtime/tree/e3e9758a10870a8f99a93a25e54ab2837d3abefc/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/Baselines) to track the code emitted to handle some core binding scenarios.
120+
121+
If the emitted code changes, these tests will fail locally and in continuous integration checks (for PRs) changes. You would need to update the baseline source files, manually or by using the following commands (PowerShell):
122+
123+
```ps
124+
> $env:RepoRootDir = "D:\repos\dotnet_runtime"
125+
> dotnet build t:test -f /p:UpdateBaselines=true
126+
```
127+
128+
We have a [test helper](https://github.com/dotnet/runtime/blob/e3e9758a10870a8f99a93a25e54ab2837d3abefc/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.Helpers.cs#L105-L118) to update the baselines. It requires setting an environment variable called `RepoRootDir` to the root repo path. In additon, the `UpdateBaselines` MSBuild property needs to be set to `true`.
129+
130+
After updating the baselines, inspect the changes to verify that they are valid. Note that the baseline tests will fail if the new code causes errors when building the resulting compilation.

src/libraries/Microsoft.Extensions.Configuration.Binder/tests/SourceGenerationTests/GeneratorTests.Helpers.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ private static async Task VerifyAgainstBaselineUsingFile(
105105
#if UPDATE_BASELINES
106106
if (!success)
107107
{
108-
string? repoRootDir = Environment.GetEnvironmentVariable("RepoRootDir");
109-
Assert.True(repoRootDir is not null, "To update baselines, specifiy the root runtime repo dir");
108+
const string envVarName = "RepoRootDir"
109+
string errMessage = $"To update baselines, specify a '{envVarName}' environment variable. See this assembly's README.md doc for more details."
110+
111+
string? repoRootDir = Environment.GetEnvironmentVariable(envVarName);
112+
Assert.True(repoRootDir is not null, errMessage);
110113

111114
IEnumerable<string> lines = r[0].SourceText.Lines.Select(l => l.ToString());
112115
string source = string.Join(Environment.NewLine, lines).TrimEnd(Environment.NewLine.ToCharArray()) + Environment.NewLine;

0 commit comments

Comments
 (0)