Skip to content

Add NU5052 pack warning for non-restricted package IDs#7487

Open
nkolev92 wants to merge 8 commits into
devfrom
dev-nkolev92-nu5052-restricted-package-id-warning
Open

Add NU5052 pack warning for non-restricted package IDs#7487
nkolev92 wants to merge 8 commits into
devfrom
dev-nkolev92-nu5052-restricted-package-id-warning

Conversation

@nkolev92

@nkolev92 nkolev92 commented Jun 17, 2026

Copy link
Copy Markdown
Member

Bug

Progress: NuGet/Home#14949

Description

Design at NuGet/Home#14950.
I am marking this PR as ready for review as the main framework of the PR has been approved.
The only open questions are around whether we put this in 10.0.400 or whether 11.0.100 is good enough.
Also as of right now, we're not enabling this for non SDK projects, so the open question around VS is not implemented. Once those are done, we can make tweaks either in this or a future PR.

Implements pack warning NU5052 when a package ID does not adhere to the restricted character set. Package IDs must start with a letter, digit, or underscore and contain only ASCII letters, digits, dots (.), dashes (-), and underscores (_) with no consecutive dots or dashes. The warning is gated by SdkAnalysisLevel >= 11.0.100.

Design decisions

Char-loop over regex for restricted ID validation. The regex from the announcement (^[A-Za-z0-9_](?!.*[.-]{2})[A-Za-z0-9.-]{0,99}$) was attempted, but benchmarking against ~1,979 real package IDs showed the negative lookahead made it 4x slower than the existing IdRegex baseline:

Method Mean Ratio
IdRegex_Only (baseline) 560.3 µs 1.00
IdRegex + RestrictedRegex 2,237.6 µs 4.00
IdRegex + CharLoop 645.7 µs 1.15

Since IdRegex already runs during pack validation, the new IsValidPackageId(string, bool useRestrictedCharacterSet) overload reuses that result and adds only a char-loop — +15% overhead instead of 4x.

Copilot instructions updated. Added guidance to docs/ so that SdkAnalysisLevel gating is done correctly via SdkAnalysisLevelMinimums.IsEnabled() in code.

Public API

  • PackageIdValidator.IsValidPackageId(string packageId, bool useRestrictedCharacterSet) — new overload on NuGet.Packaging

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 - Document NU5052 Home#14953.

nkolev92 and others added 7 commits June 17, 2026 11:24
Emit NU5052 warning during pack when a package ID does not adhere to
the restricted character set (ASCII letters, digits, dots, dashes,
underscores with no consecutive dots or dashes). The warning is gated
by SdkAnalysisLevel >= 11.0.100 using SdkAnalysisLevelMinimums.IsEnabled().

- Add PackageIdValidator.IsRestrictedPackageId() with restricted regex
- Emit NU5052 in PackCommandRunner.BuildPackage() before builder.Save()
- Plumb SdkAnalysisLevel/UsingMicrosoftNETSdk from MSBuild targets
  through PackTask/PackTaskLogic to PackArgs
- Add unit tests for IsRestrictedPackageId and integration tests for
  NU5052 emission with SdkAnalysisLevel gating

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace the separate IsRestrictedPackageId() method and its regex with
an IsValidPackageId(string, bool useRestrictedCharacterSet) overload
that reuses the existing IdRegex and adds a fast char-loop for the
restricted character check. Benchmarks showed the regex approach was
4x slower than baseline while the char-loop adds only ~15% overhead.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rename NonRestrictedPackageId to PackageIdWithInvalidCharacters for
clarity. Consolidate the four SdkAnalysisLevel gating tests into a
single Theory with MemberData.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace MemberData with InlineData for the gating theory. Add tests
verifying that NoWarn suppresses NU5052 and TreatWarningsAsErrors
elevates it to an error.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Merge the two separate NU5052 theories into one with all dimensions
(packageId, sdkAnalysisLevel, usingMicrosoftNETSdk, expectWarning).
Remove restricted PackageIdValidator tests that duplicate existing
IsValidPackageId tests (consecutive separators, invalid start char,
null).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verify that dotnet pack with a non-ASCII PackageId emits NU5052 on
.NET 11 (SDK_NEXT) and does not on .NET 10.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Only emit the restricted package ID warning (NU5052) when
UsingMicrosoftNETSdk is true. Non-SDK projects (nuget.exe pack)
do not emit this warning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nkolev92 nkolev92 marked this pull request as ready for review June 17, 2026 23:11
@nkolev92 nkolev92 requested a review from a team as a code owner June 17, 2026 23:11
@nkolev92 nkolev92 requested review from jebriede and jeffkl June 17, 2026 23:11
jeffkl
jeffkl previously approved these changes Jun 18, 2026

@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.

The language of the warning can be improved, I put my suggestion in the hat for consideration.

Comment thread src/NuGet.Core/NuGet.Commands/Strings.resx Outdated
Comment thread src/NuGet.Core/NuGet.Common/Errors/NuGetLogCode.cs
Comment thread src/NuGet.Core/NuGet.Packaging/PackageCreation/Utility/PackageIdValidator.cs Outdated
@nkolev92

Copy link
Copy Markdown
Member Author

@copilot address all feedback.

Copilot AI commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

@copilot address all feedback.

Addressed feedback in commit f636a1e:

  • Updated NU5052 warning wording in Strings.resx and regenerated corresponding .xlf files.
  • Refactored IsAsciiLetterOrDigit into a local function within IsRestrictedId().
    I could not find an existing NuGet/Home docs-tracking issue for NU5052 via search; if you want, I can wire in a specific issue link once one is provided.

@nkolev92 nkolev92 requested a review from jeffkl June 18, 2026 20:14

@jebriede jebriede 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.

Approved with suggestion.

Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

// Warn if the package ID doesn't adhere to the restricted character set (NU5052)
if (!symbolsPackage && _packArgs.UsingMicrosoftNETSdk && !PackageIdValidator.IsValidPackageId(builder.Id, useRestrictedCharacterSet: true))

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.

nit: IsValidPackageId runs before the cheaper IsEnabled gate, so we validate even
when the warning is suppressed. Checking IsEnabled first would skip that work and let
the two nested ifs collapse into one.

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