Skip to content

Make WriteCodeFragment task locale-independent for reproducible builds#13192

Merged
baronfel merged 5 commits intomainfrom
copilot/fix-writecodefragment-localization
Feb 5, 2026
Merged

Make WriteCodeFragment task locale-independent for reproducible builds#13192
baronfel merged 5 commits intomainfrom
copilot/fix-writecodefragment-localization

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 3, 2026

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
  • 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.

Original prompt

This section details on the original issue you should resolve

<issue_title>WriteCodeFragment task is locale dependent.</issue_title>
<issue_description>

string comment = ResourceUtilities.GetResourceString("WriteCodeFragment.Comment");
globalNamespace.Comments.Add(new CodeCommentStatement(comment));

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)

@baronfel 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?</comment_new>
<comment_new>@rainersigwald
I'm not aware of any but I agree that it seems like it should be invariant by default.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@dotnet-policy-service
Copy link
Copy Markdown
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.

Copilot AI and others added 3 commits February 3, 2026 17:28
- 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
Copilot AI requested a review from baronfel February 3, 2026 17:37
Comment thread src/Tasks/AssemblyResources.cs
Comment thread src/Tasks.UnitTests/WriteCodeFragment_Tests.cs Outdated
…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>
Copilot AI requested a review from baronfel February 3, 2026 18:02
@baronfel baronfel marked this pull request as ready for review February 3, 2026 18:24
Copilot AI requested a review from baronfel February 3, 2026 18:27
@baronfel baronfel enabled auto-merge (squash) February 3, 2026 22:05
@baronfel baronfel merged commit 54d87c5 into main Feb 5, 2026
10 checks passed
@baronfel baronfel deleted the copilot/fix-writecodefragment-localization branch February 5, 2026 20:25
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WriteCodeFragment task is locale dependent.

5 participants