Skip to content

Commit 444d354

Browse files
authored
Merge pull request #4756 from devlead/feature/gh-4456
GH4456: DotNetMSBuild Use MSBuild /verbosity not dotnet --verbosity
2 parents 11fda00 + 83396f7 commit 444d354

4 files changed

Lines changed: 56 additions & 7 deletions

File tree

src/Cake.Common.Tests/Unit/Tools/DotNet/MSBuild/DotNetMSBuildBuilderTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -911,6 +911,26 @@ public void Should_Add_Host_Arguments()
911911
Assert.Equal("--diagnostics msbuild", result.Args);
912912
}
913913

914+
[Theory]
915+
[InlineData(DotNetVerbosity.Quiet, "/verbosity:quiet")]
916+
[InlineData(DotNetVerbosity.Minimal, "/verbosity:minimal")]
917+
[InlineData(DotNetVerbosity.Normal, "/verbosity:normal")]
918+
[InlineData(DotNetVerbosity.Detailed, "/verbosity:detailed")]
919+
[InlineData(DotNetVerbosity.Diagnostic, "/verbosity:diagnostic")]
920+
public void Should_Use_MSBuild_Verbosity_Not_DotNet_Verbosity_When_Verbosity_Is_Specified(DotNetVerbosity verbosity, string expectedMsBuildVerbosity)
921+
{
922+
// Given: DotNetMSBuildSettings.Verbosity (not ConsoleLoggerSettings) causes MSB1016 if passed as dotnet --verbosity
923+
var fixture = new DotNetMSBuildBuilderFixture();
924+
fixture.Settings.Verbosity = verbosity;
925+
926+
// When
927+
var result = fixture.Run();
928+
929+
// Then: must use MSBuild /verbosity switch, not dotnet --verbosity (see https://github.com/cake-build/cake/issues/4456)
930+
Assert.Contains(expectedMsBuildVerbosity, result.Args);
931+
Assert.DoesNotContain("--verbosity", result.Args);
932+
}
933+
914934
[Fact]
915935
public void Should_Use_Node_Reuse_If_Specified()
916936
{

src/Cake.Common/Tools/DotNet/MSBuild/DotNetMSBuildBuilder.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -13,6 +13,10 @@ namespace Cake.Common.Tools.DotNet.MSBuild
1313
/// <summary>
1414
/// .NET Core project builder.
1515
/// </summary>
16+
/// <remarks>
17+
/// Verbosity is passed as MSBuild /verbosity (not dotnet --verbosity) because dotnet msbuild forwards
18+
/// arguments to MSBuild, which does not accept the dotnet CLI --verbosity form and fails with MSB1016.
19+
/// </remarks>
1620
public sealed class DotNetMSBuildBuilder : DotNetTool<DotNetMSBuildSettings>
1721
{
1822
private readonly ICakeEnvironment _environment;
@@ -39,13 +43,18 @@ public DotNetMSBuildBuilder(
3943
/// <param name="projectOrDirectory">The target project path.</param>
4044
/// <param name="settings">The settings.</param>
4145
/// <param name="standardOutputAction">The action to invoke with the standard output.</param>
46+
/// <remarks>
47+
/// Calls Run directly so that dotnet --verbosity is not appended (it would cause MSB1016);
48+
/// verbosity is passed as MSBuild /verbosity in the arguments instead.
49+
/// </remarks>
4250
public void Build(string projectOrDirectory, DotNetMSBuildSettings settings, Action<IEnumerable<string>> standardOutputAction)
4351
{
4452
ArgumentNullException.ThrowIfNull(settings);
4553

46-
RunCommand(
54+
var arguments = GetArguments(projectOrDirectory, settings);
55+
Run(
4756
settings,
48-
GetArguments(projectOrDirectory, settings),
57+
arguments,
4958
standardOutputAction == null ? null : new ProcessSettings { RedirectStandardOutput = true },
5059
standardOutputAction == null ? null : new Action<IProcess>(process => standardOutputAction(process.GetStandardOutput())));
5160
}

src/Cake.Common/Tools/DotNet/MSBuild/MSBuildArgumentBuilderExtensions.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -41,6 +41,12 @@ public static void AppendMSBuildSettings(this ProcessArgumentBuilder builder, Do
4141

4242
var msBuilder = new ProcessArgumentBuilder();
4343

44+
// Verbosity: use MSBuild /verbosity switch (dotnet msbuild does not support --verbosity; it forwards to MSBuild which expects /verbosity:x)
45+
if (settings.Verbosity.HasValue)
46+
{
47+
msBuilder.AppendMSBuildSwitch("verbosity", GetMSBuildVerbosityValue(settings.Verbosity.Value));
48+
}
49+
4450
// Got any targets?
4551
if (settings.Targets.Any())
4652
{
@@ -288,6 +294,20 @@ private static string GetLoggerArgument(int index, MSBuildFileLoggerSettings log
288294
return $"/fileLogger{counter} /fileloggerparameters{counter}:{parameters}";
289295
}
290296

297+
/// <summary>
298+
/// Maps <see cref="DotNet.DotNetVerbosity"/> to MSBuild verbosity string (quiet, minimal, normal, detailed, diagnostic).
299+
/// </summary>
300+
private static string GetMSBuildVerbosityValue(DotNet.DotNetVerbosity verbosity)
301+
=> verbosity switch
302+
{
303+
DotNet.DotNetVerbosity.Quiet => "quiet",
304+
DotNet.DotNetVerbosity.Minimal => "minimal",
305+
DotNet.DotNetVerbosity.Normal => "normal",
306+
DotNet.DotNetVerbosity.Detailed => "detailed",
307+
DotNet.DotNetVerbosity.Diagnostic => "diagnostic",
308+
_ => throw new ArgumentOutOfRangeException(nameof(verbosity), verbosity, "Invalid value"),
309+
};
310+
291311
private static string GetToolVersionValue(MSBuildVersion toolVersion)
292312
{
293313
switch (toolVersion)

tests/integration/Cake.Common/Tools/DotNet/DotNetAliases.cake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ Task("Cake.Common.Tools.DotNet.DotNetAliases.DotNetMSBuild")
216216
var project = path.CombineWithFilePath("hwapp/hwapp.csproj");
217217
var assembly = path.CombineWithFilePath("hwapp/bin/Debug/net10.0/hwapp.dll");
218218

219-
// When
220-
DotNetMSBuild(project.FullPath);
219+
// When (Verbosity.Quiet exercises MSBuild /verbosity, not dotnet --verbosity; see https://github.com/cake-build/cake/issues/4456)
220+
DotNetMSBuild(project.FullPath, new DotNetMSBuildSettings { Verbosity = DotNetVerbosity.Quiet });
221221

222222
// Then
223223
Assert.True(System.IO.File.Exists(assembly.FullPath));

0 commit comments

Comments
 (0)