Make WriteCodeFragment task locale-independent for reproducible builds#13192
Merged
Make WriteCodeFragment task locale-independent for reproducible builds#13192
Conversation
Contributor
|
Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo. |
- Added GetInvariantString method to AssemblyResources class - Updated WriteCodeFragment to use invariant culture for comments - Added test to verify comment is culture-invariant Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix WriteCodeFragment task to use invariant culture string
Make WriteCodeFragment task locale-independent for reproducible builds
Feb 3, 2026
baronfel
requested changes
Feb 3, 2026
…ultures Changed from [Fact] to [Theory] with [InlineData] for en-US, fr-FR, ja-JP, and de-DE cultures. This better demonstrates the "for all cultures, comment is invariant" property. Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
baronfel
approved these changes
Feb 3, 2026
JanProvaznik
approved these changes
Feb 5, 2026
ViktorHofer
approved these changes
Feb 5, 2026
JanProvaznik
pushed a commit
to JanProvaznik/msbuild
that referenced
this pull request
Feb 25, 2026
dotnet#13192) Work item (Internal use): ### Summary `WriteCodeFragment` task embeds localized resource strings as comments in generated source files, breaking reproducible builds. Generated code varies by system locale. **Changes:** - Added `AssemblyResources.GetInvariantString()` method that retrieves resources using `CultureInfo.InvariantCulture` - Updated `WriteCodeFragment.cs` line 171 to call `GetInvariantString()` instead of `ResourceUtilities.GetResourceString()` - Added `CommentIsInvariantCulture` Theory test with parameterized cultures (en-US, fr-FR, ja-JP, de-DE) that verifies comment invariance across different locales The generated comment `"// Generated by the MSBuild WriteCodeFragment class."` now remains English regardless of system locale. ### Customer Impact Builds using `WriteCodeFragment` (e.g., auto-generated assembly attributes) produce identical output regardless of build machine locale. Fixes build cache misses and enables proper reproducible builds for compliance scenarios. ### Regression? - [ ] Yes - [x] No No behavior change except comment text is now always English. Generated code functionality unchanged. ### Testing - All 48 existing `WriteCodeFragment_Tests` pass - New `CommentIsInvariantCulture` Theory test runs 4 times (once per culture parameter) verifying comment is identical across en-US, fr-FR, ja-JP, and de-DE cultures - Manual verification: French locale produces English comments while MSBuild messages remain localized ### Risk **Low.** Single-line change to comment generation. Resource lookup mechanism unchanged, only culture parameter differs. No API surface changes. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>`WriteCodeFragment` task is locale dependent.</issue_title> > <issue_description>https://github.com/dotnet/msbuild/blob/4f6355ee46480d3e016b01fb12afded5d3884d96/src/Tasks/WriteCodeFragment.cs#L171-L172 > Here `WriteCodeFragment` task use localized resource string as comment in generated source file. This behavior is at odds with reproducible build. I think that resource string should be replaced with language neutral non-localizible string constant.</issue_description> > > <agent_instructions>Make the WriteCodeFragment Task look up the invariant culture resource string for this comment. DO NOT set the thread-or-process-level static properties controlling culture/ui culture to do this, look the resource up directly.</agent_instructions> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > <comment_new><author>@baronfel</author><body> > That's one solution - another might be to allow providing the locale to use to look up the embedded resource string. > > I tend to agree that generated code should be invariant by default, though. @rainersigwald Do we have any guidelines for generated code that we use/reference?</body></comment_new> > <comment_new><author>@rainersigwald</author><body> > I'm not aware of any but I agree that it seems like it should be invariant by default.</body></comment_new> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes dotnet#13191 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work item (Internal use):
Summary
WriteCodeFragmenttask embeds localized resource strings as comments in generated source files, breaking reproducible builds. Generated code varies by system locale.Changes:
AssemblyResources.GetInvariantString()method that retrieves resources usingCultureInfo.InvariantCultureWriteCodeFragment.csline 171 to callGetInvariantString()instead ofResourceUtilities.GetResourceString()CommentIsInvariantCultureTheory test with parameterized cultures (en-US, fr-FR, ja-JP, de-DE) that verifies comment invariance across different localesThe generated comment
"// Generated by the MSBuild WriteCodeFragment class."now remains English regardless of system locale.Customer Impact
Builds using
WriteCodeFragment(e.g., auto-generated assembly attributes) produce identical output regardless of build machine locale. Fixes build cache misses and enables proper reproducible builds for compliance scenarios.Regression?
No behavior change except comment text is now always English. Generated code functionality unchanged.
Testing
WriteCodeFragment_TestspassCommentIsInvariantCultureTheory test runs 4 times (once per culture parameter) verifying comment is identical across en-US, fr-FR, ja-JP, and de-DE culturesRisk
Low. Single-line change to comment generation. Resource lookup mechanism unchanged, only culture parameter differs. No API surface changes.
Original prompt
WriteCodeFragmenttask is locale dependent. #13191💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.