Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Basic.CompilerLog.Util" Version="0.9.9" />
<PackageVersion Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageVersion Include="FluentAssertions.Json" Version="$(FluentAssertionsJsonVersion)" />
<PackageVersion Include="Microsoft.ApplicationInsights" Version="$(MicrosoftApplicationInsightsPackageVersion)" />
Expand Down Expand Up @@ -75,6 +76,7 @@
<PackageVersion Include="Microsoft.Web.Xdt" Version="$(MicrosoftWebXdtPackageVersion)" />
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="$(MicrosoftWin32SystemEventsPackageVersion)" />
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="$(MicrosoftWindowsCsWin32PackageVersion)" />
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.2.386" />
<PackageVersion Include="Moq" Version="$(MoqPackageVersion)" />
<PackageVersion Include="NETStandard.Library.NETFramework" Version="$(NETStandardLibraryNETFrameworkVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
Expand Down
2 changes: 2 additions & 0 deletions sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
build.sh = build.sh
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
LICENSE.TXT = LICENSE.TXT
NuGet.config = NuGet.config
README.md = README.md
restore.cmd = restore.cmd
restore.sh = restore.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,22 @@ Copyright (c) .NET Foundation. All rights reserved.
<IsNetCoreAppTargetingLatestTFM Condition="'$(IsNetCoreAppTargetingLatestTFM)' == '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(_TargetFrameworkVersionWithoutV)' == '$(NETCoreAppMaximumVersion)'">true</IsNetCoreAppTargetingLatestTFM>
</PropertyGroup>

<!--
Use Roslyn deployed with SDK for builds of SDK-style projects (regardless of whether the initiator is `dotnet` or `msbuild`).
See https://github.com/dotnet/sdk/blob/main/documentation/general/decouple-vs-and-net-sdk.md.
-->
<PropertyGroup>
<RoslynUseSdkCompiler Condition="'$(RoslynUseSdkCompiler)' == '' and
'$(RoslynUseMSBuildCompiler)' != 'true' and
'$(BuildWithNetFrameworkHostedCompiler)' != 'true' and
('$(DOTNET_HOST_PATH)' != '' or '$(DOTNET_EXPERIMENTAL_HOST_PATH)' != '')">true</RoslynUseSdkCompiler>
</PropertyGroup>
<PropertyGroup Condition="'$(RoslynUseSdkCompiler)' == 'true'">
<RoslynTargetsPath Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\..\..\Roslyn</RoslynTargetsPath>
<RoslynTargetsPath Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)..\..\..\Roslyn\binfx</RoslynTargetsPath>
<RoslynToolsDirectory Condition="'$(MSBuildRuntimeType)' != 'Core'">..\bincore</RoslynToolsDirectory>
<CSharpCoreTargetsPath>$(MSBuildThisFileDirectory)..\..\..\Roslyn\Microsoft.CSharp.Core.targets</CSharpCoreTargetsPath>
<VisualBasicCoreTargetsPath>$(MSBuildThisFileDirectory)..\..\..\Roslyn\Microsoft.VisualBasic.Core.targets</VisualBasicCoreTargetsPath>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Basic.CompilerLog.Util" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.Build" />
<PackageReference Include="MSBuild.StructuredLogger" />
<PackageReference Include="NuGet.ProjectModel" />
<PackageReference Include="NuGet.LibraryModel" />
<PackageReference Include="NuGet.Versioning" />
<PackageReference Include="NuGet.Configuration" />
<PackageReference Include="NuGet.Frameworks" />
<PackageReference Include="NuGet.Common" />
<PackageReference Include="Xunit.Combinatorial" />
</ItemGroup>

<ItemGroup>
Expand Down
128 changes: 128 additions & 0 deletions test/Microsoft.NET.Build.Tests/RoslynBuildTaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using Basic.CompilerLog.Util;
using Microsoft.Build.Logging.StructuredLogger;

namespace Microsoft.NET.Build.Tests;

