Skip to content

Fix package restore cycle detection#9051

Merged
drewnoakes merged 20 commits intodotnet:mainfrom
drewnoakes:fix-9050-restore-cycle-detection
May 31, 2023
Merged

Fix package restore cycle detection#9051
drewnoakes merged 20 commits intodotnet:mainfrom
drewnoakes:fix-9050-restore-cycle-detection

Conversation

@drewnoakes
Copy link
Copy Markdown
Member

@drewnoakes drewnoakes commented May 30, 2023

Fixes #9050

In #8239 we added a feature that aimed to detect NuGet package restore loops, and to stop them after a certain number of iterations to prevent endless resource usage.

It turns out that feature never worked due to using a byte[] as a key in a dictionary.

This PR:

  • Fixes the byte[] hashing issue by wrapping the byte[] with a new Hash type that adds Equals and GetHashCode, makes the type immutable and provides a little more type safety.
  • Pulls all cycle detection code out into a single component, simplifying the PackageRestoreDataSource class somewhat.
  • Bumps the compiler version we use so that we can start dogfooding C# 12 bits, and uses a primary constructor in two places.
  • Changes the message displayed to the user to be hopefully clearer and specify exactly which project is causing the restore failure, which should make further investigation easier.
  • Adds some unit tests for the restore detection. Writing these, I'm not sure the detection algorithm we use is the best fit. We will monitor telemetry and listen for feedback on false positives/negatives and iterate as needed.
  • Updates namespaces across the code from an incomplete move of code from the VS layer to the host-agnostic layer in Move PackageRestoreDataSource to Microsoft.VisualStudio.ProjectSystem.Managed #8850, so that everything's consistent. Moves some unit test code in the same direction too.
  • Removes some redundant using directives in the language service code that started appearing as warnings.

Before
image

After
image

Microsoft Reviewers: Open in CodeFlow

drewnoakes added 14 commits May 30, 2023 12:35
Also remove redundant `Size` property.
Calling `Start` on a running `Stopwatch` does nothing. The prior code could start, then return early, then potentially re-enter and misrepresent the duration of the restore. Calling `Restart` ensures the value starts from zero each time and we report the correct duration.
Allows dogfooding new C# 12 features.
The previous code was storing `byte[]` as a key in a dictionary, without specifying a comparer. `byte[]` only provides reference equality by default, not structural equality.

To fix this issue, we wrap the `byte[]` with a new `Hash` type that provides the appropriate hashing/equality checks for the type, makes its value immutable, and offers better type safety than the previous code.
Simplify the message and identify the problematic project.
All other types in this folder are named `PackageRestore*`. This should be no different.
There's no runtime difference here. I just find the code easier to read this way.
This consolidates all cycle detection logic in its own class, simplifying the `PackageRestoreDataSource` class.
@drewnoakes drewnoakes added Bug Tenet-User Friendly This issue affects the "User Friendly" tenet; UI usability, accessibility or high-DPI related. Feature-NuGet NuGet integration including pushing it properties, project and package references, and Pack support. labels May 30, 2023
@drewnoakes drewnoakes added this to the 17.7 milestone May 30, 2023
@drewnoakes drewnoakes requested a review from a team as a code owner May 30, 2023 10:52
At some point a bunch of code was moved from the VS layer into the host-agnostic layer. However the namespaces were not updated, and the unit tests were not moved.

This commit finalizes that move and makes everything consistent. All these types are internal, so there are no outside dependencies for which moving these types between namespaces would be an issue.
@drewnoakes drewnoakes merged commit 4f67759 into dotnet:main May 31, 2023
@drewnoakes drewnoakes deleted the fix-9050-restore-cycle-detection branch May 31, 2023 03:41
@drewnoakes drewnoakes added Tenet-Performance This issue affects the "Performance" tenet. Performance-Scenario-General This issue affects performance in general. labels Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature-NuGet NuGet integration including pushing it properties, project and package references, and Pack support. Performance-Scenario-General This issue affects performance in general. Tenet-Performance This issue affects the "Performance" tenet. Tenet-User Friendly This issue affects the "User Friendly" tenet; UI usability, accessibility or high-DPI related.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NuGet restore cycle detection doesn't work

3 participants