Skip to content

Migrate LegacyPackageReferenceProject to PackageSpecFactory#7263

Open
zivkan wants to merge 5 commits intodevfrom
dev-zivkan-PackageSpecFactory-VS-Legacy
Open

Migrate LegacyPackageReferenceProject to PackageSpecFactory#7263
zivkan wants to merge 5 commits intodevfrom
dev-zivkan-PackageSpecFactory-VS-Legacy

Conversation

@zivkan
Copy link
Copy Markdown
Member

@zivkan zivkan commented Apr 8, 2026

Bug

working towards: NuGet/Home#14160

Description

I finally had time to update and fix #6670

Copied from that PR's description:

Legacy csproj has some quirks, because of Visual Studio APIs. We read items via IVsBuildItemStorage.FindItems, where it's necessary to pass it a list of item metadata that you're interested in. Therefore, at some point we need a complete list of metadata names per item type, which I added to LegacyProjectAdapter. In order to ensure this list is complete, I wrote a test that's rather long and seems complicated, but what it does is runs PackageSpecFactory.GetPackageSpec on a fake project adapter that ensures there's at least one item of every type (even types that we never expect in a non-SDK style project) and validates every metadata that PackageSpecFactory asks for is in the metadata names list. This way, if the metadata names dictionary is missing an item type, the test throws, or if the test didn't create at least one item of the type, the test throws, and finally if any of the item type metadata lists is missing a name, the test throws. This gives us confidence that the product code is not missing any metadata names.

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. N/A

@zivkan zivkan requested a review from a team as a code owner April 8, 2026 07:28
@zivkan zivkan requested review from jebriede and nkolev92 April 8, 2026 07:28
Copy link
Copy Markdown
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

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

Small comment, that's a bit opinionated, but curious what you think.

Lots of code, especially test code, so hopefully I looked at all the importance things.
lmk if there's something in particular that needs a more detailed look.

Comment thread src/NuGet.Clients/NuGet.VisualStudio.Common/IVsProjectAdapter.cs
@zivkan zivkan requested review from kartheekp-ms and nkolev92 April 10, 2026 21:19
nkolev92
nkolev92 previously approved these changes Apr 10, 2026
jeffkl
jeffkl previously approved these changes Apr 13, 2026
@zivkan zivkan dismissed stale reviews from jeffkl and nkolev92 via a08af57 April 15, 2026 05:21
@zivkan zivkan force-pushed the dev-zivkan-PackageSpecFactory-VS-Legacy branch from 4dc6d5a to a08af57 Compare April 15, 2026 05:21
@zivkan zivkan requested review from jeffkl and nkolev92 April 15, 2026 05:21
jeffkl
jeffkl previously approved these changes Apr 15, 2026
@nkolev92
Copy link
Copy Markdown
Member

you have a bad rebase @zivkan ping when you've fixed it.


// PackageDownload items can contain multiple versions
string versionRanges = projectItemInstance.GetMetadata("Version");
string? versionRanges = projectItemInstance.GetMetadata("Version");
Copy link
Copy Markdown
Contributor

@kartheekp-ms kartheekp-ms Apr 15, 2026

Choose a reason for hiding this comment

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

In line no: 687 we added a null check and returned string.Empty if the version is null. Should we do the same here also?

Suggested change
string? versionRanges = projectItemInstance.GetMetadata("Version");
string? versionRanges = projectItemInstance.GetMetadata("Version") ?? string.Empty;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On line 687, the value is passed to PrunePackageReference.Create, which does not allow null, no using the string.Empty as a default is required to make the compiler happy. But I argue is a bad customer experience because it's unlikely that PrunePackageReference will work correctly with an empty value. It's just that the feature was developed because nullable was enabled on this file, so during development handling non-defined values wasn't considered (the SDK provides the values from something that auto-generates, so the risk of the version not being defined is almost zero).

This on the other hand goes through a string.IsNullOrEmpty check on the very next line and then throws, so no, I don't believe there's any benefit to defaulting to the empty string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the SDK provides the values from something that auto-generates, so the risk of the version not being defined is almost zero.

Pretty sure PrunePackageReference would throw if a bad version is passed,

public static PrunePackageReference Create(string name, string version)
{
if (string.IsNullOrEmpty(version))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.Error_PrunePackageReferenceMissingVersion, name));
}
return new PrunePackageReference(name, VersionRange.Parse("(," + version + "]"));
}
.

@zivkan zivkan requested review from jeffkl and kartheekp-ms April 17, 2026 22:41
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