-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MSBuildSdkResolver.cs
273 lines (230 loc) · 11.6 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
// 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 Microsoft.DotNet.Configurer;
using Microsoft.DotNet.DotNetSdkResolver;
using Microsoft.DotNet.NativeWrapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver;
#nullable disable
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.
// 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 Func<string> _getCurrentProcessPath;
private readonly NETCoreSdkResolver _netCoreSdkResolver;
private static CachingWorkloadResolver _staticWorkloadResolver = new CachingWorkloadResolver();
public DotNetMSBuildSdkResolver()
: this(Environment.GetEnvironmentVariable, null, VSSettings.Ambient)
{
}
// Test constructor
public DotNetMSBuildSdkResolver(Func<string, string> getEnvironmentVariable, Func<string> getCurrentProcessPath, VSSettings vsSettings)
{
_getEnvironmentVariable = getEnvironmentVariable;
_getCurrentProcessPath = getCurrentProcessPath;
_netCoreSdkResolver = new NETCoreSdkResolver(getEnvironmentVariable, vsSettings);
}
private sealed class CachedState
{
public string DotnetRoot;
public string MSBuildSdksDir;
public string NETCoreSdkVersion;
public IDictionary<string, string> PropertiesToAdd;
public CachingWorkloadResolver WorkloadResolver;
}
public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext context, SdkResultFactory factory)
{
string dotnetRoot = null;
string msbuildSdksDir = null;
string netcoreSdkVersion = null;
IDictionary<string, string> propertiesToAdd = null;
IDictionary<string, SdkResultItem> itemsToAdd = null;
List<string> warnings = null;
CachingWorkloadResolver workloadResolver = null;
if (context.State is CachedState priorResult)
{
dotnetRoot = priorResult.DotnetRoot;
msbuildSdksDir = priorResult.MSBuildSdksDir;
netcoreSdkVersion = priorResult.NETCoreSdkVersion;
propertiesToAdd = priorResult.PropertiesToAdd;
workloadResolver = priorResult.WorkloadResolver;
}
if (context.IsRunningInVisualStudio)
{
workloadResolver = _staticWorkloadResolver;
}
if (workloadResolver == null)
{
workloadResolver = new CachingWorkloadResolver();
}
if (msbuildSdksDir == null)
{
dotnetRoot = EnvironmentProvider.GetDotnetExeDirectory(_getEnvironmentVariable, _getCurrentProcessPath);
string globalJsonStartDir = GetGlobalJsonStartDir(context);
var resolverResult = _netCoreSdkResolver.ResolveNETCoreSdkDirectory(globalJsonStartDir, context.MSBuildVersion, context.IsRunningInVisualStudio, dotnetRoot);
if (resolverResult.ResolvedSdkDirectory == null)
{
return Failure(
factory,
Strings.UnableToLocateNETCoreSdk);
}
msbuildSdksDir = Path.Combine(resolverResult.ResolvedSdkDirectory, "Sdks");
netcoreSdkVersion = new DirectoryInfo(resolverResult.ResolvedSdkDirectory).Name;
// 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.
var msbuildSdksDirFromEnv = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_DIR");
var netcoreSdkVersionFromEnv = _getEnvironmentVariable("DOTNET_MSBUILD_SDK_RESOLVER_SDKS_VER");
if (!string.IsNullOrEmpty(msbuildSdksDirFromEnv))
{
msbuildSdksDir = msbuildSdksDirFromEnv;
}
if (!string.IsNullOrEmpty(netcoreSdkVersionFromEnv))
{
netcoreSdkVersion = netcoreSdkVersionFromEnv;
}
if (IsNetCoreSDKSmallerThanTheMinimumVersion(netcoreSdkVersion, sdkReference.MinimumVersion))
{
return Failure(
factory,
Strings.NETCoreSDKSmallerThanMinimumRequestedVersion,
netcoreSdkVersion,
sdkReference.MinimumVersion);
}
Version minimumMSBuildVersion = _netCoreSdkResolver.GetMinimumMSBuildVersion(resolverResult.ResolvedSdkDirectory);
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);
}
if (resolverResult.FailedToResolveSDKSpecifiedInGlobalJson)
{
if (warnings == null)
{
warnings = new List<string>();
}
if (!string.IsNullOrWhiteSpace(resolverResult.RequestedVersion))
{
warnings.Add(string.Format(Strings.GlobalJsonResolutionFailedSpecificVersion, resolverResult.RequestedVersion));
}
else
{
warnings.Add(Strings.GlobalJsonResolutionFailed);
}
if (propertiesToAdd == null)
{
propertiesToAdd = new Dictionary<string, string>();
}
propertiesToAdd.Add("SdkResolverHonoredGlobalJson", "false");
propertiesToAdd.Add("SdkResolverGlobalJsonPath", resolverResult.GlobalJsonPath);
}
}
context.State = new CachedState
{
DotnetRoot = dotnetRoot,
MSBuildSdksDir = msbuildSdksDir,
NETCoreSdkVersion = netcoreSdkVersion,
PropertiesToAdd = propertiesToAdd,
WorkloadResolver = workloadResolver
};
// First check if requested SDK resolves to a workload SDK pack
string userProfileDir = CliFolderPathCalculatorCore.GetDotnetUserProfileFolderPath();
var workloadResult = workloadResolver.Resolve(sdkReference.Name, dotnetRoot, netcoreSdkVersion, userProfileDir);
if (workloadResult is not CachingWorkloadResolver.NullResolutionResult)
{
return workloadResult.ToSdkResult(sdkReference, factory);
}
string msbuildSdkDir = Path.Combine(msbuildSdksDir, sdkReference.Name, "Sdk");
if (!Directory.Exists(msbuildSdkDir))
{
return Failure(
factory,
Strings.MSBuildSDKDirectoryNotFound,
msbuildSdkDir);
}
return factory.IndicateSuccess(msbuildSdkDir, netcoreSdkVersion, propertiesToAdd, itemsToAdd, warnings);
}
private static SdkResult Failure(SdkResultFactory factory, string format, params object[] args)
{
return factory.IndicateFailure(new[] { string.Format(format, args) });
}
/// <summary>
/// Gets the starting path to search for global.json.
/// </summary>
/// <param name="context">A <see cref="SdkResolverContext" /> that specifies where the current project is located.</param>
/// <returns>The full path to a starting directory to use when searching for a global.json.</returns>
private static string GetGlobalJsonStartDir(SdkResolverContext context)
{
// Evaluating in-memory projects with MSBuild means that they won't have a solution or project path.
// Default to using the current directory as a best effort to finding a global.json. This could result in
// using the wrong one but without a starting directory, SDK resolution won't work at all. In most cases, a
// global.json won't be found and the default SDK will be used.
string startDir = Environment.CurrentDirectory;
if (!string.IsNullOrWhiteSpace(context.SolutionFilePath))
{
startDir = Path.GetDirectoryName(context.SolutionFilePath);
}
else if(!string.IsNullOrWhiteSpace(context.ProjectFilePath))
{
startDir = Path.GetDirectoryName(context.ProjectFilePath);
}
return startDir;
}
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;
}
}
}