Skip to content

Commit e927fd8

Browse files
authored
skip projects that don't support workloads during workload restore (dotnet#42467)
2 parents 68c1c81 + c30cb15 commit e927fd8

File tree

7 files changed

+113
-6
lines changed

7 files changed

+113
-6
lines changed

src/Cli/dotnet/commands/dotnet-workload/restore/WorkloadRestoreCommand.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.CommandLine;
55
using Microsoft.Build.Construction;
66
using Microsoft.Build.Execution;
7-
using Microsoft.Build.Framework;
87
using Microsoft.Build.Logging;
98
using Microsoft.DotNet.Cli;
109
using Microsoft.DotNet.Cli.Utils;
@@ -44,6 +43,8 @@ public override int Execute()
4443
return 0;
4544
}
4645

46+
private static string GetRequiredWorkloadsTargetName = "_GetRequiredWorkloads";
47+
4748
private List<WorkloadId> RunTargetToGetWorkloadIds(IEnumerable<string> allProjects)
4849
{
4950
var globalProperties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
@@ -55,12 +56,15 @@ private List<WorkloadId> RunTargetToGetWorkloadIds(IEnumerable<string> allProjec
5556
foreach (string projectFile in allProjects)
5657
{
5758
var project = new ProjectInstance(projectFile, globalProperties, null);
59+
if (!project.Targets.ContainsKey(GetRequiredWorkloadsTargetName))
60+
{
61+
continue;
62+
}
5863

59-
bool buildResult = project.Build(new[] { "_GetRequiredWorkloads" },
60-
loggers: new ILogger[]
61-
{
64+
bool buildResult = project.Build([GetRequiredWorkloadsTargetName],
65+
loggers: [
6266
new ConsoleLogger(Verbosity.ToLoggerVerbosity())
63-
},
67+
],
6468
remoteLoggers: Enumerable.Empty<ForwardingLoggerRecord>(),
6569
targetOutputs: out var targetOutputs);
6670

@@ -73,7 +77,7 @@ private List<WorkloadId> RunTargetToGetWorkloadIds(IEnumerable<string> allProjec
7377
isUserError: false);
7478
}
7579

76-
var targetResult = targetOutputs["_GetRequiredWorkloads"];
80+
var targetResult = targetOutputs[GetRequiredWorkloadsTargetName];
7781
allWorkloadId.AddRange(targetResult.Items.Select(item => new WorkloadId(item.ItemSpec)));
7882
}
7983

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.NET.TestFramework.Commands;
5+
6+
public class DotnetWorkloadCommand : DotnetCommand
7+
{
8+
public DotnetWorkloadCommand(ITestOutputHelper log, params string[] args) : base(log)
9+
{
10+
Arguments.Add("workload");
11+
Arguments.AddRange(args);
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.Docker.Sdk">
2+
<PropertyGroup Label="Globals">
3+
<ProjectVersion>2.1</ProjectVersion>
4+
<DockerTargetOS>Windows</DockerTargetOS>
5+
<ProjectGuid>154022c1-8014-4e9d-bd78-6ff46670ffa4</ProjectGuid>
6+
<DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
7+
<DockerServiceUrl>{Scheme}://{ServiceIPAddress}{ServicePort}</DockerServiceUrl>
8+
<DockerServiceName>webapplication1</DockerServiceName>
9+
<DockerComposeBaseFilePath>DockerComposeFiles\mydockercompose</DockerComposeBaseFilePath>
10+
<AdditionalComposeFilePaths>AdditionalComposeFiles\myadditionalcompose.yml</AdditionalComposeFilePaths>
11+
<ProjectTypeGuids>E53339B2-1760-4266-BCC7-CA923CBCF16C</ProjectTypeGuids>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<None Include="DockerComposeFiles\mydockercompose.override.yml">
15+
<DependentUpon>DockerComposeFiles\mydockercompose.yml</DependentUpon>
16+
</None>
17+
<None Include="DockerComposeFiles\mydockercompose.yml" />
18+
<None Include=".dockerignore" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleApp", "SampleApp\SampleApp.csproj", "{99463187-C4F6-4A68-82AD-2BBB08DBF872}"
7+
EndProject
8+
Project("E53339B2-1760-4266-BCC7-CA923CBCF16C") = "SampleApp.Docker", "SampleApp.Docker\SampleApp.Docker.dcproj", "{154022C1-8014-4E9D-BD78-6FF46670FFA4}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{99463187-C4F6-4A68-82AD-2BBB08DBF872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{99463187-C4F6-4A68-82AD-2BBB08DBF872}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{99463187-C4F6-4A68-82AD-2BBB08DBF872}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{99463187-C4F6-4A68-82AD-2BBB08DBF872}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{154022C1-8014-4E9D-BD78-6FF46670FFA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{154022C1-8014-4E9D-BD78-6FF46670FFA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{154022C1-8014-4E9D-BD78-6FF46670FFA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{154022C1-8014-4E9D-BD78-6FF46670FFA4}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.DotNet.Cli.Workload.Restore.Tests;
5+
6+
public class GivenDotnetWorkloadRestore : SdkTest
7+
{
8+
public GivenDotnetWorkloadRestore(ITestOutputHelper log) : base(log)
9+
{
10+
}
11+
12+
public static string DcProjAssetName = "SolutionWithAppAndDcProj";
13+
14+
[Fact]
15+
public void ProjectsThatDoNotSupportWorkloadsAreNotInspected()
16+
{
17+
var projectPath =
18+
_testAssetsManager
19+
.CopyTestAsset(DcProjAssetName)
20+
.WithSource()
21+
.Path;
22+
23+
new DotnetWorkloadCommand(Log, "restore")
24+
.WithWorkingDirectory(projectPath)
25+
.Execute()
26+
.Should()
27+
// if we did try to restore the dcproj in this TestAsset we would fail, so passing means we didn't!
28+
.Pass();
29+
}
30+
}

0 commit comments

Comments
 (0)