dependabot-nuget 0.251.0 → 0.253.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/helpers/lib/NuGetUpdater/Directory.Common.props +1 -0
  3. data/helpers/lib/NuGetUpdater/Directory.Packages.props +26 -0
  4. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli/Commands/DiscoverCommand.cs +35 -0
  5. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli/NuGetUpdater.Cli.csproj +1 -1
  6. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli/Program.cs +4 -7
  7. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Discover.cs +251 -0
  8. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Update.cs +27 -9
  9. data/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/NuGetUpdater.Cli.Test.csproj +3 -3
  10. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Dependency.cs +56 -1
  11. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/DependencyType.cs +1 -1
  12. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DirectoryPackagesPropsDiscovery.cs +69 -0
  13. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DirectoryPackagesPropsDiscoveryResult.cs +11 -0
  14. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DiscoveryWorker.cs +217 -0
  15. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DotNetToolsJsonDiscovery.cs +30 -0
  16. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DotNetToolsJsonDiscoveryResult.cs +10 -0
  17. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/GlobalJsonDiscovery.cs +30 -0
  18. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/GlobalJsonDiscoveryResult.cs +10 -0
  19. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/IDiscoveryResult.cs +14 -0
  20. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/PackagesConfigDiscovery.cs +29 -0
  21. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/PackagesConfigDiscoveryResult.cs +10 -0
  22. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/ProjectDiscoveryResult.cs +13 -0
  23. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs +128 -0
  24. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/WorkspaceDiscoveryResult.cs +13 -0
  25. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/EvaluationResult.cs +8 -0
  26. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/EvaluationResultType.cs +9 -0
  27. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/BuildFile.cs +6 -8
  28. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/DotNetToolsJsonBuildFile.cs +4 -7
  29. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/GlobalJsonBuildFile.cs +24 -17
  30. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/JsonBuildFile.cs +2 -2
  31. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/PackagesConfigBuildFile.cs +8 -13
  32. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/ProjectBuildFile.cs +100 -19
  33. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Files/XmlBuildFile.cs +2 -2
  34. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/NuGetUpdater.Core.csproj +6 -6
  35. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Property.cs +6 -0
  36. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/DotNetToolsJsonUpdater.cs +23 -36
  37. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/GlobalJsonUpdater.cs +5 -10
  38. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/SdkPackageUpdater.cs +59 -26
  39. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/UpdaterWorker.cs +5 -20
  40. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/HashSetExtensions.cs +14 -0
  41. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/ImmutableArrayExtensions.cs +18 -0
  42. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs +0 -1
  43. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/MSBuildHelper.cs +281 -140
  44. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/NuGetHelper.cs +27 -4
  45. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/PathHelper.cs +18 -13
  46. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTestBase.cs +135 -0
  47. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs +91 -0
  48. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs +71 -0
  49. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.PackagesConfig.cs +67 -0
  50. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.Project.cs +405 -0
  51. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.cs +306 -0
  52. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/ExpectedDiscoveryResults.cs +36 -0
  53. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Files/DotNetToolsJsonBuildFileTests.cs +1 -2
  54. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Files/GlobalJsonBuildFileTests.cs +2 -3
  55. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Files/PackagesConfigBuildFileTests.cs +4 -6
  56. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Files/ProjectBuildFileTests.cs +6 -5
  57. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/NuGetUpdater.Core.Test.csproj +4 -3
  58. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/TemporaryDirectory.cs +38 -6
  59. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/TestBase.cs +10 -0
  60. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/PackagesConfigUpdaterTests.cs +1 -8
  61. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs +13 -41
  62. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DirsProj.cs +0 -5
  63. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs +0 -5
  64. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs +0 -5
  65. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.Mixed.cs +0 -5
  66. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackagesConfig.cs +0 -5
  67. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.Sdk.cs +30 -23
  68. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/AssertEx.cs +272 -0
  69. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/DiffUtil.cs +266 -0
  70. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/MSBuildHelperTests.cs +239 -161
  71. data/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Utilities/SdkPackageUpdaterHelperTests.cs +7 -11
  72. data/lib/dependabot/nuget/discovery/dependency_details.rb +95 -0
  73. data/lib/dependabot/nuget/discovery/dependency_file_discovery.rb +126 -0
  74. data/lib/dependabot/nuget/discovery/directory_packages_props_discovery.rb +43 -0
  75. data/lib/dependabot/nuget/discovery/discovery_json_reader.rb +83 -0
  76. data/lib/dependabot/nuget/discovery/evaluation_details.rb +63 -0
  77. data/lib/dependabot/nuget/discovery/project_discovery.rb +71 -0
  78. data/lib/dependabot/nuget/discovery/property_details.rb +43 -0
  79. data/lib/dependabot/nuget/discovery/workspace_discovery.rb +66 -0
  80. data/lib/dependabot/nuget/file_parser.rb +19 -128
  81. data/lib/dependabot/nuget/file_updater.rb +28 -60
  82. data/lib/dependabot/nuget/native_helpers.rb +55 -0
  83. data/lib/dependabot/nuget/update_checker/compatibility_checker.rb +3 -8
  84. data/lib/dependabot/nuget/update_checker/dependency_finder.rb +1 -0
  85. data/lib/dependabot/nuget/update_checker/property_updater.rb +1 -0
  86. data/lib/dependabot/nuget/update_checker/tfm_finder.rb +17 -152
  87. data/lib/dependabot/nuget/update_checker/version_finder.rb +1 -6
  88. data/lib/dependabot/nuget/update_checker.rb +4 -1
  89. metadata +44 -25
  90. data/lib/dependabot/nuget/file_parser/dotnet_tools_json_parser.rb +0 -71
  91. data/lib/dependabot/nuget/file_parser/global_json_parser.rb +0 -68
  92. data/lib/dependabot/nuget/file_parser/packages_config_parser.rb +0 -92
  93. data/lib/dependabot/nuget/file_parser/project_file_parser.rb +0 -620
  94. data/lib/dependabot/nuget/file_parser/property_value_finder.rb +0 -225
  95. data/lib/dependabot/nuget/file_updater/property_value_updater.rb +0 -81
