Skip to content

Commit ec6c62f

Browse files
committed
Verify compiler target framework
1 parent 28447ce commit ec6c62f

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

test/Microsoft.NET.Build.Tests/RoslynBuildTaskTests.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Reflection;
55
using System.Runtime.CompilerServices;
6+
using System.Runtime.Versioning;
67
using Basic.CompilerLog.Util;
78
using Microsoft.Build.Logging.StructuredLogger;
89
using Microsoft.CodeAnalysis;
@@ -12,6 +13,9 @@ namespace Microsoft.NET.Build.Tests;
1213

1314
public sealed class RoslynBuildTaskTests(ITestOutputHelper log) : SdkTest(log)
1415
{
16+
private const string CoreTargetFrameworkName = ".NETCoreApp";
17+
private const string FxTargetFrameworkName = ".NETFramework";
18+
1519
private static string CompilerFileNameWithoutExtension(Language language) => language switch
1620
{
1721
Language.CSharp => "csc",
@@ -28,7 +32,7 @@ public void FullMSBuild_SdkStyle(bool useSharedCompilation, Language language)
2832
{
2933
var testAsset = CreateProject(useSharedCompilation, language);
3034
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
31-
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), useSharedCompilation);
35+
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), CoreTargetFrameworkName, useSharedCompilation);
3236
}
3337

3438
[FullMSBuildOnlyTheory, CombinatorialData]
@@ -39,7 +43,7 @@ public void FullMSBuild_SdkStyle_OptOut(bool useSharedCompilation, Language lang
3943
doc.Root!.Element("PropertyGroup")!.Add(new XElement("RoslynCompilerType", "Framework"));
4044
});
4145
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
42-
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), useSharedCompilation);
46+
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), FxTargetFrameworkName, useSharedCompilation);
4347
}
4448

4549
[FullMSBuildOnlyTheory, CombinatorialData]
@@ -51,7 +55,7 @@ public void FullMSBuild_NonSdkStyle(bool useSharedCompilation, Language language
5155
project.TargetFrameworkVersion = "v4.7.2";
5256
});
5357
var buildCommand = BuildAndRunUsingMSBuild(testAsset);
54-
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), useSharedCompilation);
58+
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), FxTargetFrameworkName, useSharedCompilation);
5559
}
5660

5761
[FullMSBuildOnlyTheory, CombinatorialData]
@@ -60,15 +64,18 @@ public void FullMSBuild_SdkStyle_ToolsetPackage(bool useSharedCompilation, Langu
6064
var testAsset = CreateProject(useSharedCompilation, language, AddCompilersToolsetPackage);
6165
ReadOnlySpan<string> args = useFrameworkCompiler ? ["-p:RoslynCompilerType=Framework"] : [];
6266
var buildCommand = BuildAndRunUsingMSBuild(testAsset, args);
63-
VerifyCompiler(buildCommand, DotNetExecCompilerFileName(language), useSharedCompilation, toolsetPackage: true);
67+
VerifyCompiler(buildCommand,
68+
DotNetExecCompilerFileName(language),
69+
useFrameworkCompiler ? FxTargetFrameworkName : CoreTargetFrameworkName,
70+
useSharedCompilation, toolsetPackage: true);
6471
}
6572

6673
[Theory, CombinatorialData]
6774
public void DotNet(bool useSharedCompilation, Language language)
6875
{
6976
var testAsset = CreateProject(useSharedCompilation, language);
7077
var buildCommand = BuildAndRunUsingDotNet(testAsset);
71-
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), useSharedCompilation);
78+
VerifyCompiler(buildCommand, AppHostCompilerFileName(language), CoreTargetFrameworkName, useSharedCompilation);
7279
}
7380

7481
// https://github.com/dotnet/sdk/issues/49665
@@ -77,7 +84,7 @@ public void DotNet_ToolsetPackage(bool useSharedCompilation, Language language)
7784
{
7885
var testAsset = CreateProject(useSharedCompilation, language, AddCompilersToolsetPackage);
7986
var buildCommand = BuildAndRunUsingDotNet(testAsset);
80-
VerifyCompiler(buildCommand, DotNetExecCompilerFileName(language), useSharedCompilation, toolsetPackage: true);
87+
VerifyCompiler(buildCommand, DotNetExecCompilerFileName(language), CoreTargetFrameworkName, useSharedCompilation, toolsetPackage: true);
8188
}
8289

8390
private TestAsset CreateProject(bool useSharedCompilation, Language language, Action<TestProject>? configure = null, [CallerMemberName] string callingMethod = "")
@@ -159,7 +166,7 @@ private void Run(FileInfo outputFile)
159166
.And.HaveStdOut("42");
160167
}
161168

162-
private static void VerifyCompiler(TestCommand buildCommand, string compilerFileName, bool usedCompilerServer, bool toolsetPackage = false)
169+
private static void VerifyCompiler(TestCommand buildCommand, string compilerFileName, string targetFrameworkName, bool usedCompilerServer, bool toolsetPackage = false)
163170
{
164171
var binaryLogPath = Path.Join(buildCommand.WorkingDirectory, "msbuild.binlog");
165172
using (var reader = BinaryLogReader.Create(binaryLogPath))
@@ -176,6 +183,8 @@ private static void VerifyCompiler(TestCommand buildCommand, string compilerFile
176183
{
177184
call.CompilerFilePath.Should().NotContain(toolsetPackageName);
178185
}
186+
187+
GetTargetFramework(call.CompilerFilePath).Should().StartWith($"{targetFrameworkName},");
179188
}
180189

181190
// Verify compiler server message.
@@ -186,6 +195,19 @@ private static void VerifyCompiler(TestCommand buildCommand, string compilerFile
186195
: "CompilerServer: tool - using command line tool by design");
187196
}
188197

198+
private static string? GetTargetFramework(string dllPath)
199+
{
200+
var tpa = ((string?)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"))?.Split(Path.PathSeparator) ?? [];
201+
var resolver = new PathAssemblyResolver([.. tpa, dllPath]);
202+
using var mlc = new MetadataLoadContext(resolver);
203+
var asm = mlc.LoadFromAssemblyPath(dllPath);
204+
var attrFullName = typeof(TargetFrameworkAttribute).FullName;
205+
return asm.GetCustomAttributesData()
206+
.Where(a => a.AttributeType.FullName == attrFullName)
207+
.Select(a => a.ConstructorArguments[0].Value)
208+
.FirstOrDefault() as string;
209+
}
210+
189211
public enum Language
190212
{
191213
CSharp,

0 commit comments

Comments
 (0)