diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets
index 4550d18f5062..6f5100260835 100644
--- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets
+++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets
@@ -236,10 +236,25 @@ Copyright (c) .NET Foundation. All rights reserved.
+
+
+
+ <_DefineConstantsWithoutTrace Include="$(DefineConstants)" />
+ <_DefineConstantsWithoutTrace Remove="TRACE"/>
+
+
+
+ @(_DefineConstantsWithoutTrace)
+
+
+
$(DefineConstants);@(_ImplicitDefineConstant)
diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs
index 2f6d197eca6f..5ed91b6038e1 100644
--- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs
+++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs
@@ -1028,5 +1028,49 @@ public void It_warns_on_nonportable_rids(string targetFramework, string[] rids,
result.Should().NotHaveStdOutContaining("NETSDK1206");
}
}
+
+ [Theory]
+ [InlineData(true, "TRACE DISABLED")]
+ [InlineData(false, "TRACE ENABLED")]
+ public void It_can_use_implicitly_defined_compilation_constants(bool disableTracing, string expectedOutput)
+ {
+ var testProj = new TestProject()
+ {
+ Name = "DisableTracing_" + disableTracing.ToString(),
+ TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
+ IsExe = true,
+ };
+ if (disableTracing == true)
+ {
+ testProj.AdditionalProperties["DisableDiagnosticTracing"] = "true";
+ }
+
+ testProj.SourceFiles[$"{testProj.Name}.cs"] = @"
+using System;
+class Program
+{
+ static void Main(string[] args)
+ {
+ #if TRACE
+ Console.WriteLine(""TRACE ENABLED"");
+ #endif
+ #if !TRACE
+ Console.WriteLine(""TRACE DISABLED"");
+ #endif
+ }
+}";
+ var testAsset = _testAssetsManager.CreateTestProject(testProj, identifier: disableTracing.ToString());
+
+ var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name));
+ buildCommand
+ .Execute()
+ .Should()
+ .Pass();
+
+ var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, $"{testProj.Name}{EnvironmentInfo.ExecutableExtension}"));
+ runCommand
+ .Execute()
+ .Should().HaveStdOut(expectedOutput);
+ }
}
}