@@ -1,13 +1,9 @@
1
- using System;
2
- using System.Collections.Generic;
3
1
  using System.Collections.Immutable;
4
2
  using System.Diagnostics.CodeAnalysis;
5
- using System.IO;
6
- using System.Linq;
7
3
  using System.Text;
8
4
  using System.Text.Json;
5
+ using System.Text.Json.Nodes;
9
6
  using System.Text.RegularExpressions;
10
- using System.Threading.Tasks;
11
7
  using System.Xml;
12
8
 
13
9
  using Microsoft.Build.Construction;
@@ -18,111 +14,48 @@ using Microsoft.Build.Locator;
18
14
  using Microsoft.Extensions.FileSystemGlobbing;
19
15
 
20
16
  using NuGet.Configuration;
17
+ using NuGet.Versioning;
21
18
 
22
19
  using NuGetUpdater.Core.Utilities;
23
20
 
24
21
  namespace NuGetUpdater.Core;
25
22
 
26
- using EvaluationResult = (MSBuildHelper.EvaluationResultType ResultType, string EvaluatedValue, string? ErrorMessage);
27
-
28
23
  internal static partial class MSBuildHelper
29
24
  {
30
25
  public static string MSBuildPath { get; private set; } = string.Empty;
31
26
 
32
27
  public static bool IsMSBuildRegistered => MSBuildPath.Length > 0;
33
28
 
34
- static MSBuildHelper()
35
- {
36
- RegisterMSBuild();
37
- }
38
-
39
- public static void RegisterMSBuild()
29
+ public static void RegisterMSBuild(string currentDirectory, string rootDirectory)
40
30
  {
41
31
  // Ensure MSBuild types are registered before calling a method that loads the types
42
32
  if (!IsMSBuildRegistered)
43
33
  {
44
- var globalJsonPath = "global.json";
45
- var tempGlobalJsonPath = globalJsonPath + Guid.NewGuid().ToString();
46
- var globalJsonExists = File.Exists(globalJsonPath);
47
- try
34
+ var candidateDirectories = PathHelper.GetAllDirectoriesToRoot(currentDirectory, rootDirectory);
35
+ var globalJsonPaths = candidateDirectories.Select(d => Path.Combine(d, "global.json")).Where(File.Exists).Select(p => (p, p + Guid.NewGuid().ToString())).ToArray();
36
+ foreach (var (globalJsonPath, tempGlobalJsonPath) in globalJsonPaths)
48
37
  {
49
- if (globalJsonExists)
50
- {
51
- Console.WriteLine("Temporarily removing `global.json` for MSBuild detection.");
52
- File.Move(globalJsonPath, tempGlobalJsonPath);
53
- }
38
+ Console.WriteLine($"Temporarily removing `global.json` from `{Path.GetDirectoryName(globalJsonPath)}` for MSBuild detection.");
39
+ File.Move(globalJsonPath, tempGlobalJsonPath);
40
+ }
54
41
 
42
+ try
43
+ {
55
44
  var defaultInstance = MSBuildLocator.QueryVisualStudioInstances().First();
56
45
  MSBuildPath = defaultInstance.MSBuildPath;
57
46
  MSBuildLocator.RegisterInstance(defaultInstance);
58
47
  }
59
48
  finally
60
49
  {
61
- if (globalJsonExists)
50
+ foreach (var (globalJsonpath, tempGlobalJsonPath) in globalJsonPaths)
62
51
  {
63
- Console.WriteLine("Restoring `global.json` after MSBuild detection.");
64
- File.Move(tempGlobalJsonPath, globalJsonPath);
52
+ Console.WriteLine($"Restoring `global.json` to `{Path.GetDirectoryName(globalJsonpath)}` after MSBuild discovery.");
53
+ File.Move(tempGlobalJsonPath, globalJsonpath);
65
54
  }
66
55
  }
67
56
  }
68
57
  }
69
58
 
70
- public static string[] GetTargetFrameworkMonikers(ImmutableArray<ProjectBuildFile> buildFiles)
71
- {
72
- HashSet<string> targetFrameworkValues = new(StringComparer.OrdinalIgnoreCase);
73
- Dictionary<string, string> propertyInfo = new(StringComparer.OrdinalIgnoreCase);
74
-
75
- foreach (var buildFile in buildFiles)
76
- {
77
- var projectRoot = CreateProjectRootElement(buildFile);
78
-
79
- foreach (var property in projectRoot.Properties)
80
- {
81
- if (property.Name.Equals("TargetFramework", StringComparison.OrdinalIgnoreCase) ||
82
- property.Name.Equals("TargetFrameworks", StringComparison.OrdinalIgnoreCase))
83
- {
84
- foreach (var tfm in property.Value.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
85
- {
86
- targetFrameworkValues.Add(tfm);
87
- }
88
- }
89
- else if (property.Name.Equals("TargetFrameworkVersion", StringComparison.OrdinalIgnoreCase))
90
- {
91
- // For packages.config projects that use TargetFrameworkVersion, we need to convert it to TargetFramework
92
- targetFrameworkValues.Add($"net{property.Value.TrimStart('v').Replace(".", "")}");
93
- }
94
- else
95
- {
96
- propertyInfo[property.Name] = property.Value;
97
- }
98
- }
99
- }
100
-
101
- HashSet<string> targetFrameworks = new(StringComparer.OrdinalIgnoreCase);
102
-
103
- foreach (var targetFrameworkValue in targetFrameworkValues)
104
- {
105
- var (resultType, tfms, errorMessage) =
106
- GetEvaluatedValue(targetFrameworkValue, propertyInfo, propertiesToIgnore: ["TargetFramework", "TargetFrameworks"]);
107
- if (resultType != EvaluationResultType.Success)
108
- {
109
- continue;
110
- }
111
-
112
- if (string.IsNullOrEmpty(tfms))
113
- {
114
- continue;
115
- }
116
-
117
- foreach (var tfm in tfms.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
118
- {
119
- targetFrameworks.Add(tfm);
120
- }
121
- }
122
-
123
- return targetFrameworks.ToArray();
124
- }
125
-
126
59
  public static IEnumerable<string> GetProjectPathsFromSolution(string solutionPath)
127
60
  {
128
61
  var solution = SolutionFile.Parse(solutionPath);
@@ -190,19 +123,65 @@ internal static partial class MSBuildHelper
190
123
  }
191
124
  }
192
125
 
126
+ public static IReadOnlyDictionary<string, Property> GetProperties(ImmutableArray<ProjectBuildFile> buildFiles)
127
+ {
128
+ Dictionary<string, Property> properties = new(StringComparer.OrdinalIgnoreCase);
129
+
130
+ foreach (var buildFile in buildFiles)
131
+ {
132
+ var projectRoot = CreateProjectRootElement(buildFile);
133
+
134
+ foreach (var property in projectRoot.Properties)
135
+ {
136
+ // Short of evaluating the entire project, there's no way to _really_ know what package version is
137
+ // going to be used, and even then we might not be able to update it. As a best guess, we'll simply
138
+ // skip any property that has a condition _or_ where the condition is checking for an empty string.
139
+ var hasEmptyCondition = string.IsNullOrEmpty(property.Condition);
140
+ var conditionIsCheckingForEmptyString = string.Equals(property.Condition, $"$({property.Name}) == ''", StringComparison.OrdinalIgnoreCase) ||
141
+ string.Equals(property.Condition, $"'$({property.Name})' == ''", StringComparison.OrdinalIgnoreCase);
142
+ if (hasEmptyCondition || conditionIsCheckingForEmptyString)
143
+ {
144
+ properties[property.Name] = new(property.Name, property.Value, buildFile.RelativePath);
145
+ }
146
+ }
147
+ }
148
+
149
+ return properties;
150
+ }
151
+
193
152
  public static IEnumerable<Dependency> GetTopLevelPackageDependencyInfos(ImmutableArray<ProjectBuildFile> buildFiles)
194
153
  {
195
- Dictionary<string, (string, bool)> packageInfo = new(StringComparer.OrdinalIgnoreCase);
154
+ Dictionary<string, (string, bool, DependencyType)> packageInfo = new(StringComparer.OrdinalIgnoreCase);
196
155
  Dictionary<string, string> packageVersionInfo = new(StringComparer.OrdinalIgnoreCase);
197
- Dictionary<string, string> propertyInfo = new(StringComparer.OrdinalIgnoreCase);
156
+ Dictionary<string, Property> propertyInfo = new(StringComparer.OrdinalIgnoreCase);
198
157
 
199
158
  foreach (var buildFile in buildFiles)
200
159
  {
201
160
  var projectRoot = CreateProjectRootElement(buildFile);
202
161
 
162
+ foreach (var property in projectRoot.Properties)
163
+ {
164
+ // Short of evaluating the entire project, there's no way to _really_ know what package version is
165
+ // going to be used, and even then we might not be able to update it. As a best guess, we'll simply
166
+ // skip any property that has a condition _or_ where the condition is checking for an empty string.
167
+ var hasEmptyCondition = string.IsNullOrEmpty(property.Condition);
168
+ var conditionIsCheckingForEmptyString = string.Equals(property.Condition, $"$({property.Name}) == ''", StringComparison.OrdinalIgnoreCase) ||
169
+ string.Equals(property.Condition, $"'$({property.Name})' == ''", StringComparison.OrdinalIgnoreCase);
170
+ if (hasEmptyCondition || conditionIsCheckingForEmptyString)
171
+ {
172
+ propertyInfo[property.Name] = new(property.Name, property.Value, buildFile.RelativePath);
173
+ }
174
+ }
175
+
176
+ if (buildFile.IsOutsideBasePath)
177
+ {
178
+ continue;
179
+ }
180
+
203
181
  foreach (var packageItem in projectRoot.Items
204
182
  .Where(i => (i.ItemType == "PackageReference" || i.ItemType == "GlobalPackageReference")))
205
183
  {
184
+ var dependencyType = packageItem.ItemType == "PackageReference" ? DependencyType.PackageReference : DependencyType.GlobalPackageReference;
206
185
  var versionSpecification = packageItem.Metadata.FirstOrDefault(m => m.Name.Equals("Version", StringComparison.OrdinalIgnoreCase))?.Value
207
186
  ?? packageItem.Metadata.FirstOrDefault(m => m.Name.Equals("VersionOverride", StringComparison.OrdinalIgnoreCase))?.Value
208
187
  ?? string.Empty;
@@ -219,12 +198,12 @@ internal static partial class MSBuildHelper
219
198
  var vSpec = string.IsNullOrEmpty(versionSpecification) || existingUpdate ? existingVersion : versionSpecification;
220
199
 
221
200
  var isUpdate = existingUpdate && string.IsNullOrEmpty(packageItem.Include);
222
- packageInfo[attributeValue] = (vSpec, isUpdate);
201
+ packageInfo[attributeValue] = (vSpec, isUpdate, dependencyType);
223
202
  }
224
203
  else
225
204
  {
226
205
  var isUpdate = !string.IsNullOrEmpty(packageItem.Update);
227
- packageInfo[attributeValue] = (versionSpecification, isUpdate);
206
+ packageInfo[attributeValue] = (versionSpecification, isUpdate, dependencyType);
228
207
  }
229
208
  }
230
209
  }
@@ -236,25 +215,11 @@ internal static partial class MSBuildHelper
236
215
  packageVersionInfo[packageItem.Include] = packageItem.Metadata.FirstOrDefault(m => m.Name.Equals("Version", StringComparison.OrdinalIgnoreCase))?.Value
237
216
  ?? string.Empty;
238
217
  }
239
-
240
- foreach (var property in projectRoot.Properties)
241
- {
242
- // Short of evaluating the entire project, there's no way to _really_ know what package version is
243
- // going to be used, and even then we might not be able to update it. As a best guess, we'll simply
244
- // skip any property that has a condition _or_ where the condition is checking for an empty string.
245
- var hasEmptyCondition = string.IsNullOrEmpty(property.Condition);
246
- var conditionIsCheckingForEmptyString = string.Equals(property.Condition, $"$({property.Name}) == ''", StringComparison.OrdinalIgnoreCase) ||
247
- string.Equals(property.Condition, $"'$({property.Name})' == ''", StringComparison.OrdinalIgnoreCase);
248
- if (hasEmptyCondition || conditionIsCheckingForEmptyString)
249
- {
250
- propertyInfo[property.Name] = property.Value;
251
- }
252
- }
253
218
  }
