Audit static field usage: Make mutable static fields readonly to improve MSBuild safety #10359
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.
This PR addresses static field usage in Xamarin.Android.Build.Tasks to improve MSBuild safety and reduce potential thread safety issues, as identified in the audit request.
Problem
The original issue identified 27 mutable static fields that could be problematic in MSBuild context where tasks might run in parallel or be cached inappropriately. Mutable static fields can cause:
Solution
After systematically auditing all static fields in the Build.Tasks assembly, I identified and fixed 13 static fields that should be immutable after initialization by adding the
readonlymodifier:XML and Configuration Fields:
ManifestDocument.AndroidXmlNamespace&AndroidXmlToolsNamespace- XML namespace constantsManifestDocument.androidNs&androidToolsNs- Cached namespace referencesJavaObjectsXmlFile.settings- XML writer settingsSupportsGLTextureAttribute.mapping- Manifest mapping configurationRegex and Parsing Fields:
Aapt2Link.exraArgSplitRegEx- Compiled regex for argument parsingResourceIdentifier.validIdentifier- Compiled regex for validationCheckForInvalidResourceFileNames.javaKeywords- Java keyword arrayRtxtParser.knownTypes- Set of known resource typesUtility Fields:
ManagedResourceParser.compareTuple- Comparer instanceRemoveUnknownFiles.IsWindows&PathUtil.IsWindows- Platform detection flagsAnalysis of Remaining Static Fields
The remaining static fields were analyzed and found to be appropriate:
MonoAndroidHelper.SupportedVersions&AndroidSdk- Set once per build by ResolveSdksTask following established MSBuild patternsAndroidLinkConfiguration.configurations- UsesConditionalWeakTablewhich is already thread-safeMonoAndroidHelper.uname- UsesLazy<T>with thread-safe initializationImpact
These changes eliminate potential race conditions by making static fields immutable after initialization, while maintaining all existing functionality. The modifications are surgical, affecting only field declarations without changing any logic or behavior.
Fixes #5996.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.