Rationalize Microsoft.Build.Framework string resources and add ThrowIf* polyfills for exceptions#13276
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request modernizes the string resource management in Microsoft.Build.Framework to align with patterns used in other .NET repositories. It migrates from a custom FrameworkResources class with Strings.resx to an auto-generated SR class with SR.resx, adds localized resource DLLs to packaging configurations, and introduces polyfills for exception helper methods (ThrowIf*) that are available in modern .NET but not in .NET Framework.
Changes:
- Replaced custom
FrameworkResources.GetString()pattern with auto-generatedSRclass using MSBuild'sGenerateResxSourcefeature - Added
Microsoft.Build.Framework.resources.dllfiles for all 13 supported locales to VSSetup, VSSetup.Arm64, and Localization packages - Introduced polyfill extension methods for
ArgumentNullException,ArgumentException,ArgumentOutOfRangeException, andObjectDisposedExceptionwithThrowIf*helpers for non-.NET targets
Reviewed changes
Copilot reviewed 38 out of 38 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Package/MSBuild.VSSetup/files.swr | Added Framework resource DLLs for x86 and x64 builds across all 13 locales |
| src/Package/MSBuild.VSSetup.Arm64/files.arm64.swr | Added Framework resource DLLs for ARM64 builds across all 13 locales |
| src/Package/Localization/Microsoft.Build.Localization.nuspec | Added Framework resource DLLs to NuGet package for all 13 locales |
| src/MSBuildTaskHost/MSBuildTaskHost.csproj | Changed wildcard polyfill inclusion to explicit file list to exclude ArgumentExceptionExtensions |
| src/Framework/Resources/xlf/.xlf (deleted Strings.) | Removed old Strings.xlf localization files |
| src/Framework/Resources/xlf/.xlf (added SR.) | Added new SR.xlf localization files with additional resource strings for polyfills |
| src/Framework/Resources/SR.resx | Replaced Strings.resx with SR.resx containing new and existing resource strings |
| src/Framework/Resources/AssemblyResources.cs | Deleted custom FrameworkResources class in favor of auto-generated SR |
| src/Framework/Polyfills/ArgumentExceptionExtensions.cs | Added polyfill extension methods using experimental C# extension syntax |
| src/Framework/PathHelpers/AbsolutePath.cs | Updated to use ArgumentException.ThrowIfNullOrEmpty and SR.PathMustBeRooted |
| src/Framework/Microsoft.Build.Framework.csproj | Enabled GenerateResxSource for SR.resx and added using directive for Resources namespace |
| src/Framework.UnitTests/AbsolutePath_Tests.cs | Split test methods and updated assertions for new exception messages |
- Rename Strings.resx and Strings.*.xlf files to SR.resx and SR.*.xlf - Set <GenerateResxSource> property to true - Set <GenerateResxSourceEmitFormatMethods> property to true - Update <EmbeddedResource> to set <GenerateSource> to true - Add <Using Include="$(RootNamespace).Resources"/> to make it easier to access "SR" throughout Microsoft.Build.Framework - Change calls to FrameworkResources.GetString(...) to access the generated SR class. - Remove FrameworkResources (AssemblyResources.cs)
Add the following static extension methods to polyfill ThrowIf* static methods on non-.NET: - ArgumentNullException.ThrowIfNull(object?, string?) - ArgumentNullException.ThrowIfNull(void*, string?) - ArgumentException.ThrowIfNullOrEmpty(string?, string?) - ArgumentException.ThrowIfNullOrWhiteSpace(string?, string?) - ArgumentOutOfRangeException.ThrowIfZero(int, string?) - ArgumentOutOfRangeException.ThrowIfNegative(int, string?) - ArgumentOutOfRangeException.ThrowIfNegativeOrZero(int, string?) - ArgumentOutOfRangeException.ThrowIfEqual(T, T, string?) - ArgumentOutOfRangeException.ThrowIfNotEqual(T, T, string?) - ArgumentOutOfRangeException.ThrowIfGreaterThan(T, T, string?) - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(T, T, string?) - ArgumentOutOfRangeException.ThrowIfLessThan(T, T, string?) - ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(T, T, string?) - ObjectDisposedException.ThrowIf(bool, object) - ObjectDisposedException.ThrowIf(bool, Type) In addition, MSBuildTaskHost.csproj has been tweaked to only include the files in the Polyfills directory that it uses.
Use ArgumentException.ThrowIfNullOrEmpty in AbsolutePath and remove unneeded string resource, "PathMustNotBeNullOrEmpty" in favor of more general exception string.
e7ce14c to
1ca1e44
Compare
@JanProvaznik: Are there internal instructions for how to do test VS insertions with the MSBuild repo or a pipeline you could point me to? I'm happy to do these on my own to reduce cost on your end. Feel free to ping me over Teams if there's information. |
|
No wait, that's the wrong scenario, shouldn't have tried to do this while talking to you 🙃 |
|
no Rainer, that link is for sdk insertion coming from VMR (and the second one assumes the person knows what they're doing 😄 ) @DustinCampbell note for nonmicrosoft contributors: |
…f* polyfills for exceptions (dotnet#13276) ### Summary - Modernize the string resources in `Microsoft.Build.Framework` and bring them in line with other dotnet repos. (01d840a) - Add various `Microsoft.Build.Framework.resources.dlls` to the `Localization` package, `VSSetup` and `VSSetup.Arm64`. (16c3145 and b89d070) - Add polyfills on non-.NET for `ThrowIf*` static methods on various exception types. (4b362f9) - Update `AbsolutePath` to use `ArgumentNullException.ThrowIfNull(...)` and `ArgumentException.ThrowIfNullOrEmpty(...)`. (c0f748b) Eventually, I intend to use the `ThrowIf*` polyfills throughout the MSBuild code base, which will allow several functions to be removed from `ErrorUtilities`. However, while wholly mechanical, that change will be pretty large, and I didn't want to pollute this pull request with all of that.
Summary
Microsoft.Build.Frameworkand bring them in line with other dotnet repos. (01d840a)Microsoft.Build.Framework.resources.dllsto theLocalizationpackage,VSSetupandVSSetup.Arm64. (16c3145 and b89d070)ThrowIf*static methods on various exception types. (4b362f9)AbsolutePathto useArgumentNullException.ThrowIfNull(...)andArgumentException.ThrowIfNullOrEmpty(...). (c0f748b)Eventually, I intend to use the
ThrowIf*polyfills throughout the MSBuild code base, which will allow several functions to be removed fromErrorUtilities. However, while wholly mechanical, that change will be pretty large, and I didn't want to pollute this pull request with all of that.