254
219
 
255
220
  foreach (var (name, info) in packageInfo)
256
221
  {
257
- var (version, isUpdate) = info;
222
+ var (version, isUpdate, dependencyType) = info;
258
223
  if (version.Length != 0 || !packageVersionInfo.TryGetValue(name, out var packageVersion))
259
224
  {
260
225
  packageVersion = version;
@@ -262,51 +227,51 @@ internal static partial class MSBuildHelper
262
227
 
263
228
  // Walk the property replacements until we don't find another one.
264
229
  var evaluationResult = GetEvaluatedValue(packageVersion, propertyInfo);
265
- if (evaluationResult.ResultType != EvaluationResultType.Success)
266
- {
267
- // if we can't resolve the package version, don't report the dependency
268
- continue;
269
- }
270
-
271
- packageVersion = evaluationResult.EvaluatedValue.TrimStart('[', '(').TrimEnd(']', ')');
230
+ packageVersion = evaluationResult.ResultType == EvaluationResultType.Success
231
+ ? evaluationResult.EvaluatedValue.TrimStart('[', '(').TrimEnd(']', ')')
232
+ : evaluationResult.EvaluatedValue;
272
233
 
273
234
  // We don't know the version for range requirements or wildcard
274
235
  // requirements, so return "" for these.
275
236
  yield return packageVersion.Contains(',') || packageVersion.Contains('*')
276
- ? new Dependency(name, string.Empty, DependencyType.Unknown, IsUpdate: isUpdate)
277
- : new Dependency(name, packageVersion, DependencyType.Unknown, IsUpdate: isUpdate);
237
+ ? new Dependency(name, string.Empty, dependencyType, EvaluationResult: evaluationResult, IsUpdate: isUpdate)
238
+ : new Dependency(name, packageVersion, dependencyType, EvaluationResult: evaluationResult, IsUpdate: isUpdate);
278
239
  }
279
240
  }
280
241
 
281
242
  /// <summary>
282
243
  /// Given an MSBuild string and a set of properties, returns our best guess at the final value MSBuild will evaluate to.
283
244
  /// </summary>
284
- public static EvaluationResult GetEvaluatedValue(string msbuildString, Dictionary<string, string> propertyInfo, params string[] propertiesToIgnore)
245
+ public static EvaluationResult GetEvaluatedValue(string msbuildString, IReadOnlyDictionary<string, Property> propertyInfo, params string[] propertiesToIgnore)
285
246
  {
286
247
  var ignoredProperties = new HashSet<string>(propertiesToIgnore, StringComparer.OrdinalIgnoreCase);
287
248
  var seenProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
288
249
 
250
+ string originalValue = msbuildString;
251
+ string? rootPropertyName = null;
289
252
  while (TryGetPropertyName(msbuildString, out var propertyName))
290
253
  {
254
+ rootPropertyName = propertyName;
255
+
291
256
  if (ignoredProperties.Contains(propertyName))
292
257
  {
293
- return (EvaluationResultType.PropertyIgnored, msbuildString, $"Property '{propertyName}' is ignored.");
258
+ return new(EvaluationResultType.PropertyIgnored, originalValue, msbuildString, rootPropertyName, $"Property '{propertyName}' is ignored.");
294
259
  }
295
260
 
296
261
  if (!seenProperties.Add(propertyName))
297
262
  {
298
- return (EvaluationResultType.CircularReference, msbuildString, $"Property '{propertyName}' has a circular reference.");
263
+ return new(EvaluationResultType.CircularReference, originalValue, msbuildString, rootPropertyName, $"Property '{propertyName}' has a circular reference.");
299
264
  }
300
265
 
301
- if (!propertyInfo.TryGetValue(propertyName, out var propertyValue))
266
+ if (!propertyInfo.TryGetValue(propertyName, out var property))
302
267
  {
303
- return (EvaluationResultType.PropertyNotFound, msbuildString, $"Property '{propertyName}' was not found.");
268
+ return new(EvaluationResultType.PropertyNotFound, originalValue, msbuildString, rootPropertyName, $"Property '{propertyName}' was not found.");
304
269
  }
305
270
 
306
- msbuildString = msbuildString.Replace($"$({propertyName})", propertyValue);
271
+ msbuildString = msbuildString.Replace($"$({propertyName})", property.Value);
307
272
  }
308
273
 
309
- return (EvaluationResultType.Success, msbuildString, null);
274
+ return new(EvaluationResultType.Success, originalValue, msbuildString, rootPropertyName, null);
310
275
  }
311
276
 
312
277
  public static bool TryGetPropertyName(string versionContent, [NotNullWhen(true)] out string? propertyName)
@@ -344,6 +309,124 @@ internal static partial class MSBuildHelper
344
309
  }
345
310
  }
346
311
 
312
+ internal static async Task<Dependency[]?> ResolveDependencyConflicts(string repoRoot, string projectPath, string targetFramework, Dependency[] packages, Logger logger)
313
+ {
314
+ var tempDirectory = Directory.CreateTempSubdirectory("package-dependency-coherence_");
315
+ try
316
+ {
317
+ var tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);
318
+ var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"restore \"{tempProjectPath}\"", workingDirectory: tempDirectory.FullName);
319
+
320
+ // simple cases first
321
+ // if restore failed, nothing we can do
322
+ if (exitCode != 0)
323
+ {
324
+ return null;
325
+ }
326
+
327
+ // if no problems found, just return the current set
328
+ if (!stdOut.Contains("NU1608"))
329
+ {
330
+ return packages;
331
+ }
332
+
333
+ // now it gets complicated; look for the packages with issues
334
+ MatchCollection matches = PackageIncompatibilityWarningPattern().Matches(stdOut);
335
+ (string, NuGetVersion)[] badPackagesAndVersions = matches.Select(m => (m.Groups["PackageName"].Value, NuGetVersion.Parse(m.Groups["PackageVersion"].Value))).ToArray();
336
+ Dictionary<string, HashSet<NuGetVersion>> badPackagesAndCandidateVersionsDictionary = new(StringComparer.OrdinalIgnoreCase);
337
+
338
+ // and for each of those packages, find all versions greater than the one that's currently installed
339
+ foreach ((string packageName, NuGetVersion packageVersion) in badPackagesAndVersions)
340
+ {
341
+ // this command dumps a JSON object with all versions of the specified package from all package sources
342
+ (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"package search {packageName} --exact-match --format json", workingDirectory: tempDirectory.FullName);
343
+ if (exitCode != 0)
344
+ {
345
+ continue;
346
+ }
347
+
348
+ // ensure collection exists
349
+ if (!badPackagesAndCandidateVersionsDictionary.ContainsKey(packageName))
350
+ {
351
+ badPackagesAndCandidateVersionsDictionary.Add(packageName, new HashSet<NuGetVersion>());
352
+ }
353
+
354
+ HashSet<NuGetVersion> foundVersions = badPackagesAndCandidateVersionsDictionary[packageName];
355
+
356
+ var json = JsonHelper.ParseNode(stdOut);
357
+ if (json?["searchResult"] is JsonArray searchResults)
358
+ {
359
+ foreach (var searchResult in searchResults)
360
+ {
361
+ if (searchResult?["packages"] is JsonArray packagesArray)
362
+ {
363
+ foreach (var package in packagesArray)
364
+ {
365
+ // in 8.0.xxx SDKs, the package version is in the `latestVersion` property, but in 9.0.xxx, it's `version`
366
+ var packageVersionProperty = package?["version"] ?? package?["latestVersion"];
367
+ if (packageVersionProperty is JsonValue latestVersion &&
368
+ latestVersion.GetValueKind() == JsonValueKind.String &&
369
+ NuGetVersion.TryParse(latestVersion.ToString(), out var nugetVersion) &&
370
+ nugetVersion > packageVersion)
371
+ {
372
+ foundVersions.Add(nugetVersion);
373
+ }
374
+ }
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ // generate all possible combinations
381
+ (string Key, NuGetVersion v)[][] expandedLists = badPackagesAndCandidateVersionsDictionary.Select(kvp => kvp.Value.Order().Select(v => (kvp.Key, v)).ToArray()).ToArray();
382
+ IEnumerable<(string PackageName, NuGetVersion PackageVersion)>[] product = expandedLists.CartesianProduct().ToArray();
383
+
384
+ // FUTURE WORK: pre-filter individual known package incompatibilities to reduce the number of combinations, e.g., if Package.A v1.0.0
385
+ // is incompatible with Package.B v2.0.0, then remove _all_ combinations with that pair
386
+
387
+ // this is the slow part
388
+ foreach (IEnumerable<(string PackageName, NuGetVersion PackageVersion)> candidateSet in product)
389
+ {
390
+ // rebuild candidate dependency list with the relevant versions
391
+ Dictionary<string, NuGetVersion> packageVersions = candidateSet.ToDictionary(candidateSet => candidateSet.PackageName, candidateSet => candidateSet.PackageVersion);
392
+ Dependency[] candidatePackages = packages.Select(p =>
393
+ {
394
+ if (packageVersions.TryGetValue(p.Name, out var version))
395
+ {
396
+ // create a new dependency with the updated version
397
+ return new Dependency(p.Name, version.ToString(), p.Type, IsDevDependency: p.IsDevDependency, IsOverride: p.IsOverride, IsUpdate: p.IsUpdate);
398
+ }
399
+
400
+ // not the dependency we're looking for, use whatever it already was in this set
401
+ return p;
402
+ }).ToArray();
403
+
404
+ if (await DependenciesAreCoherentAsync(repoRoot, projectPath, targetFramework, candidatePackages, logger))
405
+ {
406
+ // return as soon as we find a coherent set
407
+ return candidatePackages;
408
+ }
409
+ }
410
+
411
+ // no package resolution set found
412
+ return null;
413
+ }
414
+ finally
415
+ {
416
+ tempDirectory.Delete(recursive: true);
417
+ }
418
+ }
419
+
420
+ // fully expand all possible combinations using the algorithm from here:
421
+ // https://ericlippert.com/2010/06/28/computing-a-cartesian-product-with-linq/
422
+ private static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
423
+ {
424
+ IEnumerable<IEnumerable<T>> emptyProduct = [[]];
425
+ return sequences.Aggregate(emptyProduct, (accumulator, sequence) => from accseq in accumulator
426
+ from item in sequence
427
+ select accseq.Concat([item]));
428
+ }
429
+
347
430
  private static ProjectRootElement CreateProjectRootElement(ProjectBuildFile buildFile)
348
431
  {
349
432
  var xmlString = buildFile.Contents.ToFullString();
@@ -373,12 +456,13 @@ internal static partial class MSBuildHelper
373
456
  }
374
457
  }
375
458
 
376
- private static async Task<string> CreateTempProjectAsync(
459
+ internal static async Task<string> CreateTempProjectAsync(
377
460
  DirectoryInfo tempDir,
378
461
  string repoRoot,
379
462
  string projectPath,
380
463
  string targetFramework,
381
- IReadOnlyCollection<Dependency> packages)
464
+ IReadOnlyCollection<Dependency> packages,
465
+ bool usePackageDownload = false)
382
466
  {
383
467
  var projectDirectory = Path.GetDirectoryName(projectPath);
384
468
  projectDirectory ??= repoRoot;
@@ -410,9 +494,9 @@ internal static partial class MSBuildHelper
410
494
  Environment.NewLine,
411
495
  packages
412
496
  // empty `Version` attributes will cause the temporary project to not build
413
- .Where(p => !string.IsNullOrWhiteSpace(p.Version))
497
+ .Where(p => (p.EvaluationResult is null || p.EvaluationResult.ResultType == EvaluationResultType.Success) && !string.IsNullOrWhiteSpace(p.Version))
414
498
  // If all PackageReferences for a package are update-only mark it as such, otherwise it can cause package incoherence errors which do not exist in the repo.
415
- .Select(static p => $"<PackageReference {(p.IsUpdate ? "Update" : "Include")}=\"{p.Name}\" Version=\"[{p.Version}]\" />"));
499
+ .Select(p => $"<{(usePackageDownload ? "PackageDownload" : "PackageReference")} {(p.IsUpdate ? "Update" : "Include")}=\"{p.Name}\" Version=\"[{p.Version}]\" />"));
416
500
 
417
501
  var projectContents = $"""
418
502
  <Project Sdk="Microsoft.NET.Sdk">
@@ -465,23 +549,34 @@ internal static partial class MSBuildHelper
465
549
  }
466
550
 
467
551
  internal static async Task<Dependency[]> GetAllPackageDependenciesAsync(
468
- string repoRoot, string projectPath, string targetFramework, IReadOnlyCollection<Dependency> packages, Logger? logger = null)
552
+ string repoRoot,
553
+ string projectPath,
554
+ string targetFramework,
555
+ IReadOnlyCollection<Dependency> packages,
556
+ Logger? logger = null)
469
557
  {
470
558
  var tempDirectory = Directory.CreateTempSubdirectory("package-dependency-resolution_");
471
559
  try
472
560
  {
561
+ var topLevelPackagesNames = packages.Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
473
562
  var tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);
474
563
 
475
564
  var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", $"build \"{tempProjectPath}\" /t:_ReportDependencies", workingDirectory: tempDirectory.FullName);
476
565
 
477
566
  if (exitCode == 0)
478
567
  {
568
+ ImmutableArray<string> tfms = [targetFramework];
479
569
  var lines = stdout.Split('\n').Select(line => line.Trim());
480
570
  var pattern = PackagePattern();
481
571
  var allDependencies = lines
482
572
  .Select(line => pattern.Match(line))
483
573
  .Where(match => match.Success)
484
- .Select(match => new Dependency(match.Groups["PackageName"].Value, match.Groups["PackageVersion"].Value, DependencyType.Unknown))
574
+ .Select(match =>
575
+ {
576
+ var packageName = match.Groups["PackageName"].Value;
577
+ var isTransitive = !topLevelPackagesNames.Contains(packageName);
578
+ return new Dependency(packageName, match.Groups["PackageVersion"].Value, DependencyType.Unknown, TargetFrameworks: tfms, IsTransitive: isTransitive);
579
+ })
485
580
  .ToArray();
486
581
 
487
582
  return allDependencies;
@@ -504,12 +599,25 @@ internal static partial class MSBuildHelper
504
599
  }
505
600
  }
506
601
 
507
- internal static string? GetGlobalJsonPath(string repoRootPath, string projectPath)
602
+ internal static bool TryGetGlobalJsonPath(string repoRootPath, string workspacePath, [NotNullWhen(returnValue: true)] out string? globalJsonPath)
603
+ {
604
+ globalJsonPath = PathHelper.GetFileInDirectoryOrParent(workspacePath, repoRootPath, "global.json", caseSensitive: false);
605
+ return globalJsonPath is not null;
606
+ }
607
+
608
+ internal static bool TryGetDotNetToolsJsonPath(string repoRootPath, string workspacePath, [NotNullWhen(returnValue: true)] out string? dotnetToolsJsonJsonPath)
508
609
  {
509
- return PathHelper.GetFileInDirectoryOrParent(Path.GetDirectoryName(projectPath)!, repoRootPath, "global.json");
610
+ dotnetToolsJsonJsonPath = PathHelper.GetFileInDirectoryOrParent(workspacePath, repoRootPath, "./.config/dotnet-tools.json", caseSensitive: false);
611
+ return dotnetToolsJsonJsonPath is not null;
510
612
  }
511
613
 
512
- internal static async Task<ImmutableArray<ProjectBuildFile>> LoadBuildFiles(string repoRootPath, string projectPath)
614
+ internal static bool TryGetDirectoryPackagesPropsPath(string repoRootPath, string workspacePath, [NotNullWhen(returnValue: true)] out string? directoryPackagesPropsPath)
615
+ {
616
+ directoryPackagesPropsPath = PathHelper.GetFileInDirectoryOrParent(workspacePath, repoRootPath, "./Directory.Packages.props", caseSensitive: false);
617
+ return directoryPackagesPropsPath is not null;
618
+ }
619
+
620
+ internal static async Task<(ImmutableArray<ProjectBuildFile> ProjectBuildFiles, string[] TargetFrameworks)> LoadBuildFilesAndTargetFrameworksAsync(string repoRootPath, string projectPath)
513
621
  {
514
622
  var buildFileList = new List<string>
515
623
  {
@@ -517,8 +625,9 @@ internal static partial class MSBuildHelper
517
625
  };
518
626
 
519
627
  // a global.json file might cause problems with the dotnet msbuild command; create a safe version temporarily
520
- var globalJsonPath = GetGlobalJsonPath(repoRootPath, projectPath);
628
+ TryGetGlobalJsonPath(repoRootPath, projectPath, out var globalJsonPath);
521
629
  var safeGlobalJsonName = $"{globalJsonPath}{Guid.NewGuid()}";
630
+ HashSet<string> targetFrameworks = new(StringComparer.OrdinalIgnoreCase);
522
631
 
523
632
  try
524
633
  {
@@ -547,16 +656,51 @@ internal static partial class MSBuildHelper
547
656
  // load the project even if it imports a file that doesn't exist (e.g. a file that's generated at restore
548
657
  // or build time).
549
658
  using var projectCollection = new ProjectCollection(); // do this in a one-off instance and don't pollute the global collection
550
- var project = Project.FromFile(projectPath, new ProjectOptions
659
+ Project project = Project.FromFile(projectPath, new ProjectOptions
551
660
  {
552
661
  LoadSettings = ProjectLoadSettings.IgnoreMissingImports,
553
662
  ProjectCollection = projectCollection,
554
663
  });
555
664
  buildFileList.AddRange(project.Imports.Select(i => i.ImportedProject.FullPath.NormalizePathToUnix()));
665
+
666
+ // use the MSBuild-evaluated value so we don't have to try to manually parse XML
667
+ IEnumerable<ProjectProperty> targetFrameworkProperties = project.Properties.Where(p => p.Name.Equals("TargetFramework", StringComparison.OrdinalIgnoreCase)).ToList();
668
+ IEnumerable<ProjectProperty> targetFrameworksProperties = project.Properties.Where(p => p.Name.Equals("TargetFrameworks", StringComparison.OrdinalIgnoreCase)).ToList();
669
+ IEnumerable<ProjectProperty> targetFrameworkVersionProperties = project.Properties.Where(p => p.Name.Equals("TargetFrameworkVersion", StringComparison.OrdinalIgnoreCase)).ToList();
670
+ foreach (ProjectProperty tfm in targetFrameworkProperties)
671
+ {
672
+ if (!string.IsNullOrWhiteSpace(tfm.EvaluatedValue))
673
+ {
674
+ targetFrameworks.Add(tfm.EvaluatedValue);
675
+ }
676
+ }
677
+
678
+ foreach (ProjectProperty tfms in targetFrameworksProperties)
679
+ {
680
+ foreach (string tfmValue in tfms.EvaluatedValue.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
681
+ {
682
+ targetFrameworks.Add(tfmValue);
683
+ }
684
+ }
685
+
686
+ if (targetFrameworks.Count == 0)
687
+ {
688
+ // Only try this if we haven't been able to resolve anything yet. This is because deep in the SDK, a
689
+ // `TargetFramework` of `netstandard2.0` (eventually) gets turned into `v2.0` and we don't want to
690
+ // interpret that as a .NET Framework 2.0 project.
691
+ foreach (ProjectProperty tfvm in targetFrameworkVersionProperties)
692
+ {
693
+ // `v0.0` is an error case where no TFM could be evaluated
694
+ if (tfvm.EvaluatedValue != "v0.0")
695
+ {
696
+ targetFrameworks.Add($"net{tfvm.EvaluatedValue.TrimStart('v').Replace(".", "")}");
697
+ }
698
+ }
699
+ }
556
700
  }
557
701
  catch (InvalidProjectFileException)
558
702
  {
559
- return [];
703
+ return ([], []);
560
704
  }
561
705
  finally
562
706
  {
@@ -567,25 +711,22 @@ internal static partial class MSBuildHelper
567
711
  }
568
712
 
569
713
  var repoRootPathPrefix = repoRootPath.NormalizePathToUnix() + "/";
570
- var buildFilesInRepo = buildFileList
714
+ var buildFiles = buildFileList
571
715
  .Where(f => f.StartsWith(repoRootPathPrefix, StringComparison.OrdinalIgnoreCase))
716
+ .Distinct();
717
+ var result = buildFiles
572
718
  .Where(File.Exists)
573
- .Distinct()
574
- .ToArray();
575
- var result = buildFilesInRepo
576
719
  .Select(path => ProjectBuildFile.Open(repoRootPath, path))
577
720
  .ToImmutableArray();
578
- return result;
721
+ return (result, targetFrameworks.ToArray());
579
722
  }
580
723
 
581
724
  [GeneratedRegex("^\\s*NuGetData::Package=(?<PackageName>[^,]+), Version=(?<PackageVersion>.+)$")]
582
725
  private static partial Regex PackagePattern();
583
726
 
584
- internal enum EvaluationResultType
585
- {
586
- Success,
587
- PropertyIgnored,
588
- CircularReference,
589
- PropertyNotFound,
590
- }
727
+ // Example output:
728
+ // NU1608: Detected package version outside of dependency constraint: SpecFlow.Tools.MsBuild.Generation 3.3.30 requires SpecFlow(= 3.3.30) but version SpecFlow 3.9.74 was resolved.
729
+ // PackageName-|+++++++++++++++++++++++++++++++| |++++|-PackageVersion
730
+ [GeneratedRegex("NU1608: [^:]+: (?<PackageName>[^ ]+) (?<PackageVersion>[^ ]+)")]
731
+ private static partial Regex PackageIncompatibilityWarningPattern();
591
732
  }