Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package namespace configuration reading and processing logic #4009

Merged

Conversation

nkolev92
Copy link
Member

@nkolev92 nkolev92 commented Apr 20, 2021

Bug

Fixes: NuGet/Home#10725

Regression? Last working version:

Description

Design at https://github.com/NuGet/Home/blob/package-namespaces/proposed/2021/PackageNamespaces.md.

Add the required models to read the current iteration of the configuration for namespaces.

This adds a few things:

  • Items for namespace and packageSource under packageNamespaces.
  • Reading and updating provider for namespaces.
  • A configuration model that could be used during restore time.

Note that these APIs are not final, especially the PackageNamespaceConfiguration one. I just don't want to overdo it before we under the needs of the APIs better.

Wait a feature branch?

Yes - https://github.com/NuGet/Client.Engineering/pull/910

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

@nkolev92 nkolev92 requested a review from a team as a code owner April 20, 2021 17:40
Copy link
Contributor

@dominoFire dominoFire left a comment

Choose a reason for hiding this comment

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

Looks good. Some comments on constructor.

return newItem;
}

public override bool Equals(object other)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to implement IEquatable<> ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Technically, there is no need for it.
In practice, it'd only save in boxing. Not sure it's be a huge gain in the context of configurations.

Copy link
Contributor

Choose a reason for hiding this comment

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

At least for me, it helps me in understanding code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that PackageSource element implements IEquatable interface

public class PackageSource : IEquatable<PackageSource>

Copy link
Member Author

@nkolev92 nkolev92 Apr 21, 2021

Choose a reason for hiding this comment

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

Not sure how implementing IEquatable improves code understanding here?

I see that PackageSource element implements IEquatable interface

I get that. If we look at many other items like:

https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Configuration/Settings/Items/AddItem.cs
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Configuration/Settings/Items/AuthorItem.cs
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Configuration/Settings/Items/CertificateItem.cs
https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Configuration/Settings/Items/ClearItem.cs

They don't implement it. So precedent is spotty.

IEquatable was primarily added for structs, where Equals compares the ref instead.

@nkolev92
Copy link
Member Author

Thanks @dominoFire, addressed the feedback.

Copy link
Contributor

@kartheekp-ms kartheekp-ms left a comment

Choose a reason for hiding this comment

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

Great start Nikolche. Few comments to start with.

_settings.Remove(ConfigurationConstants.PackageNamespaces, packageSourceNamespace);
}
// An error means the item doesn't exist or is in a machine wide config, therefore just ignore it
catch { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
catch { }
catch (InvalidOperationException) { }

Can we catch InvalidOperationException instead of a generic exception because _settings.Remove throws only InvalidOperationException exception in few cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

It could throw some IO exceptions if the config is not editable.
We wouldn't do anything with the exception anyways.

{
public override string ElementName => ConfigurationConstants.Namespace;

public string Id
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should add additional validation to Id attribute because it is a prefix of Package ID which has few constraints.

public const int MaxPackageIdLength = 100;
private static readonly Regex IdRegex = new Regex(pattern: @"^\w+([.-]\w+)*$",
options: RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant,
matchTimeout: TimeSpan.FromSeconds(10));

Copy link
Member Author

Choose a reason for hiding this comment

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

I meant to create a follow up issue for this.

Created an issue: NuGet/Home#10783

A couple of reasons:

  • I want to unblock the next set of work.
  • We haven't explicitly designed for the invalid namespace experience. Do we fail early? Warn and continue? Ignore? We can cover this Thursday.
  • Given that this is a configuration only change, it wasn't super clear where the most convenient location to validate this is.

return newItem;
}

public override bool Equals(object other)
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that PackageSource element implements IEquatable interface

public class PackageSource : IEquatable<PackageSource>

@nkolev92
Copy link
Member Author

Ready for another review; @kartheekp-ms @dominoFire

Copy link
Contributor

@erdembayar erdembayar left a comment

Choose a reason for hiding this comment

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

Glad to see this one is going forward.
To start I have few comments.

return packageNamespacesSection.Items.OfType<PackageSourceNamespacesItem>().ToList();
}

public void Remove(IReadOnlyList<PackageSourceNamespacesItem> packageSourceNamespaces)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are remove/update scenarios are covered in spec?
In what scenarios is it going to happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

There will be a command soon. Tracked in NuGet/Home#10735.

Implementing the Get/Add/Update/Remove in one pass seemed like a natural extension.
Keep in mind that this API is currently internal only, so it carries no risk as of now.

We will need it by the time we ship the feature.

Copy link
Contributor

@dominoFire dominoFire left a comment

Choose a reason for hiding this comment

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

Signing off. Please address comment on string resource. Thanks!

@nkolev92 nkolev92 force-pushed the dev-nkolev92-namespacesConfiguration branch from cbce607 to 06362a5 Compare April 21, 2021 19:00
Copy link
Contributor

@erdembayar erdembayar left a comment

Choose a reason for hiding this comment

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

Good job.
One tiny comment.

@nkolev92 nkolev92 merged commit ea2814d into dev-feature-packageNamespaces Apr 22, 2021
@nkolev92 nkolev92 deleted the dev-nkolev92-namespacesConfiguration branch April 22, 2021 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants