CommunityToolkit / CommunityToolkit/Maui
[BUG] Build errors when turning on the CA1305 analyzer
- Dominant language
- C#
- Stars
- 2.7k
- Forks
- 500
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 7
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
### Current Behavior
Split from Issue #2721 and PR https://github.com/CommunityToolkit/Maui/pull/2722.
When turning on the [CA1305 analyzer](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1305) 7 build issues were found in the CommunityToolkit. These issues could lead to unpredictable behavior if the CultureInfo.CurrentCulture is set to a non-US-English language:
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Camera\CameraInfo.shared.cs(99,4): error CA1305: The behavior of 'StringBuilder.Append(ref StringBuilder.AppendInterpolatedStringHandler)' could vary based on the current user's locale settings. Replace this call in 'CameraInfo.ToString()' with a call to 'StringBuilder.Append(IFormatProvider, ref StringBuilder.AppendInterpolatedStringHandler)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Core\Essentials\SpeechToText\OfflineSpeechToTextImplementation.android.cs(188,56): error CA1305: The behavior of 'int.ToString()' could vary based on the current user's locale settings. Replace this call in 'RecognitionSupportCallback.OnError(int)' with a call to 'int.ToString(IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
F:\Maui\src\CommunityToolkit.Maui.Core\Essentials\Badge\BadgeImplementation.windows.cs(22,40): error CA1305: The behavior of 'uint.ToString()' could vary based on the current user's locale settings. Replace this call in 'BadgeImplementation.SetCount(uint)' with a call to 'uint.ToString(IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)
Build failed with 7 error(s) and 81 warning(s) in 3.8s
### Expected Behavior
After that, turning on the CA1305 analyzer should not result in build errors.
### Steps To Reproduce
Open the project's `.editorconfig` and temporarily add the following lines to the end:
```
# CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = error
```
Run `dotnet build` and observe that the build fails with errors.
### Link to public reproduction project repository
https://github.com/stephenquan/Maui/tree/feature/stephenquan/ca1305
### Environment
```markdown
- .NET MAUI CommunityToolkit: 12.0.0
- OS: Windows 11
- .NET MAUI: 9.0.70
```
### Anything else?
Review the CameraInfo.shared.cs, OfflineSpeechToTextImplementation.android.cs, and BadgeImplementation.windows.cs implementations, and determine if the IFormatProvider, such as the InvariantCulture, can be used.
Contributor guide
Research direction
Start with the CA1305 setting in .editorconfig and the reported call sites in CameraInfo.shared.cs, OfflineSpeechToTextImplementation.android.cs, and BadgeImplementation.windows.cs. Run dotnet build with CA1305 enabled and review each listed diagnostic, including whether an invariant IFormatProvider is appropriate. Done means the build completes without CA1305 errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- build-system, mobile-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100