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

Do not warn http sources in package reference restore when allowInsecureConnections is set to true #5390

Conversation

heng-liu
Copy link
Contributor

@heng-liu heng-liu commented Aug 30, 2023

Bug

Fixes: NuGet/Home#12787

Regression? Last working version:

Description

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

@heng-liu heng-liu changed the base branch from dev to dev-feature-optoutHttpWarnings August 30, 2023 21:35
@heng-liu heng-liu marked this pull request as ready for review August 30, 2023 21:36
@heng-liu heng-liu requested a review from a team as a code owner August 30, 2023 21:36
IAssetsLogMessage logMessage = result.LockFile.LogMessages[0];
logMessage.Code.Should().Be(NuGetLogCode.NU1803);
Assert.Contains(expectedWarning, logMessage.Message);
Assert.DoesNotContain(unExpectedWarning, logMessage.Message);
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that this test is thorough, but is it clearer to just assert Equals on the expected warning, then drop the unexpected warning completely?

Suggested change
Assert.DoesNotContain(unExpectedWarning, logMessage.Message);

Copy link
Member

Choose a reason for hiding this comment

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

If you mean why not do Assert.Equals("NU1234: This is the expected message"), then that's high risk of causing problems for people who wish to contribute to NuGet but don't use English as their locale: NuGet/Home#12820. The risk will significantly increase if we eventually do localization more like the rest of github.com/dotnet repos, since local builds can have the satellite assemblies.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you mean why not do Assert.Equals("NU1234: This is the expected message")

No, I do not mean that. I expect we would continue using the localized variable.

Copy link
Contributor

@donnie-msft donnie-msft Sep 6, 2023

Choose a reason for hiding this comment

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

Looking at the latest commits and this thread, this hasn't been addressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing! Sorry for taking long to address this. This is fixed.

logMessage.Code.Should().Be(NuGetLogCode.NU1803);
logMessage.Message.Should().Be("You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.");

string expectedWarning = "You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.";
Copy link
Contributor

Choose a reason for hiding this comment

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

To reduce copying the HTTP source as well as emphasize that your test is expecting the HTTP source URL, I think a variable here would be clearer:

Suggested change
string expectedWarning = "You are running the 'restore' operation with an 'HTTP' source, 'http://api.source/index.json'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.";
string expectedWarning = $"You are running the 'restore' operation with an 'HTTP' source, '{testHttpSourceUrl}'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source.";

Copy link
Contributor

@donnie-msft donnie-msft Sep 6, 2023

Choose a reason for hiding this comment

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

Looking at the latest commits and this thread, this hasn't been addressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! This is fixed.

@@ -214,6 +214,9 @@ private static ExternalProjectReference GetExternalProject(PackageSpec rootProje
// Add project references
request.ExternalProjects = projectReferenceClosure.ToList();

//Update the RestoreRequest.Project.RestoreMetadata.Sources to get the attributes from settings(e.g. AllowInsecureConnections).
request.Project.RestoreMetadata.Sources = sources.Select(s => s.PackageSource).ToList();
Copy link
Member

Choose a reason for hiding this comment

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

This was my bad @heng-liu, I should've done this investigation the first time I provided feedback.

I think we need a slight change here.
I think my original implementation that I added here: 4d2c5a4 is not ideal.

Some background:

  • PackageSpec, or rather Project is considered to be the model for restore, the input. This will be created differently on the commandline (static graph vs standard) vs Visual Studio. It has a sources property, which is the processed sources based on the project path, RestoreSources msbuild property etc. It's the actual end sources. The general expectation is that the PackageSpec does not get modified. It is basically what is used for no-op.
  • RestoreRequest has a list of DependencyProviders.
    These dependency providers are "shared" across projects, created within this classl,
    var sharedCache = _providerCache.GetOrCreate(
    globalPath,
    fallbackPaths.AsList(),
    sources,
    restoreArgs.CacheContext,
    restoreArgs.Log,
    updateLastAccess);
    . It basically ensures that restore for every project shares the same providers, the same SourceRepository and like resources. These dependency provides also happen to have repositories, and PackageSource that should match the ones from the settings file. Hence the whole GetEffectiveSources thing.

When I added the original implementation, I used _request.Project.RestoreMetadata.Sources. I think that's incorrect. Those are not how the sources are used, they are used through request.DependencyProviders which is effectively based on the PackageSource list from the package spec.

I should've used that and it's already used in a bunch of places, like for example, detecting http sources for telemetry,

int httpSourcesCount = _request.DependencyProviders.RemoteProviders.Where(e => e.IsHttp).Count();
.

Basically, PackageSpec on the request is expected to be immutable, see: NuGet/Home#9041 (comment). We basically clone it a bunch of time because of fear of these types of changes.

Now this is a thing you've never caught yourself, and it's not incorrect to do this, but I think it'll be a smaller change if you just change it to use the dependencyproviders.

You can try that change and see how it goes, tests might break. Like, not sure if everything is mocked 100%. If that becomes the case, please lmk and I can help or just do the change from using the package spec metadata to the dependencyprovider once, since I caused the pain anyways :)

