33
44using System . Reflection ;
55using System . Runtime . CompilerServices ;
6+ using System . Runtime . Versioning ;
67using Basic . CompilerLog . Util ;
78using Microsoft . Build . Logging . StructuredLogger ;
89using Microsoft . CodeAnalysis ;
@@ -11,6 +12,9 @@ namespace Microsoft.NET.Build.Tests;
1112
1213public sealed class RoslynBuildTaskTests ( ITestOutputHelper log ) : SdkTest ( log )
1314{
15+ private const string CoreTargetFrameworkName = ".NETCoreApp" ;
16+ private const string FxTargetFrameworkName = ".NETFramework" ;
17+
1418 private static string CompilerFileNameWithoutExtension ( Language language ) => language switch
1519 {
1620 Language . CSharp => "csc" ,
@@ -27,7 +31,7 @@ public void FullMSBuild_SdkStyle(bool useSharedCompilation, Language language)
2731 {
2832 var testAsset = CreateProject ( useSharedCompilation , language ) ;
2933 var buildCommand = BuildAndRunUsingMSBuild ( testAsset ) ;
30- VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , useSharedCompilation ) ;
34+ VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , CoreTargetFrameworkName , useSharedCompilation ) ;
3135 }
3236
3337 [ FullMSBuildOnlyTheory , CombinatorialData ]
@@ -38,7 +42,7 @@ public void FullMSBuild_SdkStyle_OptOut(bool useSharedCompilation, Language lang
3842 doc . Root ! . Element ( "PropertyGroup" ) ! . Add ( new XElement ( "RoslynCompilerType" , "Framework" ) ) ;
3943 } ) ;
4044 var buildCommand = BuildAndRunUsingMSBuild ( testAsset ) ;
41- VerifyCompiler ( buildCommand , FxCompilerFileName ( language ) , useSharedCompilation ) ;
45+ VerifyCompiler ( buildCommand , FxCompilerFileName ( language ) , FxTargetFrameworkName , useSharedCompilation ) ;
4246 }
4347
4448 [ FullMSBuildOnlyTheory , CombinatorialData ]
@@ -50,7 +54,7 @@ public void FullMSBuild_NonSdkStyle(bool useSharedCompilation, Language language
5054 project . TargetFrameworkVersion = "v4.7.2" ;
5155 } ) ;
5256 var buildCommand = BuildAndRunUsingMSBuild ( testAsset ) ;
53- VerifyCompiler ( buildCommand , FxCompilerFileName ( language ) , useSharedCompilation ) ;
57+ VerifyCompiler ( buildCommand , FxCompilerFileName ( language ) , FxTargetFrameworkName , useSharedCompilation ) ;
5458 }
5559
5660 [ FullMSBuildOnlyTheory , CombinatorialData ]
@@ -59,15 +63,18 @@ public void FullMSBuild_SdkStyle_ToolsetPackage(bool useSharedCompilation, Langu
5963 var testAsset = CreateProject ( useSharedCompilation , language , AddCompilersToolsetPackage ) ;
6064 ReadOnlySpan < string > args = useFrameworkCompiler ? [ "-p:RoslynCompilerType=Framework" ] : [ ] ;
6165 var buildCommand = BuildAndRunUsingMSBuild ( testAsset , args ) ;
62- VerifyCompiler ( buildCommand , useFrameworkCompiler ? FxCompilerFileName ( language ) : CoreCompilerFileName ( language ) , useSharedCompilation , toolsetPackage : true ) ;
66+ VerifyCompiler ( buildCommand ,
67+ useFrameworkCompiler ? FxCompilerFileName ( language ) : CoreCompilerFileName ( language ) ,
68+ useFrameworkCompiler ? FxTargetFrameworkName : CoreTargetFrameworkName ,
69+ useSharedCompilation , toolsetPackage : true ) ;
6370 }
6471
6572 [ Theory , CombinatorialData ]
6673 public void DotNet ( bool useSharedCompilation , Language language )
6774 {
6875 var testAsset = CreateProject ( useSharedCompilation , language ) ;
6976 var buildCommand = BuildAndRunUsingDotNet ( testAsset ) ;
70- VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , useSharedCompilation ) ;
77+ VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , CoreTargetFrameworkName , useSharedCompilation ) ;
7178 }
7279
7380 // https://github.com/dotnet/sdk/issues/49665
@@ -76,7 +83,7 @@ public void DotNet_ToolsetPackage(bool useSharedCompilation, Language language)
7683 {
7784 var testAsset = CreateProject ( useSharedCompilation , language , AddCompilersToolsetPackage ) ;
7885 var buildCommand = BuildAndRunUsingDotNet ( testAsset ) ;
79- VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , useSharedCompilation , toolsetPackage : true ) ;
86+ VerifyCompiler ( buildCommand , CoreCompilerFileName ( language ) , CoreTargetFrameworkName , useSharedCompilation , toolsetPackage : true ) ;
8087 }
8188
8289 private TestAsset CreateProject ( bool useSharedCompilation , Language language , Action < TestProject > ? configure = null , [ CallerMemberName ] string callingMethod = "" )
@@ -158,7 +165,7 @@ private void Run(FileInfo outputFile)
158165 . And . HaveStdOut ( "42" ) ;
159166 }
160167
161- private static void VerifyCompiler ( TestCommand buildCommand , string compilerFileName , bool usedCompilerServer , bool toolsetPackage = false )
168+ private static void VerifyCompiler ( TestCommand buildCommand , string compilerFileName , string targetFrameworkName , bool usedCompilerServer , bool toolsetPackage = false )
162169 {
163170 var binaryLogPath = Path . Join ( buildCommand . WorkingDirectory , "msbuild.binlog" ) ;
164171 using ( var reader = BinaryLogReader . Create ( binaryLogPath ) )
@@ -175,6 +182,8 @@ private static void VerifyCompiler(TestCommand buildCommand, string compilerFile
175182 {
176183 call . CompilerFilePath . Should ( ) . NotContain ( toolsetPackageName ) ;
177184 }
185+
186+ GetTargetFramework ( call . CompilerFilePath ) . Should ( ) . StartWith ( $ "{ targetFrameworkName } ,") ;
178187 }
179188
180189 // Verify compiler server message.
@@ -185,6 +194,19 @@ private static void VerifyCompiler(TestCommand buildCommand, string compilerFile
185194 : "CompilerServer: tool - using command line tool by design" ) ;
186195 }
187196
197+ private static string ? GetTargetFramework ( string dllPath )
198+ {
199+ var tpa = ( ( string ? ) AppContext . GetData ( "TRUSTED_PLATFORM_ASSEMBLIES" ) ) ? . Split ( Path . PathSeparator ) ?? [ ] ;
200+ var resolver = new PathAssemblyResolver ( [ .. tpa , dllPath ] ) ;
201+ using var mlc = new MetadataLoadContext ( resolver ) ;
202+ var asm = mlc . LoadFromAssemblyPath ( dllPath ) ;
203+ var attrFullName = typeof ( TargetFrameworkAttribute ) . FullName ;
204+ return asm . GetCustomAttributesData ( )
205+ . Where ( a => a . AttributeType . FullName == attrFullName )
206+ . Select ( a => a . ConstructorArguments [ 0 ] . Value )
207+ . FirstOrDefault ( ) as string ;
208+ }
209+
188210 public enum Language
189211 {
190212 CSharp ,
0 commit comments