Skip to content

Migrate LegacyPackageReferenceProject to PackageSpecFactory - #6670

Closed
zivkan wants to merge 5 commits into
devfrom
dev-zivkan-PackageSpecFactory-VS-Legacy
Closed

zivkan wants to merge 5 commits into
devfrom
dev-zivkan-PackageSpecFactory-VS-Legacy

Conversation

@zivkan

@zivkan zivkan commented Aug 1, 2025

Copy link
Copy Markdown
Member

Bug

working towards: NuGet/Home#14160

Description

Migrate non-SDK style projects in VS to use the same PackageSpecFactory as static graph restore (and eventually everything else), so we have less duplicated code.

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.

While working on this, I found that I didn't enable null checking on all PackageSpecFactory's interfaces, so I fixed that, which required some changes to null annotations to make right.

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.

@zivkan
zivkan requested a review from a team as a code owner August 1, 2025 07:38
@zivkan
zivkan requested review from Nigusu-Allehu and jeffkl August 1, 2025 07:38

var items = _projectAdapter.GetBuildItemInformation(itemType, metadataNames);
var result = new List<IItem>(items is ICollection collection ? collection.Count : 0);
foreach (var (itemId, itemMetadata) in items)

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.

Is this how it used to work? It seems better to make this wrap the original dictionary for lazy lookups rather than make a copy of the dictionary since not every metadata item is read?

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.

There's no "original" dictionary to wrap. We get an array of values, and need to use an array of names to understand which value matches what metadata.

Only PackageReference and ProjectReference get more than 1 metadata value.

Getting PackageVersion items and PrunePackageReference items use item.ItemMetadata.FirstOrDefault() to avoid needing look at keys to get the value.

Meanwhile, ProjectReference and PackageReference expose the two Array objects returned from the VS APIs, and then the callers use this helper method to first find the index in the metadata names array, then look up the value.

So, the existing code uses less memory, but does a linear search each lookup.

return settings.GetConfigFilePaths();
}


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.

nit: extra new line.

@zivkan
zivkan requested review from jeffkl and nkolev92 August 5, 2025 09:33

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

There are compiler errors, I'm going to just leave this comment to see if I get notified when you push a fix...

nkolev92
nkolev92 previously approved these changes Aug 5, 2025
@zivkan
zivkan marked this pull request as draft August 5, 2025 23:19
@dotnet-policy-service dotnet-policy-service Bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Aug 12, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 7 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants