This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MSBuildSdkResolver.cs
332 lines (277 loc) · 13.1 KB
/
MSBuildSdkResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Microsoft.DotNet.MSBuildSdkResolver
{
// Thread-safety note:
// 1. MSBuild can call the same resolver instance in parallel on multiple threads.
// 2. Nevertheless, in the IDE, project re-evaluation can create new instances for each evaluation.
//
// As such, all state (instance or static) must be guarded against concurrent access/updates.
// Caches of minimum versions, compatible SDKs are static to benefit multiple IDE evaluations.
// VSSettings are also effectively static (singleton instance that can be swapped by tests).
public sealed class DotNetMSBuildSdkResolver : SdkResolver
{
public override string Name => "Microsoft.DotNet.MSBuildSdkResolver";
// Default resolver has priority 10000 and we want to go before it and leave room on either side of us.
public override int Priority => 5000;
private readonly Func<string, string> _getEnvironmentVariable;
private readonly VSSettings _vsSettings;
private static readonly ConcurrentDictionary<string, Version> s_minimumMSBuildVersions
= new ConcurrentDictionary<string, Version>();
private static readonly ConcurrentDictionary<CompatibleSdkKey, CompatibleSdkValue> s_compatibleSdks
= new ConcurrentDictionary<CompatibleSdkKey, CompatibleSdkValue>();
public DotNetMSBuildSdkResolver()
: this(Environment.GetEnvironmentVariable, VSSettings.Ambient)
{
}
// Test constructor
internal DotNetMSBuildSdkResolver(Func<string, string> getEnvironmentVariable, VSSettings vsSettings)
{
_getEnvironmentVariable = getEnvironmentVariable;
_vsSettings = vsSettings;
}
private sealed class CachedResult
{
public string MSBuildSdksDir;
public string NETCoreSdkVersion;
}
public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext context, SdkResultFactory factory)
{
string msbuildSdksDir = null;
string netcoreSdkVersion = null;
if (context.State is CachedResult priorResult)
{
msbuildSdksDir = priorResult.MSBuildSdksDir;
netcoreSdkVersion = priorResult.NETCoreSdkVersion;
}
if (msbuildSdksDir == null)
{
// These are overrides that are used to force the resolved SDK tasks and targets to come from a given
// base directory and report a given version to msbuild (which may be null if unknown. One key use case
// for this is to test SDK tasks and targets without deploying them inside the .NET Core SDK.
msbuildSdksDir = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR");
netcoreSdkVersion = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_VER");
}
if (msbuildSdksDir == null)
{
string dotnetExeDir = GetDotnetExeDirectory();
var resolverResult = ResolveNETCoreSdkDirectory(context, dotnetExeDir);
string netcoreSdkDir = resolverResult.ResolvedSdkDirectory;
string globalJsonPath = resolverResult.GlobalJsonPath;
if (netcoreSdkDir == null)
{
return Failure(
factory,
Strings.UnableToLocateNETCoreSdk);
}
msbuildSdksDir = Path.Combine(netcoreSdkDir, "Sdks");
netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;
if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
{
return Failure(
factory,
Strings.NETCoreSDKSmallerThanMinimumRequestedVersion,
netcoreSdkVersion,
sdkReference.MinimumVersion);
}
Version minimumMSBuildVersion = GetMinimumMSBuildVersion(netcoreSdkDir);
if (context.MSBuildVersion < minimumMSBuildVersion)
{
return Failure(
factory,
Strings.MSBuildSmallerThanMinimumVersion,
netcoreSdkVersion,
minimumMSBuildVersion,
context.MSBuildVersion);
}
string minimumVSDefinedSDKVersion = GetMinimumVSDefinedSDKVersion();
if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, minimumVSDefinedSDKVersion))
{
return Failure(
factory,
Strings.NETCoreSDKSmallerThanMinimumVersionRequiredByVisualStudio,
netcoreSdkVersion,
minimumVSDefinedSDKVersion);
}
}
context.State = new CachedResult
{
MSBuildSdksDir = msbuildSdksDir,
NETCoreSdkVersion = netcoreSdkVersion
};
string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");
if (!Directory.Exists(msbuildSdkDir))
{
return Failure(
factory,
Strings.MSBuildSDKDirectoryNotFound,
msbuildSdkDir);
}
return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion);
}
private static SdkResult Failure(SdkResultFactory factory, string format, params object[] args)
{
return factory.IndicateFailure(new[] { string.Format(format, args) });
}
private sealed class CompatibleSdkKey : IEquatable<CompatibleSdkKey>
{
public readonly string DotnetExeDirectory;
public readonly Version MSBuildVersion;
public CompatibleSdkKey(string dotnetExeDirectory, Version msbuildVersion)
{
DotnetExeDirectory = dotnetExeDirectory;
MSBuildVersion = msbuildVersion;
}
public bool Equals(CompatibleSdkKey other)
{
return other != null
&& DotnetExeDirectory == other.DotnetExeDirectory
&& MSBuildVersion == other.MSBuildVersion;
}
public override bool Equals(object obj)
{
return Equals(obj as CompatibleSdkValue);
}
public override int GetHashCode()
{
int h1 = DotnetExeDirectory.GetHashCode();
int h2 = MSBuildVersion.GetHashCode();
return unchecked(((h1 << 5) + h1) ^ h2);
}
}
private sealed class CompatibleSdkValue
{
public readonly string MostRecentCompatible;
public readonly string MostRecentCompatibleNonPreview;
public CompatibleSdkValue(string mostRecentCompatible, string mostRecentCompatibleNonPreview)
{
MostRecentCompatible = mostRecentCompatible;
MostRecentCompatibleNonPreview = mostRecentCompatibleNonPreview;
}
}
private string GetMostCompatibleSdk(string dotnetExeDirectory, Version msbuildVersion)
{
CompatibleSdkValue sdks = GetMostCompatibleSdks(dotnetExeDirectory, msbuildVersion);
if (_vsSettings.DisallowPrerelease())
{
return sdks.MostRecentCompatibleNonPreview;
}
return sdks.MostRecentCompatible;
}
private CompatibleSdkValue GetMostCompatibleSdks(string dotnetExeDirectory, Version msbuildVersion)
{
return s_compatibleSdks.GetOrAdd(
new CompatibleSdkKey(dotnetExeDirectory, msbuildVersion),
key =>
{
string mostRecent = null;
string mostRecentNonPreview = null;
string[] availableSdks = NETCoreSdkResolver.GetAvailableSdks(key.DotnetExeDirectory);
for (int i = availableSdks.Length - 1; i >= 0; i--)
{
string netcoreSdkDir = availableSdks[i];
string netcoreSdkVersion = new DirectoryInfo(netcoreSdkDir).Name;
Version minimumMSBuildVersion = GetMinimumMSBuildVersion(netcoreSdkDir);
if (key.MSBuildVersion < minimumMSBuildVersion)
{
continue;
}
if (mostRecent == null)
{
mostRecent = netcoreSdkDir;
}
if (netcoreSdkVersion.IndexOf('-') < 0)
{
mostRecentNonPreview = netcoreSdkDir;
break;
}
}
return new CompatibleSdkValue(mostRecent, mostRecentNonPreview);
});
}
private Version GetMinimumMSBuildVersion(string netcoreSdkDir)
{
return s_minimumMSBuildVersions.GetOrAdd(
netcoreSdkDir,
dir =>
{
string minimumVersionFilePath = Path.Combine(netcoreSdkDir, "minimumMSBuildVersion");
if (!File.Exists(minimumVersionFilePath))
{
// smallest version that had resolver support and also
// greater than or equal to the version required by any
// .NET Core SDK that did not have this file.
return new Version(15, 3, 0);
}
return Version.Parse(File.ReadLines(minimumVersionFilePath).First().Trim());
});
}
private static string GetMinimumVSDefinedSDKVersion()
{
string dotnetMSBuildSdkResolverDirectory =
Path.GetDirectoryName(typeof(DotNetMSBuildSdkResolver).GetTypeInfo().Assembly.Location);
string minimumVSDefinedSdkVersionFilePath =
Path.Combine(dotnetMSBuildSdkResolverDirectory, "minimumVSDefinedSDKVersion");
if (!File.Exists(minimumVSDefinedSdkVersionFilePath))
{
// smallest version that is required by VS 15.3.
return "1.0.4";
}
return File.ReadLines(minimumVSDefinedSdkVersionFilePath).First().Trim();
}
private bool IsNetCoreSDKSmallerThanTheMinimumVersion(string netcoreSdkVersion, string minimumVersion)
{
FXVersion netCoreSdkFXVersion;
FXVersion minimumFXVersion;
if (string.IsNullOrEmpty(minimumVersion))
{
return false;
}
if (!FXVersion.TryParse(netcoreSdkVersion, out netCoreSdkFXVersion) ||
!FXVersion.TryParse(minimumVersion, out minimumFXVersion))
{
return true;
}
return FXVersion.Compare(netCoreSdkFXVersion, minimumFXVersion) < 0;
}
private NETCoreSdkResolver.Result ResolveNETCoreSdkDirectory(SdkResolverContext context, string dotnetExeDir)
{
string globalJsonStartDir = Path.GetDirectoryName(context.SolutionFilePath ?? context.ProjectFilePath);
var result = NETCoreSdkResolver.ResolveSdk(dotnetExeDir, globalJsonStartDir, _vsSettings.DisallowPrerelease());
if (result.ResolvedSdkDirectory != null
&& result.GlobalJsonPath == null
&& context.MSBuildVersion < GetMinimumMSBuildVersion(result.ResolvedSdkDirectory))
{
string mostCompatible = GetMostCompatibleSdk(dotnetExeDir, context.MSBuildVersion);
if (mostCompatible != null)
{
result.ResolvedSdkDirectory = mostCompatible;
}
}
return result;
}
private string GetDotnetExeDirectory()
{
string environmentOverride = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR");
if (environmentOverride != null)
{
return environmentOverride;
}
var environmentProvider = new EnvironmentProvider(_getEnvironmentVariable);
var dotnetExe = environmentProvider.GetCommandPath("dotnet");
if (dotnetExe != null && !Interop.RunningOnWindows)
{
// e.g. on Linux the 'dotnet' command from PATH is a symlink so we need to
// resolve it to get the actual path to the binary
dotnetExe = Interop.Unix.realpath(dotnetExe) ?? dotnetExe;
}
return Path.GetDirectoryName(dotnetExe);
}
}
}