dev: enable nullable where no errors, treat warnings as errors#3750
dev: enable nullable where no errors, treat warnings as errors#3750
Conversation
|
This takes a step toward our C# code enforcing non-nullable type usage. |
RaymondLuong3
left a comment
There was a problem hiding this comment.
Why not just enable nullable from the .csproj files?
<nullable>enable</nullable>
@RaymondLuong3 reviewed all commit messages and made 1 comment.
Reviewable status: 0 of 255 files reviewed, all discussions resolved.
|
If we enable nullable from the csproj files now, it would apply that setting to all C# files in the project. We would get many errors where things like |
|
@marksvc Is it possible to enable it globally, and then selectively, at a file level, disable it, so all new code will automatically have it enabled? |
e3aeb71 to
1eef167
Compare
marksvc
left a comment
There was a problem hiding this comment.
@Nateowami Thank you for suggesting that. There are around 250 files that can have nullable enabled, and about 161 files that can not (without further work). Now the csproj files enable nullable, which will desirably apply to new files automatically, and not-yet-migrated/fixed-up files are marked with #nullable disable warnings.
When we change a file so it conforms with non-nullable types, we can remove its #nullable disable warnings line.
In file src/SIL.XForge.Scripture/Pages/Shared/_SelectLanguagePartial.cshtml I was not successful applying #nullable disable warnings. Instead, I modify the code so it does not show the <select> element if requestCulture != null. My understanding is that the asp-for is both an input and output binding. It would be read from to show the current language in the element, and written to when a language is selected. Neither of those are going to work if requestCulture is null, and AI suggests the page would show an error in that case. With the change, it will not try to read to or write from requestCulture if it is null, and will just omit the language chooser from the page in that case.
@marksvc made 1 comment.
Reviewable status: 0 of 255 files reviewed, all discussions resolved (waiting on RaymondLuong3).
| #nullable disable warnings | ||
| var requestCulture = Context.Features.Get<IRequestCultureFeature>(); | ||
| InterfaceLanguage? locale = LocOptions.Value.SupportedUICultures?.Where(c => | ||
| SharedResource.Cultures[c.IetfLanguageTag].Tags.Contains(requestCulture.RequestCulture.UICulture.Name) |
There was a problem hiding this comment.
🚩 Inconsistent null handling between _Footer.cshtml and _SelectLanguagePartial.cshtml
Both _Footer.cshtml:12 and _SelectLanguagePartial.cshtml:11 obtain requestCulture from Context.Features.Get<IRequestCultureFeature>(), which can return null. The PR properly adds a null check in _SelectLanguagePartial.cshtml:31 (@if (requestCulture != null)), but in _Footer.cshtml:11 it only adds #nullable disable warnings to suppress the compiler warning. If requestCulture is null, _Footer.cshtml:14 would throw a NullReferenceException when accessing requestCulture.RequestCulture.UICulture.Name. While IRequestCultureFeature should always be present when UseRequestLocalization middleware is configured (src/SIL.XForge.Scripture/Startup.cs:252), this is a pre-existing inconsistency that the PR's nullable migration makes more visible. The AGENTS.md rule 'Corner-cases happen. They should be handled in code' suggests the _Footer.cshtml should also get a proper null check rather than just warning suppression.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Fixing potential NREs is for possible followup, and not intended to be part of this PR. It was done in _SelectLanguagePartial.cshtml where I did not seem to be able to just #nullable disable warnings.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3750 +/- ##
=======================================
Coverage 81.31% 81.31%
=======================================
Files 620 620
Lines 39079 39079
Branches 6376 6376
=======================================
Hits 31778 31778
Misses 6329 6329
Partials 972 972 ☔ View full report in Codecov by Sentry. |
Nateowami
left a comment
There was a problem hiding this comment.
Changes look good, except that I can't build locally due to warnings being treated as errors, and these two errors being present:
src/SIL.XForge.Scripture/Controllers/OnboardingRequestRpcController.cs(490,41): error CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [/home/nate/src/web-xforge/src/SIL.XForge.Scripture/SIL.XForge.Scripture.csproj]
src/SIL.XForge.Scripture/Controllers/OnboardingRequestRpcController.cs(544,41): error CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. [/home/nate/src/web-xforge/src/SIL.XForge.Scripture/SIL.XForge.Scripture.csproj]
@Nateowami reviewed 418 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on marksvc).
Files with
#nullable enablewill have an error from new code likestring foo = null. (Instead ofstring? foo = null.)This change is