public sealed class RoslynBuildTaskTests(ITestOutputHelper log) : SdkTest(log)
{
private const string CoreCompilerFileName = "csc.dll";
private const string FxCompilerFileName = "csc.exe";

[FullMSBuildOnlyTheory, CombinatorialData]
public void FullMSBuild_SdkStyle(bool useSharedCompilation)
{
var testAsset = CreateProject(useSharedCompilation);
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
VerifyCompiler(buildCommand, CoreCompilerFileName, useSharedCompilation);
}

[FullMSBuildOnlyTheory, CombinatorialData]
public void FullMSBuild_SdkStyle_OptOut(bool useSharedCompilation)
{
var testAsset = CreateProject(useSharedCompilation).WithProjectChanges(static doc =>
{
doc.Root!.Element("PropertyGroup")!.Add(new XElement("RoslynUseSdkCompiler", "false"));
});
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
VerifyCompiler(buildCommand, FxCompilerFileName, useSharedCompilation);
}

[FullMSBuildOnlyTheory, CombinatorialData]
public void FullMSBuild_NonSdkStyle(bool useSharedCompilation)
{
var testAsset = CreateProject(useSharedCompilation, static project =>
{
project.IsSdkProject = false;
project.TargetFrameworkVersion = "v4.7.2";
});
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
VerifyCompiler(buildCommand, FxCompilerFileName, useSharedCompilation);
}

[Theory, CombinatorialData]
public void DotNet(bool useSharedCompilation)
{
var testAsset = CreateProject(useSharedCompilation);
var buildCommand = BuildAndRunUsingDotNet(testAsset);
VerifyCompiler(buildCommand, CoreCompilerFileName, useSharedCompilation);
}

private TestAsset CreateProject(bool useSharedCompilation, Action<TestProject>? configure = null, [CallerMemberName] string callingMethod = "")
{
var project = new TestProject
{
Name = "App1",
IsExe = true,
SourceFiles =
{
["Program.cs"] = """
class Program
{
static void Main()
{
System.Console.WriteLine(40 + 2);
}
}
""",
},
};

// UseSharedCompilation should be the default, so set it only if false.
if (!useSharedCompilation)
{
project.AdditionalProperties["UseSharedCompilation"] = "false";
}
Comment on lines +74 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be simpler to just always be explicit here:

Suggested change
// UseSharedCompilation should be the default, so set it only if false.
if (!useSharedCompilation)
{
project.AdditionalProperties["UseSharedCompilation"] = "false";
}
project.AdditionalProperties["UseSharedCompilation"] = useSharedCompilation ? "true" : "false";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which you can probably fold into the initializer above actually

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intention is to not set UseSharedCompilation to true, so the tests also verify that true is the default value


configure?.Invoke(project);
return _testAssetsManager.CreateTestProject(project, callingMethod: callingMethod);
}

private TestCommand BuildAndRunUsingMSBuild(TestAsset testAsset)
{
var buildCommand = new MSBuildCommand(testAsset, "Build");
buildCommand.WithWorkingDirectory(testAsset.Path)
.Execute("-bl").Should().Pass();

Run(buildCommand.GetOutputFile());

return buildCommand;
}

private TestCommand BuildAndRunUsingDotNet(TestAsset testAsset)
{
var buildCommand = new DotnetBuildCommand(testAsset);
buildCommand.Execute("-bl").Should().Pass();

Run(buildCommand.GetOutputFile());

return buildCommand;
}

private void Run(FileInfo outputFile)
{
var runCommand = new RunExeCommand(Log, outputFile.FullName);
runCommand.Execute().Should().Pass()
.And.HaveStdOut("42");
}

private static void VerifyCompiler(TestCommand buildCommand, string compilerFileName, bool usedCompilerServer)
{
var binaryLogPath = Path.Join(buildCommand.WorkingDirectory, "msbuild.binlog");
using (var reader = BinaryLogReader.Create(binaryLogPath))
{
var call = reader.ReadAllCompilerCalls().Should().ContainSingle().Subject;
Path.GetFileName(call.CompilerFilePath).Should().Be(compilerFileName);
}

// Verify compiler server message.
var compilerServerMesssages = BinaryLog.ReadBuild(binaryLogPath).FindChildrenRecursive<Message>(
static message => message.Text.StartsWith("CompilerServer:", StringComparison.Ordinal));
compilerServerMesssages.Should().ContainSingle().Which.Text.Should().StartWith(usedCompilerServer
? "CompilerServer: server - server processed compilation - "
: "CompilerServer: tool - using command line tool by design");
}
}
21 changes: 21 additions & 0 deletions test/Microsoft.NET.TestFramework/Commands/DotnetBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.NET.TestFramework.Commands
{
public class DotnetBuildCommand : DotnetCommand
{
public TestAsset? TestAsset { get; }

public DotnetBuildCommand(ITestOutputHelper log, params string[] args) : base(log)
{
Arguments.Add("build");
Expand All @@ -13,6 +17,8 @@ public DotnetBuildCommand(ITestOutputHelper log, params string[] args) : base(lo

public DotnetBuildCommand(TestAsset testAsset, params string[] args) : this(testAsset.Log, args)
{
TestAsset = testAsset;

if (testAsset.TestProject != null && testAsset.TestProject.Name is not null)
{
WorkingDirectory = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name);
Expand All @@ -22,5 +28,20 @@ public DotnetBuildCommand(TestAsset testAsset, params string[] args) : this(test
WorkingDirectory = testAsset.TestRoot;
}
}

public DirectoryInfo GetOutputDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null, string? platform = null)
{
Debug.Assert(TestAsset?.TestProject?.Name != null);
var projectPath = Path.Combine(TestAsset.Path, TestAsset.TestProject.Name);
return new DirectoryInfo(OutputPathCalculator.FromProject(projectPath, TestAsset)
.GetOutputDirectory(targetFramework, configuration, runtimeIdentifier, platform));
}

public FileInfo GetOutputFile()
{
Debug.Assert(TestAsset?.TestProject?.Name != null);
var extension = TestAsset.TestProject.IsExe ? ".exe" : ".dll";
return GetOutputDirectory().File(TestAsset.TestProject.Name + extension);
}
}
}
8 changes: 8 additions & 0 deletions test/Microsoft.NET.TestFramework/Commands/MSBuildCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.DotNet.Cli.Utils;

namespace Microsoft.NET.TestFramework.Commands
Expand Down Expand Up @@ -84,6 +85,13 @@ public virtual DirectoryInfo GetOutputDirectory(string? targetFramework = null,
return new DirectoryInfo(output);
}

public FileInfo GetOutputFile()
{
Debug.Assert(TestAsset?.TestProject?.Name != null);
var extension = TestAsset.TestProject.IsExe ? ".exe" : ".dll";
return GetOutputDirectory().File(TestAsset.TestProject.Name + extension);
}

public virtual DirectoryInfo GetIntermediateDirectory(string? targetFramework = null, string configuration = "Debug", string? runtimeIdentifier = null)
{
if (TestAsset != null)
Expand Down
Loading