Skip to content

Use Package Source name as key when updating nuget.config, to avoid duplicates#5334

Merged
zivkan merged 3 commits intodevfrom
dev-zivkan-disable-source-with-protocolVersion
Aug 8, 2023
Merged

Use Package Source name as key when updating nuget.config, to avoid duplicates#5334
zivkan merged 3 commits intodevfrom
dev-zivkan-disable-source-with-protocolVersion

Conversation

@zivkan
Copy link
Member

@zivkan zivkan commented Jul 26, 2023

Bug

Fixes: NuGet/Home#12776
Fixes: NuGet/Home#10015

Regression? no

Description

When updating nuget.config's package sources (for example, to disable package one), the "name" should be a "key" used to match two SourceItems. Same name, then update the source, whether it's a change in value or protocolVersion.

Changed some tests, because in my opinion the old behaviour didn't make any sense.

Deleted IPackageSourceProvider2 and related code, because it was a codespaces change that 1. didn't work and 2. the people working on it marked it as obsolete rather than deleting it, and then never got around to deleting it as they had planned. This seems like a good a time as any to delete it, since it's impacting this PR's other changes.

PR Checklist

  • PR has a meaningful title

  • PR has a linked issue.

  • Described changes

  • Tests

    • Automated tests added
    • OR
    • Test exception
    • OR
    • N/A
  • Documentation

    • Documentation PR or issue filled
    • OR
    • N/A

martinrrm
martinrrm previously approved these changes Jul 26, 2023
Copy link
Contributor

@martinrrm martinrrm left a comment

Choose a reason for hiding this comment

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

LGTM. I agree that old behavior is weird. Name has the key attribute in XML element, using protocolVersion as a second key feels wrong.

jebriede
jebriede previously approved these changes Jul 27, 2023
Copy link
Contributor

@jebriede jebriede left a comment

Choose a reason for hiding this comment

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

This is a good change since the protocol version shouldn't make the entry unique from one that doesn't have that attribute specified. @zivkan: What happens when a package source has a protocolVersion="3" and a user opens the Tools -> Options -> Package Source page, then just clicks OK? With this change, it should no longer be adding a second entry in the nuget.config, but will it update the package source item and remove the protocolVersion attribute? Or will it leave that attribute alone? It would be good to know what is happening in that scenario to see if it's expected behavior or if we have to make changes there as well.

@jebriede
Copy link
Contributor

jebriede commented Jul 27, 2023

This is a good change since the protocol version shouldn't make the entry unique from one that doesn't have that attribute specified. @zivkan: What happens when a package source has a protocolVersion="3" and a user opens the Tools -> Options -> Package Source page, then just clicks OK? With this change, it should no longer be adding a second entry in the nuget.config, but will it update the package source item and remove the protocolVersion attribute? Or will it leave that attribute alone? It would be good to know what is happening in that scenario to see if it's expected behavior or if we have to make changes there as well.

I just tested this branch with a nuget.config that has just nuget.org with protocolVersion="3" and if I make an unrelated change in the Package Source Options dialog, such as adding another package source and click OK, it removes protocolVersion="3" from the nuget.org entry in the nuget.config. This feels wrong to me. If we aren’t supporting the PackageVersion property in the Package Sources UI, we should probably not be removing it either then unless this was a conscious design decision we made at some point.

@aortiz-msft aortiz-msft self-requested a review July 28, 2023 16:16
Copy link
Contributor

@aortiz-msft aortiz-msft left a comment

Choose a reason for hiding this comment

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

I just tested this branch with a nuget.config that has just nuget.org with protocolVersion="3" and if I make an unrelated change in the Package Source Options dialog, such as adding another package source and click OK, it removes protocolVersion="3" from the nuget.org entry in the nuget.config. This feels wrong to me. If we aren’t supporting the PackageVersion property in the Package Sources UI, we should probably not be removing it either then unless this was a conscious design decision we made at some point.

Looks like we need a CompareTo method for PackageSource so we can safely ignore the ProtocolVersion in this case as part of the comparison.

@zivkan
Copy link
Member Author

zivkan commented Jul 31, 2023

Unfortunately, a new CompareTo method isn't going to solve all problems. The bug the PR fixes (a duplicate source being added to the config) is more or less the same code that causes protocolVersion to be removed with this PR's current fix.

The package source options page ultimately calls into PackageSourceProvier.SavePackageSources with a list of all package sources. The options page has stripped the protocolVersion out of the package source, so from NuGet.Configuration's point of view, it's doing the correct thing removing the protocolVersion from source, since that's presisely what the VS code is asking it to do.

To fix this, we'd have to at the very minimum make the VS options page remember the protocolVersion from package sources loaded from config. A better solution would be to add protocolVersion to the VS options page, but that's a much, much bigger change. Which should be done eventually, but do we want to migrate the page to WPF before or after making such a change?

I'll investigate having options just remember the previous protocolVersion. Hopefully it's not a major change, but this is one of the older parts of our code (after all it's still using WinForms) that hasn't had as much refactoring/modernization over the years.

jeffkl
jeffkl previously approved these changes Jul 31, 2023
@zivkan zivkan dismissed stale reviews from jeffkl, jebriede, and martinrrm via c747bc0 August 1, 2023 13:34
@zivkan zivkan force-pushed the dev-zivkan-disable-source-with-protocolVersion branch 2 times, most recently from c747bc0 to 86d3881 Compare August 1, 2023 13:57
Comment on lines +330 to +324
~override sealed NuGet.Configuration.AddItem.Equals(object other) -> bool
override sealed NuGet.Configuration.AddItem.GetHashCode() -> int
Copy link
Member Author

Choose a reason for hiding this comment

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

These are breaking changes in the Client SDK, with comments in AddItem.cs explaining why it's important. The bug prevented by making these methods sealed is subtle and these methods need to be implemented in a specific way that goes against normal coding conventions, hence the risk of defects is high (especially when the person implementing tests validate buggy behaviour 🤦)

Copy link
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.

Looks reasonable. There's 1 bug I see, but there's also some compile errors rn.

@zivkan zivkan force-pushed the dev-zivkan-disable-source-with-protocolVersion branch from 86d3881 to e0e743e Compare August 2, 2023 12:52
@zivkan zivkan changed the title Config's SourceItem.Equals should consider name only Use Package Source name as key when updating nuget.config, to avoid duplicates Aug 3, 2023
@zivkan zivkan requested a review from aortiz-msft August 5, 2023 07:35
@zivkan zivkan merged commit 3dbfac4 into dev Aug 8, 2023
@zivkan zivkan deleted the dev-zivkan-disable-source-with-protocolVersion branch August 8, 2023 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

8 participants