dfederm / ReferenceTrimmer

Easily identify which dependencies can be removed from a .NET project
MIT License
183 stars 15 forks source link

InvalidOperationException: Sequence contains no matching element in CollectDeclaredReferencesTask #101

Closed KirillOsenkov closed 1 month ago

KirillOsenkov commented 1 month ago

Saw this when opening Gardener.sln without having it restored

C:\Users\kirillo\.nuget\packages\referencetrimmer\3.3.6\build\ReferenceTrimmer.targets (31,5): error MSB4018: The "CollectDeclaredReferencesTask" task failed unexpectedly.
System.InvalidOperationException: Sequence contains no matching element
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at ReferenceTrimmer.Tasks.CollectDeclaredReferencesTask.GetPackageInfos() in D:\a\ReferenceTrimmer\ReferenceTrimmer\src\Tasks\CollectDeclaredReferencesTask.cs:line 242
   at ReferenceTrimmer.Tasks.CollectDeclaredReferencesTask.Execute() in D:\a\ReferenceTrimmer\ReferenceTrimmer\src\Tasks\CollectDeclaredReferencesTask.cs:line 157
   at Microsoft.Build.BackEnd.TaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
KirillOsenkov commented 1 month ago

is it here?

https://github.com/dfederm/ReferenceTrimmer/blob/9b795d801cc2bbc8e71aca36638cd4fcc6b1a9fe/src/Tasks/CollectDeclaredReferencesTask.cs#L244

KirillOsenkov commented 1 month ago

Caught it under debugger by opening the binlog from the design-time build, right-click the CollectDeclaredReferencesTask -> Debug, then use a certain Smart Attach feature to attach to the MSBuild process, then debug ;)

packageFolders contains a single item C:\Users\kirillo\.nuget\packages\, nugetLibraryRelativePath == azure.core/1.38.0, azure.core in my cache contain 1.35 and 1.39, but no 1.38.

Should perhaps be resilient to this situation.

KirillOsenkov commented 1 month ago

I'll leave my repo in this state (won't restore) in case we want to debug this a bit more.

dfederm commented 1 month ago

I'm not sure I understand this state exactly. Doesn't this imply that the project.assets.json file specifies a package (azure.core/1.38.0 in your case), but the package doesn't exist on disk? So the restore didn't actually place the package?

Did you perhaps purge your cache sometime after restoring, and then didn't restore again?

KirillOsenkov commented 1 month ago

yes it's possible! I clear the package cache every once in a while.