Copy link
Member

Choose a reason for hiding this comment

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

I think the review has been re-requested mistakenly,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @nkolev92 for the detailed contexts! (I was wondering why the tests for no-op failed previously.)
I changed to use RemoteProviders in a testing branch and the build shows the newly changed test failed. There is no warning in result.LockFile.LogMessages so it failed at this line
However, I can see the warning from the console when running the commandline.
Do you have any suggestions about fixing this test? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

var sources = new List<PackageSource> { new PackageSource(pathContext.PackageSource) };
has a bug. It's using a hardcoded list where it's using the PathContext source instead of the ones from the package spec. It should be using the PackageSpec ones. I'll do some quick testing to see if I can have a fix.

Copy link
Member

Choose a reason for hiding this comment

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

I'd add a new CreateRestoreRequestMethod, where the source is spec.RestoreMetadata.Sources.

That makes the test pass. Other tests would fail if you make the change in project test helpers directly.
I know how to fix the ones I wrote, but there's many other tests that have package specs that are incomplete.

Copy link
Member

Choose a reason for hiding this comment

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

Actually if you replace that line with:

            var sources = spec.RestoreMetadata.Sources.Any() ?
                spec.RestoreMetadata.Sources :
                new List<PackageSource> { new PackageSource(pathContext.PackageSource) };

All tests seem to pass.

Copy link
Member

Choose a reason for hiding this comment

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

If you end up choosing that, please create an issue to fix the tests and assign to me. I think our test helpers code should use as much of the production code as possible. I'm hoping to do some changes in the ProjectTestHelpers that'll help us avoid these problems.

Copy link
Member

@nkolev92 nkolev92 Sep 7, 2023

Choose a reason for hiding this comment

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

I spent about an hour doing some refactoring: https://github.com/NuGet/NuGet.Client/tree/dev-nkolev92-cleanupTestHelpers. Feel free to make the change to fix your tests.

Copy link
Member

Choose a reason for hiding this comment

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

I have #5407 out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @nkolev92 for your help! Please take another look.

Copy link
Contributor

@donnie-msft donnie-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 re-reviewed as requested by @jeffkl, but after looking at the latest commits and my unresolved threads, there are unaddressed comments by me.

@heng-liu heng-liu force-pushed the dev-hengliu-optoutHttpWarnings-packageReferrenceRestore branch from 7a9ce43 to 6f8a363 Compare September 8, 2023 01:09
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.

🚢

@@ -239,11 +239,12 @@ public async Task<RestoreResult> ExecuteAsync(CancellationToken token)
_success = false;
}

if (_request.Project?.RestoreMetadata != null)
if (_request.DependencyProviders.RemoteProviders != null)
Copy link
Member

Choose a reason for hiding this comment

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

Excellent!

Thanks for fixing my mistake :)

@heng-liu heng-liu merged commit 47abced into dev-feature-optoutHttpWarnings Sep 11, 2023
@heng-liu heng-liu deleted the dev-hengliu-optoutHttpWarnings-packageReferrenceRestore branch September 11, 2023 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants