From 5c7fb36477a277557387618dee0fa73de829c570 Mon Sep 17 00:00:00 2001 From: Amaury Leveugle Date: Tue, 17 Nov 2026 21:33:48 +0100 Subject: [PATCH] Gate solution device selection on IsTestingPlatformApplication only IsTestProject is a VSTest concept and is irrelevant for MTP. VSTest projects are never deployed and are rejected later with the VSTest-unsupported error, so they must not opt into device selection. Previously, a VSTest project whose inner build imported ComputeAvailableDevices failed with "No devices are available" instead. Adds ItDoesNotSelectDevicesForVSTestProjectsInSolution. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> (cherry picked from commit e068010513d6617b2ab8653b2438152a95febc8b) --- src/Cli/AGENTS.md | 7 +- .../Commands/Test/MTP/MSBuildUtility.cs | 6 +- .../Test/MTP/SolutionAndProjectUtility.cs | 24 +++--- .../Test/GivenDotnetTestSelectsDevice.cs | 77 ++++++++++++------- 4 files changed, 69 insertions(+), 45 deletions(-) diff --git a/src/Cli/AGENTS.md b/src/Cli/AGENTS.md index b5d0fe7c1ad7..168774fb6e52 100644 --- a/src/Cli/AGENTS.md +++ b/src/Cli/AGENTS.md @@ -126,9 +126,10 @@ target-produced project state, and the session's success/failure cleanup. Cover In `--solution` runs, [`device selection`](dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs) applies only to -test projects (`IsTestProject` or `IsTestingPlatformApplication`). App and library projects, such as -MAUI heads, can import `ComputeAvailableDevices` in their inner builds and must not prompt for a device. The single-project -path evaluates before restore, so it must not apply this NuGet-dependent gate. +MTP test applications (`IsTestingPlatformApplication`); `IsTestProject` is a VSTest concept and must not +opt a project in. App and library projects, such as MAUI heads, can import `ComputeAvailableDevices` in +their inner builds and must not prompt for a device. The single-project path evaluates before restore, so +it must not apply this NuGet-dependent gate. Parent-process operations must also use the captured invocation directory: pass it through background workload updates to both `global.json` discovery and NuGet configuration, and initialize absolute MTP diff --git a/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs b/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs index aab3ad959954..1f6a5d7af592 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs @@ -384,9 +384,9 @@ private static (ConcurrentBag CreateTraversalProjectVisitSet() /// The evaluation happens in the collection of , which is the one /// every project of the run has to share; pass an to reuse an /// existing one and avoid redundant evaluation. - /// When is , only target frameworks that - /// are test projects participate in device selection. Solutions use this so that non-test projects - /// (e.g. a MAUI app or library) that import ComputeAvailableDevices through their workload - /// are not asked for a device. It must only be used after restore, since test-project properties - /// typically come from NuGet package imports. + /// When is , only target + /// frameworks that are Microsoft.Testing.Platform applications participate in device selection. + /// Solutions use this so that other projects (e.g. a MAUI app or library) that import + /// ComputeAvailableDevices through their workload are not asked for a device. It must only be + /// used after restore, since IsTestingPlatformApplication typically comes from NuGet package imports. /// internal static DeviceSelectionEvaluation? EvaluateProjectForDeviceSelection( string projectFilePath, @@ -491,7 +491,7 @@ internal static HashSet CreateTraversalProjectVisitSet() EvaluationContext? evaluationContext = null, string? configuration = null, string? platform = null, - bool requireTestProject = false) + bool requireTestingPlatformApplication = false) { // --device is already handled by HandleDeviceWithTargetFrameworkSelection if (!string.IsNullOrWhiteSpace(buildOptions.Device)) @@ -571,7 +571,7 @@ internal static HashSet CreateTraversalProjectVisitSet() evaluatedProjects[framework] = frameworkProject; } - return new DeviceSelectionEvaluation(evaluatedProjects, testTfmsInParallel, requireTestProject); + return new DeviceSelectionEvaluation(evaluatedProjects, testTfmsInParallel, requireTestingPlatformApplication); } /// @@ -621,7 +621,7 @@ internal static HashSet CreateTraversalProjectVisitSet() internal sealed record DeviceSelectionEvaluation( Dictionary EvaluatedProjects, bool TestTfmsInParallel, - bool RequireTestProject = false) + bool RequireTestingPlatformApplication = false) { public IEnumerable> ProjectsByFramework => EvaluatedProjects.Count == 1 @@ -633,11 +633,9 @@ public bool SupportsDeviceSelection public bool RequiresDeviceSelection(ProjectInstance project) => project.Targets.ContainsKey(Constants.ComputeAvailableDevices) - && (!RequireTestProject - || IsTrue(project.GetPropertyValue(ProjectProperties.IsTestProject)) - || IsTrue(project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication))); - - private static bool IsTrue(string value) => bool.TryParse(value, out bool result) && result; + && (!RequireTestingPlatformApplication + || (bool.TryParse(project.GetPropertyValue(ProjectProperties.IsTestingPlatformApplication), out bool isTestingPlatformApplication) + && isTestingPlatformApplication)); } internal sealed record DeviceSelectionResult( diff --git a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestSelectsDevice.cs b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestSelectsDevice.cs index 228bf6dbec02..2edc93e7f639 100644 --- a/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestSelectsDevice.cs +++ b/test/dotnet.Tests/CommandTests/Test/GivenDotnetTestSelectsDevice.cs @@ -490,51 +490,76 @@ public void ItDoesNotSelectDevicesForNonTestProjectsInSolution() // Mimics a MAUI app in the same solution: the workload imports ComputeAvailableDevices into // its inner builds even though it is not a test project. See https://github.com/dotnet/sdk/issues/57120. - string appDir = Path.Combine(testInstance.Path, "App"); - Directory.CreateDirectory(appDir); - File.WriteAllText(Path.Combine(appDir, "Program.cs"), "System.Console.WriteLine();"); - File.WriteAllText(Path.Combine(appDir, "App.csproj"), + AddProjectWithDeviceTargetsToSolution(testInstance.Path, "App"); + + var result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US") + .Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true", "-bl"); + + result.Should().Pass() + .And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable); + + AssertTargetInBinlog( + Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"), + "ComputeAvailableDevices", + targets => targets.Should().HaveCount(2, "only the test project's target frameworks should discover devices")); + AssertTargetInBinlog( + Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"), + "DeployToDevice", + targets => targets.Should().HaveCount(2, "only the test project's target frameworks should be deployed")); + } + + [TestMethod] + public void ItDoesNotSelectDevicesForVSTestProjectsInSolution() + { + var testInstance = TestAssetsManager.CopyTestAsset("DotnetTestDevices") + .WithSource(); + + // IsTestProject alone marks a VSTest project, which MTP rejects; it must not opt into device selection. + AddProjectWithDeviceTargetsToSolution(testInstance.Path, "VSTestApp", "true"); + + var result = new DotnetTestCommand(Log, disableNewOutput: false) + .WithWorkingDirectory(testInstance.Path) + .WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US") + .Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true"); + + result.Should().Fail() + .And.HaveStdErrContaining(string.Format(CliCommandStrings.CmdUnsupportedVSTestTestApplicationsDescription, "VSTestApp.csproj")) + .And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable); + } + + private static void AddProjectWithDeviceTargetsToSolution(string solutionDirectory, string projectName, string additionalProperties = "") + { + string projectDirectory = Path.Combine(solutionDirectory, projectName); + Directory.CreateDirectory(projectDirectory); + File.WriteAllText(Path.Combine(projectDirectory, "Program.cs"), "System.Console.WriteLine();"); + File.WriteAllText(Path.Combine(projectDirectory, $"{projectName}.csproj"), $$""" Exe {{ToolsetInfo.CurrentTargetFramework}} false + {{additionalProperties}} - + """); - File.WriteAllText(Path.Combine(appDir, "App.Device.targets"), + File.WriteAllText(Path.Combine(projectDirectory, "Device.targets"), """ """); - File.WriteAllText(Path.Combine(testInstance.Path, "TestSolution.slnx"), - """ + File.WriteAllText(Path.Combine(solutionDirectory, "TestSolution.slnx"), + $""" - + """); - - var result = new DotnetTestCommand(Log, disableNewOutput: false) - .WithWorkingDirectory(testInstance.Path) - .WithEnvironmentVariable("DOTNET_CLI_UI_LANGUAGE", "en-US") - .Execute("--solution", "TestSolution.slnx", "-p:SingleDevice=true", "-bl"); - - result.Should().Pass() - .And.NotHaveStdErrContaining(CliCommandStrings.RunCommandNoDevicesAvailable); - - AssertTargetInBinlog( - Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"), - "ComputeAvailableDevices", - targets => targets.Should().HaveCount(2, "only the test project's target frameworks should discover devices")); - AssertTargetInBinlog( - Path.Combine(testInstance.Path, "msbuild-dotnet-test.binlog"), - "DeployToDevice", - targets => targets.Should().HaveCount(2, "only the test project's target frameworks should be deployed")); } [TestMethod]