Skip to content

Enabling nullability in NuGet.Packaging - Package Creation - #7217

Merged
nkolev92 merged 14 commits into
devfrom
dev-nkolev92-packaging-phase-4
Mar 18, 2026
Merged

nkolev92 merged 14 commits into
devfrom
dev-nkolev92-packaging-phase-4

Conversation

@nkolev92

@nkolev92 nkolev92 commented Mar 16, 2026 •

Copy link
Copy Markdown
Member

Bug

Progress: NuGet/Home#14778

Description

Continuation of #7210.
These changes cover all the packaging creation APIs.
This is the most complex PR to date in the nullability enabling.

  • As I had mentioned in Enabling nullability in NuGet.Packaging - phase 1, models and simple classes #7176, there are some types which are practically never null, but allow nulls for package creation purposes.
    There are a bunch of places where we'll better off purposely annotating incorrectly, so I'm adding a keyword to help find those instances, NU_NULL_INC.
  • IPackageMetadata is annotated with everything nullable, but the PackageBuilder implementation for example makes Id to not nullable.
    Note that this is ok and intentional as implementers can have stricter nullability restrictions.
  • LicenseMetadata.LicenseUrl is actually never null and fixed that up. It was null because of a default case in a switch statement.
  • I added required to the properties in types where it'd be helpful to enforce non-nullability such as ManifestContentFiles.Include.
  • PhysicalPackageFile - NuGetFramework & TargetFramework are now auto properties.
  • Path.GetDirectoryName has an interesting syntax that has led me to suppress the nullability warnings after calls when the path we pass is not null (majority of the cases, it'd null ref anyways):

After this, signing APIs are the only thing that's left.

PR Checklist

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

@nkolev92
nkolev92 marked this pull request as ready for review March 16, 2026 20:19
@nkolev92
nkolev92 requested a review from a team as a code owner March 16, 2026 20:19
@nkolev92
nkolev92 requested a review from jeffkl March 17, 2026 18:10
@nkolev92
nkolev92 force-pushed the dev-nkolev92-packaging-phase-4 branch from f842484 to a0cce42 Compare March 17, 2026 18:11
@jeffkl
jeffkl requested a review from Copilot March 17, 2026 22:44

Copilot AI 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.

Pull request overview

This PR continues the NuGet.Packaging nullability enablement work by updating package-creation and related packaging/signing APIs to be nullable-annotated, adjusting implementations to satisfy the new contracts, and updating PublicAPI listings accordingly.

Changes:

  • Removed #nullable disable across package creation/signing/rules code and annotated APIs with ?, attributes, and null-suppression where needed.
  • Introduced stricter construction contracts via required on select model properties.
  • Updated PublicAPI shipped/unshipped files for net472/net8.0 and adjusted/added a few call sites/tests for the new nullability behavior.

Reviewed changes

Copilot reviewed 51 out of 51 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
test/NuGet.Core.Tests/NuGet.Packaging.Test/SigningTests/RepositorySignatureInfoUtilityTests.cs Updates tests to pass empty certificate list instead of null.
src/NuGet.Core/NuGet.Packaging/Signing/SignatureLog.cs Enables nullability and updates Equals signature/nullable properties.
src/NuGet.Core/NuGet.Packaging/Signing/SignatureException.cs Enables nullability and marks optional properties nullable.
src/NuGet.Core/NuGet.Packaging/Signing/RepositorySignatureInfoProvider.cs Adds nullability annotations to TryGet... out parameter with [NotNullWhen].
src/NuGet.Core/NuGet.Packaging/Signing/RepositorySignatureInfo.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Signing/Oids.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/UpholdBuildConventionRule.cs Nullability updates in rule implementation and comparer.
src/NuGet.Core/NuGet.Packaging/Rules/UnspecifiedDependencyVersionRule.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/RuleSet.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/ReferencesInNuspecMatchRefAssetsRule.cs Adds null-safety (!/?) around XML traversal and collections.
src/NuGet.Core/NuGet.Packaging/Rules/PathTooLongRule.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/MisplacedAssemblyUnderLibRule.cs Nullability updates around path handling.
src/NuGet.Core/NuGet.Packaging/Rules/LicenseUrlDeprecationWarning.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/InvalidUndottedFrameworkRule.cs Adds null-safety to XML traversal and property extraction.
src/NuGet.Core/NuGet.Packaging/Rules/InvalidPrereleaseDependencyRule.cs Tightens Validate to treat builder as non-null.
src/NuGet.Core/NuGet.Packaging/Rules/InvalidPlaceholderFileRule.cs Nullability updates around path handling.
src/NuGet.Core/NuGet.Packaging/Rules/InitScriptNotUnderToolsRule.cs Nullability updates around path handling.
src/NuGet.Core/NuGet.Packaging/Rules/IconUrlDeprecationWarning.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/Rules/DependenciesGroupsForEachTFMRule.cs Adds null-safety around XML root usage.
src/NuGet.Core/NuGet.Packaging/Rules/DefaultManifestValuesRule.cs Adds null-suppression when emitting warnings.
src/NuGet.Core/NuGet.Packaging/PublicAPI/net8.0/PublicAPI.Unshipped.txt Updates unshipped API entries to reflect nullable annotations.
src/NuGet.Core/NuGet.Packaging/PublicAPI/net8.0/PublicAPI.Shipped.txt Updates shipped API entries to reflect nullable annotations.
src/NuGet.Core/NuGet.Packaging/PublicAPI/net472/PublicAPI.Unshipped.txt Updates unshipped API entries to reflect nullable annotations.
src/NuGet.Core/NuGet.Packaging/PublicAPI/net472/PublicAPI.Shipped.txt Updates shipped API entries to reflect nullable annotations.
src/NuGet.Core/NuGet.Packaging/PackageExtractor.cs Updates call site to match new nullability of repository signature APIs.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Xml/PackageMetadataXmlExtensions.cs Nullability updates and safer attribute construction when writing nuspec XML.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Utility/PackageIdValidator.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Utility/FrameworkNameUtility.cs Makes framework parsing APIs nullable and applies null-suppression for directory parsing.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Extensions/XElementExtensions.cs Enables nullability and adjusts signatures to allow nullable inputs/outputs.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Extensions/StringExtensions.cs Enables nullability and marks legacy helper as obsolete.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Extensions/FrameworksExtensions.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Extensions/CollectionExtensions.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/PhysicalPackageFile.cs Introduces nullable/required changes and converts framework fields to auto-properties.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/PackageReferenceSet.cs Enables nullability for target framework and related constructors.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/PackageBuilder.cs Broad nullability enablement across package creation APIs and internal helpers.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestVersionUtility.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestSchemaUtility.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestReader.cs Enables nullability and adjusts XML parsing and model population accordingly.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestMetadata.cs Enables nullability and adjusts validation/merge logic accordingly.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestFile.cs Enables nullability for file entries and validation.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestContentFiles.cs Makes Include required and enables nullability on optional fields.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/Manifest.cs Enables nullability and adjusts file list handling.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/LicenseMetadata.cs Makes LicenseUrl non-null and updates behavior for unsupported license types.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/IPackageMetadata.cs Updates metadata interface to nullable where appropriate.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/IPackageFile.cs Updates file interface to nullable for framework/effective path where appropriate.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/FrameworkAssemblyReference.cs Removes #nullable disable as part of nullability enablement.
src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/EmptyFrameworkFolderFile.cs Adds [SetsRequiredMembers] to align with required base members.
src/NuGet.Core/NuGet.Packaging/NuspecReader.cs Removes SafeTrim usage and aligns license parsing with nullability.
src/NuGet.Core/NuGet.Packaging/NupkgMetadata/NupkgMetadataFile.cs Introduces required on ContentHash and enables nullability.
src/NuGet.Core/NuGet.Packaging/Core/PackageIdentity.cs Adds NU_NULL_INC marker to intentional nullable-contract mismatch.
src/NuGet.Core/NuGet.Commands/RestoreCommand/DependencyGraphResolver.cs Adds null-guard when aggregating suppressions.
Comments suppressed due to low confidence (1)

src/NuGet.Core/NuGet.Packaging/Rules/MisplacedAssemblyUnderLibRule.cs:29

  • Path.GetDirectoryName(file) can return null when file is at the package root (e.g. readme.txt). The null-forgiving operator here will lead to a NullReferenceException when calling directory.Equals(...). Consider using var directory = Path.GetDirectoryName(file) ?? string.Empty; (or skipping files with no directory).

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@nkolev92
nkolev92 merged commit 15dafc4 into dev Mar 18, 2026
16 checks passed
@nkolev92
nkolev92 deleted the dev-nkolev92-packaging-phase-4 branch March 18, 2026 17:55
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.

5 participants