Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Make nullable enabled by default across entire repo - #6960

#6960
Merged
zivkan merged 4 commits into
devNuGet/NuGet.Client:devfrom
dev-zivkan-nullable-defaultNuGet/NuGet.Client:dev-zivkan-nullable-defaultCopy head branch name to clipboard
Nov 20, 2025
Merged

Make nullable enabled by default across entire repo#6960
zivkan merged 4 commits into
devNuGet/NuGet.Client:devfrom
dev-zivkan-nullable-defaultNuGet/NuGet.Client:dev-zivkan-nullable-defaultCopy head branch name to clipboard

Conversation

@zivkan

@zivkan zivkan commented Nov 19, 2025

Copy link
Copy Markdown
Member

Bug

Fixes: code quality

Description

Enable C# nullable annotation checking across entire repo in 3 easy steps:

  1. Set <Nullable>enable</Nullable> in build/common.project.props
  2. Add #nullable disable to every .cs file that fails to compile, until the solution compiles again.
    • There were a small number of other changes required when dependencies of classes that already had nullable enabled needed to resolve new nullability issues
  3. Remove #nullable enable from all the code that previously opt-in individually.
  4. NuGet.CommandLine.XPlat is a special case, because it uses a package that ships source code, and there's no way to exclude the package's source from nullability checks. So this project has nullable disabled, and source files in that project need #nullable enable where possible. We need to migrate the CLI parser to System.CommandLine and remove the other package to bring this project back in line with the rest of the repo.

Step 2 was performed by creating an MSBuild logger that logged every message to JSON, then doing command line builds using the custom logger. Then a second app was used to parse the json output, find files that are failing to compile, then add #nullable disable to the top of the file. Then, I just needed to keep running the two commands in a loop, with only a few manual fixes, to get the repo building again.

Step 3 was mostly a global search and replace, but it did find a few places where nullable was disabled and then re-enabled in the same file, so those were cleaned up.

Personally, I find the easiest way to review this PR is to do git diff origin/dev... on the command line. Then page through the results. The vast majority of changes are #nullable lines, so you can quickly scroll and anything that's different really pops out for more detailed investigation.

Doing git diff origin/dev... --stat shows the number of lines changed per file, and almost all files only have one or two lines changed. The files with more than 2 lines changed are:

 .../Converters/EnumerableToVisibilityConverter.cs                 | 4 ++--
 .../Commands/Package/Update/PackageUpdateCommandRunner.cs         | 6 ++----
 .../NuGet.Commands/RestoreCommand/RestoreCommandProvidersCache.cs | 4 +---
 .../NuGet.Commands/RestoreCommand/Utility/AuditUtility.cs         | 4 +---
 .../NuGet.DependencyResolver.Core/Remote/RemoteWalkContext.cs     | 4 +---
 src/NuGet.Core/NuGet.DependencyResolver.Core/ResolverUtility.cs   | 8 +++-----
 src/NuGet.Core/NuGet.Packaging/ContentModel/ContentItem.cs        | 4 +---
 .../Licenses/NuGetLicenseExpressionParsingException.cs            | 3 +++
 src/NuGet.Core/NuGet.Protocol/HttpSource/HttpRetryHandler.cs      | 6 ++----
 .../NuGet.Protocol/Providers/ReadmeUriTemplateResourceProvider.cs | 3 +--
 .../ensure-nupkg-dependencies-on-source.csproj                    | 3 +--

Doing (gci -recurse -filter *.cs).Length in PowerShell after a git clean shows there are 3882 .cs files in the repo. Since this PR changes 2176, and some of those are csproj files, and about 300 were files with #nullable enable removed, I estimate about 2,000 files now have null checking enabled, which is a little over half the files. It's also easy for someone enthusiastic about making more nullability improvements, as they can search for #nullable disable and start fixing some files.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests N/A
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc. N/A

@zivkan
zivkan requested a review from a team as a code owner November 19, 2025 04:46
@zivkan
zivkan requested review from jebriede and jeffkl November 19, 2025 04:46
Comment thread build/Shared/TaskResult.cs

@jeffkl jeffkl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure all of the #nullable enable are removed

@zivkan

zivkan commented Nov 19, 2025

Copy link
Copy Markdown
Member Author

@jeffkl

Let's make sure all of the #nullable enable are removed

As I wrote in the PR description, since NuGet.CommandLine.XPlat uses a package that ships source code, and that code doesn't work when nullable is enabled, I had to disable nullable by default in the project and enable it in individual files used by the project.

@zivkan
zivkan merged commit ba112de into dev Nov 20, 2025
17 of 18 checks passed
@zivkan
zivkan deleted the dev-zivkan-nullable-default branch November 20, 2025 10:43
@zivkan zivkan mentioned this pull request Nov 21, 2025
3 tasks
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.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.