@@ -382,16 +382,16 @@ type internal FxResolver
382382
383383 let tryGetNetCoreRefsPackDirectoryRoot () = tryNetCoreRefsPackDirectoryRoot.Force()
384384
385+ let getTfmNumber ( v : string ) =
386+ let arr = v.Split([| '.' |], 3 )
387+ arr[ 0 ] + " ." + arr[ 1 ]
388+
385389 // Tries to figure out the tfm for the compiler instance.
386390 // On coreclr it uses the deps.json file
387391 //
388392 // On-demand because (a) some FxResolver are ephemeral (b) we want to avoid recomputation
389- let tryGetRunningTfm =
393+ let tryGetRunningTfm () =
390394 let runningTfmOpt =
391- let getTfmNumber ( v : string ) =
392- let arr = v.Split([| '.' |], 3 )
393- arr[ 0 ] + " ." + arr[ 1 ]
394-
395395 // Compute TFM from System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
396396 // System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;;
397397 // val it: string = ".NET 6.0.7"
@@ -548,6 +548,24 @@ type internal FxResolver
548548 assemblyReferences |> List.iter traverseDependencies
549549 assemblies
550550
551+ let tryGetTfmFromSdkDir ( sdkDir : string ) =
552+ let dotnetConfigFile = Path.Combine( sdkDir, " dotnet.runtimeconfig.json" )
553+
554+ try
555+ use stream = FileSystem.OpenFileForReadShim( dotnetConfigFile)
556+ let dotnetConfig = stream.ReadAllText()
557+ let pattern = " \" tfm\" : \" "
558+
559+ let startPos =
560+ dotnetConfig.IndexOf( pattern, StringComparison.OrdinalIgnoreCase)
561+ + pattern.Length
562+
563+ let endPos = dotnetConfig.IndexOf( " \" " , startPos)
564+ let tfm = dotnetConfig[ startPos .. endPos - 1 ]
565+ tfm
566+ with _ ->
567+ tryGetRunningTfm ()
568+
551569 // This list is the default set of references for "non-project" files.
552570 //
553571 // These DLLs are
@@ -799,12 +817,37 @@ type internal FxResolver
799817 RequireFxResolverLock( fxtok, " assuming all member require lock" )
800818 tryGetSdkDir () |> replayWarnings)
801819
802- /// Gets the selected target framework moniker, e.g netcore3.0, net472, and the running rid of the current machine
820+ /// Gets
821+ /// 1. The Target Framework Moniker (TFM) used for scripting (e.g netcore3.0, net472)
822+ /// 2. The running RID of the current machine (e.g. win-x64)
823+ ///
824+ /// When analyzing scripts for editing, this is **not** necessarily the running TFM. Rather, it is the TFM to use for analysing
825+ /// a script.
826+ ///
827+ /// Summary:
828+ /// - When running scripts (isInteractive = true) this is identical to the running TFM.
829+ ///
830+ /// - When analyzing .NET Core scripts (isInteractive = false, tryGetSdkDir is Some),
831+ /// the scripting TFM is determined from dotnet.runtimeconfig.json in the SDK directory
832+ ///
833+ /// - Otherwise, the running TFM is used. That is, if editing with .NET Framework/Core-based tooling a script is assumed
834+ /// to be .NET Framework/Core respectively.
835+ ///
836+ /// The RID returned is always the RID of the running machine.
803837 member _.GetTfmAndRid () =
804838 fxlock.AcquireLock( fun fxtok ->
805839 RequireFxResolverLock( fxtok, " assuming all member require lock" )
806840
807- let runningTfm = tryGetRunningTfm
841+ // Interactive processes read their own configuration to find the running tfm
842+ let targetTfm =
843+ if isInteractive then
844+ tryGetRunningTfm ()
845+ else
846+ let sdkDir = tryGetSdkDir () |> replayWarnings
847+
848+ match sdkDir with
849+ | Some dir -> tryGetTfmFromSdkDir dir
850+ | None -> tryGetRunningTfm ()
808851
809852 // Coreclr has mechanism for getting rid
810853 // System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier
@@ -855,7 +898,7 @@ type internal FxResolver
855898 | Architecture.Arm64 -> baseRid + " -arm64"
856899 | _ -> baseRid + " -arm"
857900
858- runningTfm , runningRid)
901+ targetTfm , runningRid)
859902
860903 static member ClearStaticCaches () =
861904 desiredDotNetSdkVersionForDirectoryCache.Clear()
@@ -878,7 +921,7 @@ type internal FxResolver
878921 let defaultReferences =
879922 if assumeDotNetFramework then
880923 getDotNetFrameworkDefaultReferences useFsiAuxLib, assumeDotNetFramework
881- else if useSdkRefs then
924+ elif useSdkRefs then
882925 // Go fetch references
883926 let sdkDir = tryGetSdkRefsPackDirectory () |> replayWarnings
884927
0 commit comments