1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | |
4 | using System; |
5 | using System.Collections.Generic; |
6 | using System.Diagnostics; |
7 | using System.Diagnostics.CodeAnalysis; |
8 | using System.Globalization; |
9 | using System.IO; |
10 | using System.Linq; |
11 | using System.Reflection; |
12 | using System.Reflection.PortableExecutable; |
13 | using System.Text; |
14 | using System.Threading; |
15 | using System.Threading.Tasks; |
16 | |
17 | using Microsoft.Extensions.DependencyModel; |
18 | using Microsoft.TestPlatform.TestHostProvider; |
19 | using Microsoft.TestPlatform.TestHostProvider.Hosting; |
20 | using Microsoft.TestPlatform.TestHostProvider.Resources; |
21 | using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions; |
22 | using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers; |
23 | using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers; |
24 | using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces; |
25 | using Microsoft.VisualStudio.TestPlatform.ObjectModel; |
26 | using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces; |
27 | using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host; |
28 | using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; |
29 | using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities; |
30 | using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; |
31 | using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; |
32 | using Microsoft.VisualStudio.TestPlatform.Utilities; |
33 | using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers; |
34 | using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; |
35 | |
36 | using Newtonsoft.Json; |
37 | using Newtonsoft.Json.Linq; |
38 | |
39 | namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting; |
40 | |
41 | /// <summary> |
42 | /// A host manager for <c>dotnet</c> core runtime. |
43 | /// </summary> |
44 | /// <remarks> |
45 | /// Note that some functionality of this entity overlaps with that of <see cref="DefaultTestHostManager"/>. That is |
46 | /// intentional since we want to move this to a separate assembly (with some runtime extensibility discovery). |
47 | /// </remarks> |
48 | [ExtensionUri(DotnetTestHostUri)] |
49 | [FriendlyName(DotnetTestHostFriendlyName)] |
50 | public class DotnetTestHostManager : ITestRuntimeProvider2 |
51 | { |
52 | private const string DotnetTestHostUri = "HostProvider://DotnetTestHost"; |
53 | // Should the friendly name ever change, please make sure to change the corresponding constant |
54 | // inside ProxyOperationManager::IsTesthostCompatibleWithTestSessions(). |
55 | private const string DotnetTestHostFriendlyName = "DotnetTestHost"; |
56 | private const string TestAdapterRegexPattern = @"TestAdapter.dll"; |
57 | private const string PROCESSOR_ARCHITECTURE = nameof(PROCESSOR_ARCHITECTURE); |
58 | |
59 | private readonly IDotnetHostHelper _dotnetHostHelper; |
60 | private readonly IEnvironment _platformEnvironment; |
61 | private readonly IProcessHelper _processHelper; |
62 | private readonly IRunSettingsHelper _runsettingHelper; |
63 | private readonly IFileHelper _fileHelper; |
64 | private readonly IWindowsRegistryHelper _windowsRegistryHelper; |
65 | private readonly IEnvironmentVariableHelper _environmentVariableHelper; |
66 | |
67 | private ITestHostLauncher? _customTestHostLauncher; |
68 | private Process? _testHostProcess; |
69 | private StringBuilder? _testHostProcessStdError; |
70 | private StringBuilder? _testHostProcessStdOut; |
71 | private bool _hostExitedEventRaised; |
72 | private string _hostPackageVersion = "15.0.0"; |
73 | private Architecture _architecture; |
74 | private Framework? _targetFramework; |
75 | private bool _isVersionCheckRequired = true; |
76 | private string? _dotnetHostPath; |
77 | private bool _captureOutput; |
78 | private protected TestHostManagerCallbacks? _testHostManagerCallbacks; |
79 | |
80 | /// <summary> |
81 | /// Initializes a new instance of the <see cref="DotnetTestHostManager"/> class. |
82 | /// </summary> |
83 | public DotnetTestHostManager() |
84 | : this( |
85 | new ProcessHelper(), |
86 | new FileHelper(), |
87 | new DotnetHostHelper(), |
88 | new PlatformEnvironment(), |
89 | RunSettingsHelper.Instance, |
90 | new WindowsRegistryHelper(), |
91 | new EnvironmentVariableHelper()) |
92 | { |
93 | } |
94 | |
95 | /// <summary> |
96 | /// Initializes a new instance of the <see cref="DotnetTestHostManager"/> class. |
97 | /// </summary> |
98 | /// <param name="processHelper">Process helper instance.</param> |
99 | /// <param name="fileHelper">File helper instance.</param> |
100 | /// <param name="dotnetHostHelper">DotnetHostHelper helper instance.</param> |
101 | /// <param name="platformEnvironment">Platform Environment</param> |
102 | /// <param name="runsettingHelper">RunsettingHelper instance</param> |
103 | /// <param name="windowsRegistryHelper">WindowsRegistryHelper instance</param> |
104 | /// <param name="environmentVariableHelper">EnvironmentVariableHelper instance</param> |
105 | internal DotnetTestHostManager( |
106 | IProcessHelper processHelper, |
107 | IFileHelper fileHelper, |
108 | IDotnetHostHelper dotnetHostHelper, |
109 | IEnvironment platformEnvironment, |
110 | IRunSettingsHelper runsettingHelper, |
111 | IWindowsRegistryHelper windowsRegistryHelper, |
112 | IEnvironmentVariableHelper environmentVariableHelper) |
113 | { |
114 | _processHelper = processHelper; |
115 | _fileHelper = fileHelper; |
116 | _dotnetHostHelper = dotnetHostHelper; |
117 | _platformEnvironment = platformEnvironment; |
118 | _runsettingHelper = runsettingHelper; |
119 | _windowsRegistryHelper = windowsRegistryHelper; |
120 | _environmentVariableHelper = environmentVariableHelper; |
121 | } |
122 | |
123 | /// <inheritdoc /> |
124 | public event EventHandler<HostProviderEventArgs>? HostLaunched; |
125 | |
126 | /// <inheritdoc /> |
127 | public event EventHandler<HostProviderEventArgs>? HostExited; |
128 | |
129 | /// <summary> |
130 | /// Gets a value indicating whether gets a value indicating if the test host can be shared for multiple sources. |
131 | /// </summary> |
132 | /// <remarks> |
133 | /// Dependency resolution for .net core projects are pivoted by the test project. Hence each test |
134 | /// project must be launched in a separate test host process. |
135 | /// </remarks> |
136 | public bool Shared => false; |
137 | |
138 | /// <summary> |
139 | /// Gets a value indicating whether the test host supports protocol version check |
140 | /// By default this is set to true. For host package version 15.0.0, this will be set to false; |
141 | /// </summary> |
142 | internal virtual bool IsVersionCheckRequired |
143 | { |
144 | get |
145 | { |
146 | return _isVersionCheckRequired; |
147 | } |
148 | |
149 | private set |
150 | { |
151 | _isVersionCheckRequired = value; |
152 | } |
153 | } |
154 | |
155 | /// <summary> |
156 | /// Gets a value indicating whether the test host supports protocol version check |
157 | /// </summary> |
158 | internal bool MakeRunsettingsCompatible => _hostPackageVersion.StartsWith("15.0.0-preview"); |
159 | |
160 | /// <summary> |
161 | /// Gets callback on process exit |
162 | /// </summary> |
163 | private Action<object?> ExitCallBack => process => |
164 | { |
165 | TPDebug.Assert(_testHostProcessStdError is not null, "_testHostProcessStdError is null"); |
166 | TestHostManagerCallbacks.ExitCallBack(_processHelper, process, _testHostProcessStdError, OnHostExited); |
167 | }; |
168 | |
169 | /// <summary> |
170 | /// Gets callback to read from process error stream |
171 | /// </summary> |
172 | private Action<object?, string?> ErrorReceivedCallback => (process, data) => |
173 | { |
174 | TPDebug.Assert(_testHostProcessStdError is not null, "_testHostProcessStdError is null"); |
175 | TPDebug.Assert(_testHostManagerCallbacks is not null, "Initialize must have been called before ExitCallBack"); |
176 | _testHostManagerCallbacks.ErrorReceivedCallback(_testHostProcessStdError, data); |
177 | }; |
178 | |
179 | /// <summary> |
180 | /// Gets callback to read from process standard stream |
181 | /// </summary> |
182 | private Action<object?, string?> OutputReceivedCallback => (process, data) => |
183 | { |
184 | TPDebug.Assert(_testHostProcessStdOut is not null, "LaunchTestHostAsync must have been called before OutputReceivedCallback"); |
185 | TPDebug.Assert(_testHostManagerCallbacks is not null, "Initialize must have been called before OutputReceivedCallback"); |
186 | _testHostManagerCallbacks.StandardOutputReceivedCallback(_testHostProcessStdOut, data); |
187 | }; |
188 | |
189 | /// <inheritdoc/> |
190 | public void Initialize(IMessageLogger? logger, string runsettingsXml) |
191 | { |
192 | _hostExitedEventRaised = false; |
193 | var runConfiguration = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettingsXml); |
194 | _captureOutput = runConfiguration.CaptureStandardOutput; |
195 | var forwardOutput = runConfiguration.ForwardStandardOutput; |
196 | _testHostManagerCallbacks = new TestHostManagerCallbacks(forwardOutput, logger); |
197 | |
198 | _architecture = runConfiguration.TargetPlatform; |
199 | _targetFramework = runConfiguration.TargetFramework; |
200 | _dotnetHostPath = runConfiguration.DotnetHostPath; |
201 | } |
202 | |
203 | /// <inheritdoc/> |
204 | public void SetCustomLauncher(ITestHostLauncher customLauncher) |
205 | { |
206 | _customTestHostLauncher = customLauncher; |
207 | } |
208 | |
209 | /// <inheritdoc/> |
210 | public TestHostConnectionInfo GetTestHostConnectionInfo() |
211 | { |
212 | return new TestHostConnectionInfo { Endpoint = "127.0.0.1:0", Role = ConnectionRole.Client, Transport = Transport.Sockets }; |
213 | } |
214 | |
215 | /// <inheritdoc/> |
216 | public Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken) |
217 | { |
218 | // Do NOT offload this to thread pool using Task.Run, we already are on thread pool |
219 | // and this would go into a queue after all the other startup tasks. Meaning we will start |
220 | // testhost much later, and not immediately. |
221 | return Task.FromResult(LaunchHost(testHostStartInfo, cancellationToken)); |
222 | } |
223 | |
224 | /// <inheritdoc/> |
225 | public virtual TestProcessStartInfo GetTestHostProcessStartInfo( |
226 | IEnumerable<string> sources, |
227 | IDictionary<string, string?>? environmentVariables, |
228 | TestRunnerConnectionInfo connectionInfo) |
229 | { |
230 | TPDebug.Assert(_targetFramework is not null, "_targetFramework is null"); |
231 | EqtTrace.Verbose($"DotnetTestHostmanager.GetTestHostProcessStartInfo: Platform environment '{_platformEnvironment.Architecture}' target architecture '{_architecture}' framework '{_targetFramework}' OS '{_platformEnvironment.OperatingSystem}'"); |
232 | |
233 | var startInfo = new TestProcessStartInfo(); |
234 | startInfo.EnvironmentVariables = environmentVariables ?? new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase); |
235 | |
236 | string? dotnetRootPath = _environmentVariableHelper.GetEnvironmentVariable("VSTEST_DOTNET_ROOT_PATH"); |
237 | string? dotnetRootArchitecture = _environmentVariableHelper.GetEnvironmentVariable("VSTEST_DOTNET_ROOT_ARCHITECTURE"); |
238 | |
239 | if (!StringUtilities.IsNullOrWhiteSpace(dotnetRootPath)) |
240 | { |
241 | if (StringUtils.IsNullOrWhiteSpace(dotnetRootArchitecture)) |
242 | { |
243 | throw new InvalidOperationException("'VSTEST_DOTNET_ROOT_PATH' and 'VSTEST_DOTNET_ROOT_ARCHITECTURE' must be both always set. If you are seeing this error, this is a bug in dotnet SDK that sets those variables."); |
244 | } |
245 | |
246 | EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: VSTEST_DOTNET_ROOT_PATH={dotnetRootPath}"); |
247 | EqtTrace.Verbose($"DotnetTestHostmanager.LaunchTestHostAsync: VSTEST_DOTNET_ROOT_ARCHITECTURE={dotnetRootArchitecture}"); |
248 | |
249 | if (!FeatureFlag.Instance.IsSet(FeatureFlag.VSTEST_DISABLE_DOTNET_ROOT_ON_NONWINDOWS)) |
250 | { |
251 | // Set DOTNET_ROOT_<ARCH> for any run, so it gets propagated to testhost and its child processes, like dotnet run does it. This allows executables that start under testhost to find the path to dotnet |
252 | // from which we called dotnet test. Before this change we only expected testhost.exe to be in this situation, but with xunit v3 running separate exe under testhost, the need for setting architecture |
253 | // specific DOTNET_ROOT increases and makes this necessary for users to have good experience. |
254 | SetDotnetRootForArchitecture(startInfo, dotnetRootPath!, dotnetRootArchitecture); |
255 | } |
256 | } |
257 | |
258 | // .NET core host manager is not a shared host. It will expect a single test source to be provided. |
259 | // TODO: Throw an exception when we get 0 or more than 1 source, that explains what happened, instead of .Single throwing a generic exception? |
260 | var args = string.Empty; |
261 | var sourcePath = sources.Single(); |
262 | var sourceFile = Path.GetFileNameWithoutExtension(sourcePath); |
263 | var sourceDirectory = Path.GetDirectoryName(sourcePath)!; |
264 | |
265 | // Probe for runtime config and deps file for the test source |
266 | var runtimeConfigPath = Path.Combine(sourceDirectory, string.Concat(sourceFile, ".runtimeconfig.json")); |
267 | var runtimeConfigFound = false; |
268 | if (_fileHelper.Exists(runtimeConfigPath)) |
269 | { |
270 | runtimeConfigFound = true; |
271 | string argsToAdd = " --runtimeconfig " + runtimeConfigPath.AddDoubleQuote(); |
272 | args += argsToAdd; |
273 | EqtTrace.Verbose("DotnetTestHostmanager: Adding {0} in args", argsToAdd); |
274 | } |
275 | else |
276 | { |
277 | EqtTrace.Verbose("DotnetTestHostmanager: File {0}, does not exist", runtimeConfigPath); |
278 | } |
279 | |
280 | // Use the deps.json for test source |
281 | var depsFilePath = Path.Combine(sourceDirectory, string.Concat(sourceFile, ".deps.json")); |
282 | if (_fileHelper.Exists(depsFilePath)) |
283 | { |
284 | string argsToAdd = " --depsfile " + depsFilePath.AddDoubleQuote(); |
285 | args += argsToAdd; |
286 | EqtTrace.Verbose("DotnetTestHostmanager: Adding {0} in args", argsToAdd); |
287 | } |
288 | else |
289 | { |
290 | EqtTrace.Verbose("DotnetTestHostmanager: File {0}, does not exist", depsFilePath); |
291 | } |
292 | |
293 | var runtimeConfigDevPath = Path.Combine(sourceDirectory, string.Concat(sourceFile, ".runtimeconfig.dev.json")); |
294 | string testHostPath = string.Empty; |
295 | bool useCustomDotnetHostpath = !_dotnetHostPath.IsNullOrEmpty(); |
296 | |
297 | if (useCustomDotnetHostpath) |
298 | { |
299 | EqtTrace.Verbose("DotnetTestHostmanager: User specified custom path to dotnet host: '{0}'.", _dotnetHostPath); |
300 | } |
301 | |
302 | // Try find testhost.exe (or the architecture specific version). We ship those ngened executables for Windows |
303 | // because they have faster startup time. We ship them only for some platforms. |
304 | // When user specified path to dotnet.exe don't try to find the executable, because we will always use the |
305 | // testhost.dll together with their dotnet.exe. |
306 | bool testHostExeFound = false; |
307 | if (!useCustomDotnetHostpath |
308 | && _platformEnvironment.OperatingSystem.Equals(PlatformOperatingSystem.Windows) |
309 | // On arm we cannot rely on apphost and we'll use dotnet.exe muxer. |
310 | && !IsWinOnArm()) |
311 | { |
312 | // testhost.exe is 64-bit and has no suffix other versions have architecture suffix. |
313 | var exeName = _architecture is Architecture.X64 or Architecture.Default or Architecture.AnyCPU |
314 | ? "testhost.exe" |
315 | : $"testhost.{_architecture.ToString().ToLowerInvariant()}.exe"; |
316 | |
317 | var fullExePath = Path.Combine(sourceDirectory, exeName); |
318 | |
319 | // check for testhost.exe in sourceDirectory. If not found, check in nuget folder. |
320 | if (_fileHelper.Exists(fullExePath)) |
321 | { |
322 | EqtTrace.Verbose($"DotnetTestHostManager: {exeName} found at path: " + fullExePath); |
323 | startInfo.FileName = fullExePath; |
324 | testHostExeFound = true; |
325 | } |
326 | else |
327 | { |
328 | // Check if testhost.dll is found in nuget folder or next to the test.dll, and use that to locate testhost.exe that is in the build folder in the same Nuget package. |
329 | testHostPath = GetTestHostPath(runtimeConfigDevPath, depsFilePath, sourceDirectory); |
330 | if (!testHostPath.IsNullOrWhiteSpace() && testHostPath.IndexOf("microsoft.testplatform.testhost", StringComparison.OrdinalIgnoreCase) >= 0) |
331 | { |
332 | // testhost.dll is present in path {testHostNugetRoot}\lib\net8.0\testhost.dll |
333 | // testhost.(x86).exe is present in location {testHostNugetRoot}\build\net8.0\{x86/x64}\{testhost.x86.exe/testhost.exe} |
334 | var folderName = _architecture is Architecture.X64 or Architecture.Default or Architecture.AnyCPU |
335 | ? Architecture.X64.ToString().ToLowerInvariant() |
336 | : _architecture.ToString().ToLowerInvariant(); |
337 | |
338 | var testHostNugetRoot = new DirectoryInfo(testHostPath).Parent!.Parent!.Parent!; |
339 | |
340 | #if DOTNET_BUILD_FROM_SOURCE |
341 | var testHostExeNugetPath = Path.Combine(testHostNugetRoot.FullName, "build", "net8.0", folderName, exeName); |
342 | if (!_fileHelper.Exists(testHostExeNugetPath)) |
343 | { |
344 | testHostExeNugetPath = Path.Combine(testHostNugetRoot.FullName, "build", "net9.0", folderName, exeName); |
345 | } |
346 | #else |
347 | var testHostExeNugetPath = Path.Combine(testHostNugetRoot.FullName, "build", "net8.0", folderName, exeName); |
348 | #endif |
349 | |
350 | if (_fileHelper.Exists(testHostExeNugetPath)) |
351 | { |
352 | EqtTrace.Verbose("DotnetTestHostManager: Testhost.exe/testhost.x86.exe found at path: " + testHostExeNugetPath); |
353 | startInfo.FileName = testHostExeNugetPath; |
354 | testHostExeFound = true; |
355 | } |
356 | } |
357 | } |
358 | } |
359 | |
360 | if (!testHostExeFound) |
361 | { |
362 | // We did not find testhost.exe, either it did not exist, or we are not on Windows, or the user forced a custom path to dotnet. So we will try |
363 | // to find testhost.dll from the runtime config and deps.json. |
364 | if (testHostPath.IsNullOrEmpty()) |
365 | { |
366 | testHostPath = GetTestHostPath(runtimeConfigDevPath, depsFilePath, sourceDirectory); |
367 | } |
368 | |
369 | if (testHostPath.IsNullOrEmpty()) |
370 | { |
371 | // We still did not find testhost.dll. Try finding it next to vstest.console, (or in next to vstest.console ./TestHostNet for .NET Framework) |
372 | #if NETFRAMEWORK |
373 | var testHostNextToRunner = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!, "TestHostNet", "testhost.dll"); |
374 | #else |
375 | var testHostNextToRunner = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!, "testhost.dll"); |
376 | #endif |
377 | if (_fileHelper.Exists(testHostNextToRunner)) |
378 | { |
379 | EqtTrace.Verbose("DotnetTestHostManager: Found testhost.dll next to runner executable: {0}.", testHostNextToRunner); |
380 | testHostPath = testHostNextToRunner; |
381 | |
382 | // Because we could not find the testhost based on the project reference, or next to the tested dll, |
383 | // it is most likely not referenced from it, or the .dll is unmanaged. |
384 | // |
385 | // Add additional deps, that describes the dependencies of testhost.dll, which will merge with deps.json |
386 | // if the process provided any, or will be used by itself if none was provided. |
387 | var testhostDeps = Path.Combine(Path.GetDirectoryName(testHostNextToRunner)!, "testhost.deps.json"); |
388 | string argsToAdd = " --additional-deps " + testhostDeps.AddDoubleQuote(); |
389 | args += argsToAdd; |
390 | EqtTrace.Verbose("DotnetTestHostmanager: Adding {0} in args", argsToAdd); |
391 | |
392 | // Additional deps will contain relative paths, tell the process to search for the dlls also |
393 | // next to the testhost.dll. The additional deps file is specially crafted to keep all the |
394 | // .dlls in the root folder, by only referencing libraries, and setting the path to "/". |
395 | // Without this, e.g. using the normal deps.json that is generated when testhost.dll is built, |
396 | // dotnet would consider additional deps path as the root of a Nuget package source, |
397 | // and would try to locate the dlls in a more complicated folder structure, and would fail to |
398 | // find those dependencies. |
399 | // |
400 | // If they were in the base path (where the test dll is) it would work |
401 | // fine, because in base folder, dotnet searches directly in that folder, but not in probing paths. |
402 | var testHostProbingPath = Path.GetDirectoryName(testHostNextToRunner)!; |
403 | argsToAdd = " --additionalprobingpath " + testHostProbingPath.AddDoubleQuote(); |
404 | args += argsToAdd; |
405 | EqtTrace.Verbose("DotnetTestHostmanager: Adding {0} in args", argsToAdd); |
406 | |
407 | if (!runtimeConfigFound) |
408 | { |
409 | // When runtime config is not found, we don't know which version exactly should be selected for the runtime. |
410 | // This can happen when the test project is .NET (Core) but does not have EXE output type, or when the dll is native. |
411 | // |
412 | // When the project is .NET (Core) we can look at the TargetFramework and gather the rough version from there. We then |
413 | // provide a runtime config targetting that version. It rolls forward on the minor version by default, so the latest |
414 | // version that is present will be selected in that range. Same as if you had EXE and no special settings. |
415 | // E.g. the dll targets netcoreapp3.1, we get 3.1 from the attribute in the Dll, and provide testhost-3.1.runtimeconfig.json |
416 | // this will resolve to 3.1.17 runtime because that is the latest installed on the system. |
417 | // |
418 | // |
419 | // In the other case, where the Dll is native, we take the a runtime config that will roll forward to the latest version |
420 | // because we don't care on which version we will run, and rolling forward gives us the best chance of findind some runtime. |
421 | // |
422 | // |
423 | // There are 2 options how to provide the runtime version. Using --runtimeconfig, and --fx-version. The --fx-version does |
424 | // not roll forward even when the --roll-forward option is provided (or --roll-forward-on-no-candidate-fx for netcoreapp2.1) |
425 | // and we don't know the exact version we want to use. So the only option for us is to use the runtimeconfig.json. |
426 | // |
427 | // |
428 | // TODO: This version check is a hack, when the target framework is figured out it tries to unify to a single common framework |
429 | // even if there are incompatible frameworks (e.g any .NET Framwork assembly and any .NET (Core) assembly). Those incompatibilities |
430 | // will fall back to a common default framework. And that framework (stored in Framework.DefaultFramework) depends on compile time variables |
431 | // so depending on the version of vstest.console you are using, you will get a different value. This value for vstest.console.exe (under VS) |
432 | // is .NET Framework 4, but for vstest.console.dll (under dotnet test) is .NET Core 1.0. Those values are also valid values, so we have no idea |
433 | // if user actually provided a .NET Core 1.0 dll, or we are using fallback because we are running under vstest.console, and there is conflict, |
434 | // or if user provided native dll which does not have the attribute (that we read via PEReader). |
435 | // |
436 | // Another aspect of this is that we are unifying the dlls, so until we add per assembly data, this would be less accurate than using runtimeconfig.json |
437 | // but we can work around that by 1) changing how we schedule runners, to make sure we can process more that 1 type of assembly in vstest.console and |
438 | // 2) making sure we still make the project executable (and so we actually do get runtimeconfig unless the user tries hard to not make the test and EXE). |
439 | var suffix = _targetFramework.Version == "1.0.0.0" ? "latest" : $"{new Version(_targetFramework.Version).Major}.{new Version(_targetFramework.Version).Minor}"; |
440 | var testhostRuntimeConfig = Path.Combine(Path.GetDirectoryName(testHostNextToRunner)!, $"testhost-{suffix}.runtimeconfig.json"); |
441 | if (!_fileHelper.Exists(testhostRuntimeConfig)) |
442 | { |
443 | testhostRuntimeConfig = Path.Combine(Path.GetDirectoryName(testHostNextToRunner)!, $"testhost-latest.runtimeconfig.json"); |
444 | } |
445 | |
446 | argsToAdd = " --runtimeconfig " + testhostRuntimeConfig.AddDoubleQuote(); |
447 | args += argsToAdd; |
448 | EqtTrace.Verbose("DotnetTestHostmanager: Adding {0} in args", argsToAdd); |
449 | } |
450 | } |
451 | } |
452 | |
453 | if (testHostPath.IsNullOrEmpty()) |
454 | { |
455 | throw new TestPlatformException("Could not find testhost"); |
456 | } |
457 | |
458 | // We silently force x64 only if the target architecture is the default one and is not specified by user |
459 | // through --arch or runsettings or -- RunConfiguration.TargetPlatform=arch |
460 | bool forceToX64 = _runsettingHelper.IsDefaultTargetArchitecture && SilentlyForceToX64(sourcePath); |
461 | EqtTrace.Verbose($"DotnetTestHostmanager: Current process architetcure '{_processHelper.GetCurrentProcessArchitecture()}'"); |
462 | bool isSameArchitecture = IsSameArchitecture(_architecture, _processHelper.GetCurrentProcessArchitecture()); |
463 | var currentProcessPath = _processHelper.GetCurrentProcessFileName()!; |
464 | bool isRunningWithDotnetMuxer = IsRunningWithDotnetMuxer(currentProcessPath); |
465 | if (useCustomDotnetHostpath) |
466 | { |
467 | startInfo.FileName = _dotnetHostPath; |
468 | } |
469 | |
470 | // If already running with the dotnet executable and the architecture is compatible, use it; otherwise search the correct muxer architecture on disk. |
471 | else if (isRunningWithDotnetMuxer && isSameArchitecture && !forceToX64) |
472 | { |
473 | EqtTrace.Verbose("DotnetTestHostmanager.LaunchTestHostAsync: Compatible muxer architecture of running process '{0}' and target architecture '{1}'", _processHelper.GetCurrentProcessArchitecture(), _architecture); |
474 | startInfo.FileName = currentProcessPath; |
475 | } |
476 | else |
477 | { |
478 | PlatformArchitecture targetArchitecture = TranslateToPlatformArchitecture(_architecture); |
479 | EqtTrace.Verbose($"DotnetTestHostmanager: Searching muxer for the architecture '{targetArchitecture}', OS '{_platformEnvironment.OperatingSystem}' framework '{_targetFramework}' SDK platform architecture '{_platformEnvironment.Architecture}'"); |
480 | if (forceToX64) |
481 | { |
482 | EqtTrace.Verbose($"DotnetTestHostmanager: Forcing the search to x64 architecure, IsDefaultTargetArchitecture '{_runsettingHelper.IsDefaultTargetArchitecture}' OS '{_platformEnvironment.OperatingSystem}' framework '{_targetFramework}'"); |
483 | } |
484 | |
485 | // Check if DOTNET_ROOT resolution should be bypassed. |
486 | var shouldIgnoreDotnetRoot = (_environmentVariableHelper.GetEnvironmentVariable("VSTEST_IGNORE_DOTNET_ROOT")?.Trim() ?? "0") != "0"; |
487 | var muxerResolutionStrategy = shouldIgnoreDotnetRoot |
488 | ? (DotnetMuxerResolutionStrategy.GlobalInstallationLocation | DotnetMuxerResolutionStrategy.DefaultInstallationLocation) |
489 | : DotnetMuxerResolutionStrategy.Default; |
490 | |
491 | PlatformArchitecture finalTargetArchitecture = forceToX64 ? PlatformArchitecture.X64 : targetArchitecture; |
492 | if (!_dotnetHostHelper.TryGetDotnetPathByArchitecture(finalTargetArchitecture, muxerResolutionStrategy, out string? muxerPath)) |
493 | { |
494 | string message = string.Format(CultureInfo.CurrentCulture, Resources.NoDotnetMuxerFoundForArchitecture, $"dotnet{(_platformEnvironment.OperatingSystem == PlatformOperatingSystem.Windows ? ".exe" : string.Empty)}", finalTargetArchitecture.ToString()); |
495 | EqtTrace.Error(message); |
496 | throw new TestPlatformException(message); |
497 | } |
498 | |
499 | startInfo.FileName = muxerPath; |
500 | } |
501 | |
502 | EqtTrace.Verbose("DotnetTestHostmanager: Full path of testhost.dll is {0}", testHostPath); |
503 | args = "exec" + args; |
504 | args += " " + testHostPath.AddDoubleQuote(); |
505 | } |
506 | |
507 | EqtTrace.Verbose("DotnetTestHostmanager: Full path of host exe is {0}", startInfo.FileName); |
508 | |
509 | // Attempt to upgrade netcoreapp2.1 and earlier versions of testhost to netcoreapp3.1 or a newer runtime, |
510 | // assuming that the user does not have that old runtime installed. |
511 | if (_targetFramework.Name.StartsWith(".NETCoreApp,", StringComparison.OrdinalIgnoreCase) |
512 | && Version.TryParse(_targetFramework.Version, out var version) |
513 | && version < new Version(3, 0)) |
514 | { |
515 | args += " --roll-forward Major"; |
516 | } |
517 | |
518 | args += " " + connectionInfo.ToCommandLineOptions(); |
519 | |
520 | // Create a additional probing path args with Nuget.Client |
521 | // args += "--additionalprobingpath xxx" |
522 | // TODO this may be required in ASP.net, requires validation |
523 | |
524 | // Sample command line for the spawned test host |
525 | // "D:\dd\gh\Microsoft\vstest\tools\dotnet\dotnet.exe" exec |
526 | // --runtimeconfig G:\tmp\netcore-test\bin\Debug\netcoreapp1.0\netcore-test.runtimeconfig.json |
527 | // --depsfile G:\tmp\netcore-test\bin\Debug\netcoreapp1.0\netcore-test.deps.json |
528 | // --additionalprobingpath C:\Users\username\.nuget\packages\ |
529 | // G:\nuget-package-path\microsoft.testplatform.testhost\version\**\testhost.dll |
530 | // G:\tmp\netcore-test\bin\Debug\netcoreapp1.0\netcore-test.dll |
531 | startInfo.Arguments = args; |
532 | |
533 | // If we're running using custom apphost we need to set DOTNET_ROOT/DOTNET_ROOT(x86) |
534 | // We're setting it inside SDK to support private install scenario. |
535 | // i.e. I've got only private install and no global installation, in this case apphost needs to use env var to locate runtime. |
536 | if (testHostExeFound) |
537 | { |
538 | if (!StringUtilities.IsNullOrWhiteSpace(dotnetRootPath)) |
539 | { |
540 | // The parent process is passing to us the path in which the dotnet.exe is and is passing the architecture of the dotnet.exe, |
541 | // so if the child process (testhost) is the same architecture it can pick up that dotnet.exe location and run. This is to allow |
542 | // local installations of dotnet/sdk to work with testhost. |
543 | // |
544 | // There are 2 complications in this process: |
545 | // 1) There are differences between how .NET Apphosts are handling DOTNET_ROOT, versions pre-net6 are only looking at |
546 | // DOTNET_ROOT(x86) and then DOTNET_ROOT. This makes is really easy to set DOTNET_ROOT to point at x64 dotnet installation |
547 | // and have that picked up by x86 testhost and fail. |
548 | // Unfortunately vstest.console has to support both new (17.14+) testhosts that are built against net8, and old (pre 17.14) |
549 | // testhosts that are built using netcoreapp3.1 apphost, and so their approach to resolving DOTNET_ROOT differ. |
550 | // |
551 | // /!\ The apphost version does not align with the targeted framework (tfm), an older testhost is built against netcoreapp3.1 |
552 | // but can be used to run net8 tests. The only way to tell is the version of the testhost. |
553 | // |
554 | // netcoreapp3.1 hosts only support DOTNET_ROOT and DOTNET_ROOT(x86) env variables. |
555 | // net8 hosts, support also DOTNET_ROOT_<ARCH> variables, which is what we should prefer to set the location of dotnet |
556 | // in a more architecture specific way. |
557 | // |
558 | // 2) The surrounding environment might already have the environment variables set, most likely by setting DOTNET_ROOT, which is |
559 | // a universal way of setting where the dotnet is, that works across all different architectures of the .NET apphost. |
560 | // By setting our (hopefully more specific variable) we might overwrite what user specified, and in case of DOTNET_ROOT it is probably |
561 | // preferable when we can set the DOTNET_ROOT_<ARCH> variable. |
562 | var testhostDllPath = Path.ChangeExtension(startInfo.FileName, ".dll"); |
563 | // This file check is for unit tests, we expect the file to always be there. Otherwise testhost.exe would not be able to run. |
564 | var testhostVersionInfo = _fileHelper.Exists(testhostDllPath) ? FileVersionInfo.GetVersionInfo(testhostDllPath) : null; |
565 | if (testhostVersionInfo != null && testhostVersionInfo.ProductMajorPart >= 17 && testhostVersionInfo.ProductMinorPart >= 14) |
566 | { |
567 | // This is a new testhost that builds at least against net8 we should set the architecture specific DOTNET_ROOT_<ARCH>. |
568 | // |
569 | // We ship just testhost.exe and testhost.x86.exe if the architecture is different we won't find the testhost*.exe and |
570 | // won't reach this code, but let's write this in a generic way anyway, to avoid breaking if we add more variants of testhost*.exe. |
571 | // |
572 | // If the feature flag is set, revert to previous behavior of setting DOTNET_ROOT_<ARCH> only on Windows after we found testhost.exe. |
573 | if (FeatureFlag.Instance.IsSet(FeatureFlag.VSTEST_DISABLE_DOTNET_ROOT_ON_NONWINDOWS)) |
574 | { |
575 | SetDotnetRootForArchitecture(startInfo, dotnetRootPath!, dotnetRootArchitecture!); |
576 | } |
577 | } |
578 | else |
579 | { |
580 | // This is an old testhost that built against netcoreapp3.1, it does not understand architecture specific DOTNET_ROOT_<ARCH>, we have to set it more carefully |
581 | // to avoid setting DOTNET_ROOT that points to x64 but is picked up by x86 host. |
582 | // |
583 | // Also avoid setting it if we are already getting it from the surrounding environment. |
584 | var architectureFromEnv = (Architecture)Enum.Parse(typeof(Architecture), dotnetRootArchitecture!, ignoreCase: true); |
585 | if (architectureFromEnv == _architecture) |
586 | { |
587 | if (_architecture == Architecture.X86) |
588 | { |
589 | const string dotnetRootX86 = "DOTNET_ROOT(x86)"; |
590 | if (StringUtils.IsNullOrWhiteSpace(_environmentVariableHelper.GetEnvironmentVariable(dotnetRootX86))) |
591 | { |
592 | startInfo.EnvironmentVariables.Add(dotnetRootX86, dotnetRootPath); |
593 | } |
594 | } |
595 | else |
596 | { |
597 | const string dotnetRoot = "DOTNET_ROOT"; |
598 | if (StringUtils.IsNullOrWhiteSpace(_environmentVariableHelper.GetEnvironmentVariable(dotnetRoot))) |
599 | { |
600 | startInfo.EnvironmentVariables.Add(dotnetRoot, dotnetRootPath); |
601 | } |
602 | } |
603 | } |
604 | } |
605 | } |
606 | } |
607 | |
608 | startInfo.WorkingDirectory = sourceDirectory; |
609 | |
610 | return startInfo; |
611 | |
612 | bool IsRunningWithDotnetMuxer(string currentProcessPath) |
613 | => currentProcessPath.EndsWith("dotnet", StringComparison.OrdinalIgnoreCase) || |
614 | currentProcessPath.EndsWith("dotnet.exe", StringComparison.OrdinalIgnoreCase); |
615 | |
616 | bool IsWinOnArm() |
617 | { |
618 | bool isWinOnArm = false; |
619 | if (_platformEnvironment.OperatingSystem == PlatformOperatingSystem.Windows) |
620 | { |
621 | string? processorArchitecture = Environment.GetEnvironmentVariable(PROCESSOR_ARCHITECTURE, EnvironmentVariableTarget.Machine); |
622 | if (processorArchitecture is not null) |
623 | { |
624 | EqtTrace.Verbose($"DotnetTestHostmanager.IsWinOnArm: Current PROCESSOR_ARCHITECTURE from environment variable '{processorArchitecture}'"); |
625 | isWinOnArm = processorArchitecture.ToString().ToLowerInvariant().Contains("arm"); |
626 | } |
627 | } |
628 | |
629 | EqtTrace.Verbose($"DotnetTestHostmanager.IsWinOnArm: Is Windows on ARM '{isWinOnArm}'"); |
630 | return isWinOnArm; |
631 | } |
632 | |
633 | PlatformArchitecture TranslateToPlatformArchitecture(Architecture targetArchitecture) |
634 | { |
635 | switch (targetArchitecture) |
636 | { |
637 | case Architecture.X86: |
638 | return PlatformArchitecture.X86; |
639 | case Architecture.X64: |
640 | return PlatformArchitecture.X64; |
641 | case Architecture.ARM: |
642 | return PlatformArchitecture.ARM; |
643 | case Architecture.ARM64: |
644 | return PlatformArchitecture.ARM64; |
645 | case Architecture.S390x: |
646 | return PlatformArchitecture.S390x; |
647 | case Architecture.Ppc64le: |
648 | return PlatformArchitecture.Ppc64le; |
649 | case Architecture.RiscV64: |
650 | return PlatformArchitecture.RiscV64; |
651 | case Architecture.AnyCPU: |
652 | case Architecture.Default: |
653 | default: |
654 | break; |
655 | } |
656 | |
657 | throw new TestPlatformException($"Invalid target architecture '{targetArchitecture}'"); |
658 | } |
659 | |
660 | static bool IsSameArchitecture(Architecture targetArchitecture, PlatformArchitecture platformAchitecture) |
661 | => targetArchitecture switch |
662 | { |
663 | Architecture.X86 => platformAchitecture == PlatformArchitecture.X86, |
664 | Architecture.X64 => platformAchitecture == PlatformArchitecture.X64, |
665 | Architecture.ARM => platformAchitecture == PlatformArchitecture.ARM, |
666 | Architecture.ARM64 => platformAchitecture == PlatformArchitecture.ARM64, |
667 | Architecture.S390x => platformAchitecture == PlatformArchitecture.S390x, |
668 | Architecture.Ppc64le => platformAchitecture == PlatformArchitecture.Ppc64le, |
669 | Architecture.RiscV64 => platformAchitecture == PlatformArchitecture.RiscV64, |
670 | _ => throw new TestPlatformException($"Invalid target architecture '{targetArchitecture}'"), |
671 | }; |
672 | |
673 | bool SilentlyForceToX64(string sourcePath) |
674 | { |
675 | // We need to force x64 in some scenario |
676 | // https://github.com/dotnet/sdk/blob/main/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets#L140-L143 |
677 | |
678 | // If we are running on an M1 with a native SDK and the TFM is < 6.0, we have to use a x64 apphost since there are no osx-arm64 apphosts previous to .NET 6.0. |
679 | if (_platformEnvironment.OperatingSystem == PlatformOperatingSystem.OSX && |
680 | _platformEnvironment.Architecture == PlatformArchitecture.ARM64 && |
681 | new Version(_targetFramework.Version).Major < 6) |
682 | { |
683 | return true; |
684 | } |
685 | |
686 | // If we are running on win-arm64 and the TFM is < 5.0, we have to use a x64 apphost since there are no win-arm64 apphosts previous to .NET 5.0. |
687 | return _platformEnvironment.OperatingSystem == PlatformOperatingSystem.Windows && |
688 | _platformEnvironment.Architecture == PlatformArchitecture.ARM64 && |
689 | new Version(_targetFramework.Version).Major < 5 && |
690 | !IsNativeModule(sourcePath); |
691 | } |
692 | |
693 | bool IsNativeModule(string modulePath) |
694 | { |
695 | // Scenario: dotnet test nativeArm64.dll for CppUnitTestFramework |
696 | // If the dll is native and we're not running in process(vstest.console.exe) |
697 | // the expected target framework is ".NETCoreApp,Version=v1.0". |
698 | // In this case we don't want to force x64 architecture |
699 | using var assemblyStream = _fileHelper.GetStream(sourcePath, FileMode.Open, FileAccess.Read); |
700 | using var peReader = new PEReader(assemblyStream); |
701 | if (!peReader.HasMetadata || (peReader.PEHeaders.CorHeader?.Flags & CorFlags.ILOnly) == 0) |
702 | { |
703 | EqtTrace.Verbose($"DotnetTestHostmanager.IsNativeModule: Source '{sourcePath}' is native."); |
704 | return true; |
705 | } |
706 | |
707 | return false; |
708 | } |
709 | } |
710 | |
711 | private void SetDotnetRootForArchitecture(TestProcessStartInfo startInfo, string dotnetRootPath, string dotnetRootArchitecture) |
712 | { |
713 | var environmentVariableName = $"DOTNET_ROOT_{dotnetRootArchitecture.ToUpperInvariant()}"; |
714 | |
715 | var existingDotnetRoot = _environmentVariableHelper.GetEnvironmentVariable(environmentVariableName); |
716 | if (!StringUtilities.IsNullOrWhiteSpace(existingDotnetRoot)) |
717 | { |
718 | EqtTrace.Verbose($"DotnetTestHostManager.SetDotnetRootForArchitecture: The variable {environmentVariableName} is already set in the surrounding environment, don't add it to testhost start info, because we want to keep what user provided externally."); |
719 | } |
720 | else |
721 | { |
722 | startInfo.EnvironmentVariables ??= new Dictionary<string, string?>(); |
723 | |
724 | // Set the architecture specific variable to the environment of the process so it is picked up. |
725 | startInfo.EnvironmentVariables.Add(environmentVariableName, dotnetRootPath); |
726 | |
727 | EqtTrace.Verbose($"DotnetTestHostManager.SetDotnetRootForArchitecture: Adding {environmentVariableName}={dotnetRootPath} to testhost start info."); |
728 | } |
729 | } |
730 | |
731 | /// <inheritdoc/> |
732 | public IEnumerable<string> GetTestPlatformExtensions(IEnumerable<string> sources, IEnumerable<string> extensions) |
733 | { |
734 | List<string> extensionPaths = new(); |
735 | var sourceDirectory = Path.GetDirectoryName(sources.Single()); |
736 | |
737 | if (!sourceDirectory.IsNullOrEmpty() && _fileHelper.DirectoryExists(sourceDirectory)) |
738 | { |
739 | extensionPaths.AddRange(_fileHelper.EnumerateFiles(sourceDirectory, SearchOption.TopDirectoryOnly, TestAdapterRegexPattern)); |
740 | } |
741 | |
742 | return extensionPaths; |
743 | } |
744 | |
745 | /// <inheritdoc/> |
746 | public IEnumerable<string> GetTestSources(IEnumerable<string> sources) |
747 | { |
748 | // We do not have scenario where netcore tests are deployed to remote machine, so no need to update sources |
749 | return sources; |
750 | } |
751 | |
752 | /// <inheritdoc/> |
753 | public bool CanExecuteCurrentRunConfiguration(string? runsettingsXml) |
754 | { |
755 | var config = XmlRunSettingsUtilities.GetRunConfigurationNode(runsettingsXml); |
756 | var framework = config.TargetFramework; |
757 | |
758 | return framework!.Name.IndexOf("netcoreapp", StringComparison.OrdinalIgnoreCase) >= 0 |
759 | || framework.Name.IndexOf("net5", StringComparison.OrdinalIgnoreCase) >= 0; |
760 | } |
761 | |
762 | /// <inheritdoc/> |
763 | public Task CleanTestHostAsync(CancellationToken cancellationToken) |
764 | { |
765 | try |
766 | { |
767 | _processHelper.TerminateProcess(_testHostProcess); |
768 | } |
769 | catch (Exception ex) |
770 | { |
771 | EqtTrace.Warning("DotnetTestHostManager: Unable to terminate test host process: " + ex); |
772 | } |
773 | |
774 | _testHostProcess?.Dispose(); |
775 | |
776 | return Task.FromResult(true); |
777 | } |
778 | |
779 | /// <inheritdoc /> |
780 | public bool AttachDebuggerToTestHost() |
781 | { |
782 | TPDebug.Assert(_targetFramework is not null && _testHostProcess is not null, "DotnetTestHostManager: TargetFramework is null"); |
783 | return _customTestHostLauncher switch |
784 | { |
785 | ITestHostLauncher3 launcher3 => launcher3.AttachDebuggerToProcess(new AttachDebuggerInfo { ProcessId = _testHostProcess.Id, TargetFramework = _targetFramework.ToString() }, CancellationToken.None), |
786 | ITestHostLauncher2 launcher2 => launcher2.AttachDebuggerToProcess(_testHostProcess.Id), |
787 | _ => false, |
788 | }; |
789 | } |
790 | |
791 | /// <summary> |
792 | /// Raises HostLaunched event |
793 | /// </summary> |
794 | /// <param name="e">host provider event args</param> |
795 | private void OnHostLaunched(HostProviderEventArgs e) |
796 | { |
797 | HostLaunched?.SafeInvoke(this, e, "HostProviderEvents.OnHostLaunched"); |
798 | } |
799 | |
800 | /// <summary> |
801 | /// Raises HostExited event |
802 | /// </summary> |
803 | /// <param name="e">host provider event args</param> |
804 | private void OnHostExited(HostProviderEventArgs e) |
805 | { |
806 | if (!_hostExitedEventRaised) |
807 | { |
808 | _hostExitedEventRaised = true; |
809 | EqtTrace.Verbose("DotnetTestHostManager.OnHostExited: invoking OnHostExited callback"); |
810 | HostExited?.SafeInvoke(this, e, "HostProviderEvents.OnHostExited"); |
811 | } |
812 | else |
813 | { |
814 | EqtTrace.Verbose("DotnetTestHostManager.OnHostExited: exit event was already raised, skipping"); |
815 | } |
816 | } |
817 | |
818 | [MemberNotNull(nameof(_testHostProcessStdError))] |
819 | [MemberNotNullWhen(true, nameof(_testHostProcess))] |
820 | private bool LaunchHost(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken) |
821 | { |
822 | _testHostProcessStdError = new StringBuilder(0, CoreUtilities.Constants.StandardErrorMaxLength); |
823 | _testHostProcessStdOut = new StringBuilder(0, CoreUtilities.Constants.StandardErrorMaxLength); |
824 | |
825 | // We launch the test host process here if we're on the normal test running workflow. |
826 | // If we're debugging and we have access to the newest version of the testhost launcher |
827 | // interface we launch it here as well, but we expect to attach later to the test host |
828 | // process by using its PID. |
829 | // For every other workflow (e.g.: profiling) we ask the IDE to launch the custom test |
830 | // host for us. In the profiling case this is needed because then the IDE sets some |
831 | // additional environmental variables for us to help with probing. |
832 | if ((_customTestHostLauncher == null) |
833 | || (_customTestHostLauncher.IsDebug |
834 | && _customTestHostLauncher is ITestHostLauncher2)) |
835 | { |
836 | if (EqtTrace.IsVerboseEnabled) |
837 | { |
838 | var dotnetEnvVars = testHostStartInfo.EnvironmentVariables? |
839 | .Where(kvp => kvp.Key.StartsWith("DOTNET_", StringComparison.OrdinalIgnoreCase)) |
840 | .Select(kvp => $"{kvp.Key}={kvp.Value}") |
841 | .ToArray() ?? Array.Empty<string>(); |
842 | |
843 | EqtTrace.Verbose($"DotnetTestHostManager: Starting process '{0}' with command line '{1}' and DOTNET environment: {string.Join(", ", dotnetEnvVars)} ", testHostStartInfo.FileName, testHostStartInfo.Arguments); |
844 | } |
845 | |
846 | cancellationToken.ThrowIfCancellationRequested(); |
847 | |
848 | var outputCallback = _captureOutput ? OutputReceivedCallback : null; |
849 | _testHostProcess = _processHelper.LaunchProcess( |
850 | testHostStartInfo.FileName!, |
851 | testHostStartInfo.Arguments, |
852 | testHostStartInfo.WorkingDirectory, |
853 | testHostStartInfo.EnvironmentVariables, |
854 | ErrorReceivedCallback, |
855 | ExitCallBack, |
856 | outputCallback) as Process; |
857 | } |
858 | else |
859 | { |
860 | var processId = _customTestHostLauncher.LaunchTestHost(testHostStartInfo, cancellationToken); |
861 | _testHostProcess = Process.GetProcessById(processId); |
862 | _processHelper.SetExitCallback(processId, ExitCallBack); |
863 | } |
864 | |
865 | if (_testHostProcess is null) |
866 | { |
867 | return false; |
868 | } |
869 | |
870 | DefaultTestHostManager.AdjustProcessPriorityBasedOnSettings(_testHostProcess, testHostStartInfo.EnvironmentVariables); |
871 | OnHostLaunched(new HostProviderEventArgs("Test Runtime launched", 0, _testHostProcess.Id)); |
872 | return true; |
873 | } |
874 | |
875 | private string GetTestHostPath(string runtimeConfigDevPath, string depsFilePath, string sourceDirectory) |
876 | { |
877 | string testHostPackageName = "microsoft.testplatform.testhost"; |
878 | // This must be empty string, otherwise the Path.Combine below |
879 | // will fail if a very specific setup is used where you add our dlls |
880 | // as assemblies directly, but you have no RuntimeAssemblyGroups in deps.json |
881 | // because you don't add our nuget package. In such case we just want to move on |
882 | // to the next fallback. |
883 | string testHostPath = string.Empty; |
884 | |
885 | if (_fileHelper.Exists(depsFilePath)) |
886 | { |
887 | if (_fileHelper.Exists(runtimeConfigDevPath)) |
888 | { |
889 | EqtTrace.Verbose("DotnetTestHostmanager: Reading file {0} to get path of testhost.dll", depsFilePath); |
890 | |
891 | // Get testhost relative path |
892 | using (var stream = _fileHelper.GetStream(depsFilePath, FileMode.Open, FileAccess.Read)) |
893 | { |
894 | var context = new DependencyContextJsonReader().Read(stream); |
895 | var testhostPackage = context.RuntimeLibraries.FirstOrDefault(lib => lib.Name.Equals(testHostPackageName, StringComparison.OrdinalIgnoreCase)); |
896 | |
897 | if (testhostPackage != null) |
898 | { |
899 | foreach (var runtimeAssemblyGroup in testhostPackage.RuntimeAssemblyGroups) |
900 | { |
901 | foreach (var path in runtimeAssemblyGroup.AssetPaths) |
902 | { |
903 | if (path.EndsWith("testhost.dll", StringComparison.OrdinalIgnoreCase)) |
904 | { |
905 | testHostPath = path; |
906 | break; |
907 | } |
908 | } |
909 | } |
910 | |
911 | if (testhostPackage.Path is not null) |
912 | { |
913 | testHostPath = Path.Combine(testhostPackage.Path, testHostPath); |
914 | } |
915 | _hostPackageVersion = testhostPackage.Version; |
916 | IsVersionCheckRequired = !_hostPackageVersion.StartsWith("15.0.0"); |
917 | EqtTrace.Verbose("DotnetTestHostmanager: Relative path of testhost.dll with respect to package folder is {0}", testHostPath); |
918 | } |
919 | } |
920 | |
921 | // Get probing path |
922 | using (StreamReader file = new(_fileHelper.GetStream(runtimeConfigDevPath, FileMode.Open, FileAccess.Read))) |
923 | using (JsonTextReader reader = new(file)) |
924 | { |
925 | JObject context = (JObject)JToken.ReadFrom(reader); |
926 | JObject runtimeOptions = (JObject)context.GetValue("runtimeOptions")!; |
927 | JToken additionalProbingPaths = runtimeOptions.GetValue("additionalProbingPaths")!; |
928 | foreach (var x in additionalProbingPaths) |
929 | { |
930 | EqtTrace.Verbose("DotnetTestHostmanager: Looking for path {0} in folder {1}", testHostPath, x.ToString()); |
931 | string testHostFullPath; |
932 | try |
933 | { |
934 | testHostFullPath = Path.Combine(x.ToString(), testHostPath); |
935 | } |
936 | catch (ArgumentException) |
937 | { |
938 | // https://github.com/Microsoft/vstest/issues/847 |
939 | // skip any invalid paths and continue checking the others |
940 | continue; |
941 | } |
942 | |
943 | if (_fileHelper.Exists(testHostFullPath)) |
944 | { |
945 | EqtTrace.Verbose("DotnetTestHostmanager: Found testhost.dll in {0}", testHostFullPath); |
946 | return testHostFullPath; |
947 | } |
948 | } |
949 | } |
950 | } |
951 | else |
952 | { |
953 | EqtTrace.Verbose("DotnetTestHostmanager: Runtimeconfig.dev.json {0} does not exist.", runtimeConfigDevPath); |
954 | } |
955 | } |
956 | else |
957 | { |
958 | EqtTrace.Verbose("DotnetTestHostmanager: Deps file {0} does not exist.", depsFilePath); |
959 | } |
960 | |
961 | // If we are here it means it couldn't resolve testhost.dll from nuget cache. |
962 | // Try resolving testhost from output directory of test project. This is required if user has published the test project |
963 | // and is running tests in an isolated machine. A second scenario is self test: test platform unit tests take a project |
964 | // dependency on testhost (instead of nuget dependency), this drops testhost to output path. |
965 | var testHostNextToTestProject = Path.Combine(sourceDirectory, "testhost.dll"); |
966 | if (_fileHelper.Exists(testHostNextToTestProject)) |
967 | { |
968 | EqtTrace.Verbose("DotnetTestHostManager: Found testhost.dll in source directory: {0}.", testHostNextToTestProject); |
969 | return testHostNextToTestProject; |
970 | } |
971 | |
972 | return testHostPath; |
973 | } |
974 | } |