Skip to content

Commit

Permalink
Fix sdk refs (#10839)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Jan 5, 2021
1 parent 9b34daa commit 28a9a43
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 108 deletions.
51 changes: 38 additions & 13 deletions src/fsharp/CompilerConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ type PackageManagerLine =

[<NoEquality; NoComparison>]
type TcConfigBuilder =
{ mutable primaryAssembly: PrimaryAssembly
{
mutable primaryAssembly: PrimaryAssembly
mutable noFeedback: bool
mutable stackReserveSize: int32 option
mutable implicitIncludeDir: string (* normally "." *)
Expand Down Expand Up @@ -400,7 +401,6 @@ type TcConfigBuilder =
mutable includewin32manifest: bool
mutable linkResources: string list
mutable legacyReferenceResolver: LegacyReferenceResolver
mutable fxResolver: FxResolver

mutable showFullPaths: bool
mutable errorStyle: ErrorStyle
Expand Down Expand Up @@ -464,7 +464,11 @@ type TcConfigBuilder =
/// When false FSI will lock referenced assemblies requiring process restart, false = disable Shadow Copy false (*default*)
mutable shadowCopyReferences: bool
mutable useSdkRefs: bool

mutable fxResolver: FxResolver

/// specify the error range for FxResolver
mutable rangeForErrors: range

/// Override the SDK directory used by FxResolver, used for FCS only
mutable sdkDirOverride: string option

Expand Down Expand Up @@ -571,7 +575,6 @@ type TcConfigBuilder =
includewin32manifest = true
linkResources = []
legacyReferenceResolver = null
fxResolver = Unchecked.defaultof<_>
showFullPaths = false
errorStyle = ErrorStyle.DefaultErrors

Expand Down Expand Up @@ -611,6 +614,8 @@ type TcConfigBuilder =
copyFSharpCore = CopyFSharpCoreFlag.No
shadowCopyReferences = false
useSdkRefs = true
fxResolver = Unchecked.defaultof<FxResolver>
rangeForErrors = range0
sdkDirOverride = None
tryGetMetadataSnapshot = (fun _ -> None)
internalTestSpanStackReferring = false
Expand Down Expand Up @@ -638,29 +643,51 @@ type TcConfigBuilder =
}
|> Seq.distinct

static member CreateNew(legacyReferenceResolver, fxResolver, defaultFSharpBinariesDir, reduceMemoryUsage, implicitIncludeDir,
isInteractive, isInvalidationSupported, defaultCopyFSharpCore, tryGetMetadataSnapshot) =
static member CreateNew(legacyReferenceResolver,
defaultFSharpBinariesDir,
reduceMemoryUsage,
implicitIncludeDir,
isInteractive,
isInvalidationSupported,
defaultCopyFSharpCore,
tryGetMetadataSnapshot,
sdkDirOverride,
rangeForErrors) =

Debug.Assert(FileSystem.IsPathRootedShim implicitIncludeDir, sprintf "implicitIncludeDir should be absolute: '%s'" implicitIncludeDir)

if (String.IsNullOrEmpty defaultFSharpBinariesDir) then
failwith "Expected a valid defaultFSharpBinariesDir"

let tcConfigBuilder =
{ TcConfigBuilder.Initial with
{ TcConfigBuilder.Initial with
implicitIncludeDir = implicitIncludeDir
defaultFSharpBinariesDir = defaultFSharpBinariesDir
reduceMemoryUsage = reduceMemoryUsage
legacyReferenceResolver = legacyReferenceResolver
fxResolver = fxResolver
isInteractive = isInteractive
isInvalidationSupported = isInvalidationSupported
copyFSharpCore = defaultCopyFSharpCore
tryGetMetadataSnapshot = tryGetMetadataSnapshot
useFsiAuxLib = isInteractive
rangeForErrors = rangeForErrors
sdkDirOverride = sdkDirOverride
}
tcConfigBuilder

member tcConfigB.FxResolver =
let resolver =
lazy (let assumeDotNetFramework = Some (tcConfigB.primaryAssembly = PrimaryAssembly.Mscorlib)
FxResolver(assumeDotNetFramework, tcConfigB.implicitIncludeDir, rangeForErrors=tcConfigB.rangeForErrors, useSdkRefs=tcConfigB.useSdkRefs, isInteractive=tcConfigB.isInteractive, sdkDirOverride=tcConfigB.sdkDirOverride))

if tcConfigB.fxResolver = Unchecked.defaultof<FxResolver> then
lock tcConfigB (fun () ->
if tcConfigB.fxResolver = Unchecked.defaultof<FxResolver> then
tcConfigB.fxResolver <- resolver.Force()
)

tcConfigB.fxResolver

member tcConfigB.ResolveSourceFile(m, nm, pathLoadedFrom) =
use unwindBuildPhase = PushThreadBuildPhaseUntilUnwind BuildPhase.Parameter
ResolveFileUsingPaths(tcConfigB.includes @ [pathLoadedFrom], m, nm)
Expand Down Expand Up @@ -801,7 +828,7 @@ type TcConfigBuilder =

member tcConfigB.AddPathMapping (oldPrefix, newPrefix) =
tcConfigB.pathMap <- tcConfigB.pathMap |> PathMap.addMapping oldPrefix newPrefix

static member SplitCommandLineResourceInfo (ri: string) =
let p = ri.IndexOf ','
if p <> -1 then
Expand Down Expand Up @@ -885,9 +912,7 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
#endif
None, data.legacyReferenceResolver.Impl.HighestInstalledNetFrameworkVersion()

let systemAssemblies = data.fxResolver.GetSystemAssemblies()

member x.FxResolver = data.fxResolver
member x.FxResolver = data.FxResolver
member x.primaryAssembly = data.primaryAssembly
member x.noFeedback = data.noFeedback
member x.stackReserveSize = data.stackReserveSize
Expand Down Expand Up @@ -1166,7 +1191,7 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
try
FileSystem.SafeExists filename &&
((tcConfig.GetTargetFrameworkDirectories() |> List.exists (fun clrRoot -> clrRoot = Path.GetDirectoryName filename)) ||
(systemAssemblies.Contains (fileNameWithoutExtension filename)) ||
(tcConfig.FxResolver.GetSystemAssemblies().Contains (fileNameWithoutExtension filename)) ||
tcConfig.FxResolver.IsInReferenceAssemblyPackDirectory filename)
with _ ->
false
Expand Down
13 changes: 9 additions & 4 deletions src/fsharp/CompilerConfig.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ type TcConfigBuilder =
mutable includewin32manifest: bool
mutable linkResources: string list
mutable legacyReferenceResolver: LegacyReferenceResolver
mutable fxResolver: FxResolver
mutable showFullPaths: bool
mutable errorStyle: ErrorStyle
mutable utf8output: bool
Expand Down Expand Up @@ -257,6 +256,8 @@ type TcConfigBuilder =
mutable copyFSharpCore: CopyFSharpCoreFlag
mutable shadowCopyReferences: bool
mutable useSdkRefs: bool
mutable fxResolver: FxResolver
mutable rangeForErrors: range
mutable sdkDirOverride: string option

/// A function to call to try to get an object that acts as a snapshot of the metadata section of a .NET binary,
Expand All @@ -276,16 +277,17 @@ type TcConfigBuilder =

static member Initial: TcConfigBuilder

static member CreateNew:
static member CreateNew:
legacyReferenceResolver: LegacyReferenceResolver *
fxResolver: FxResolver *
defaultFSharpBinariesDir: string *
reduceMemoryUsage: ReduceMemoryFlag *
implicitIncludeDir: string *
isInteractive: bool *
isInvalidationSupported: bool *
defaultCopyFSharpCore: CopyFSharpCoreFlag *
tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot
tryGetMetadataSnapshot: ILReaderTryGetMetadataSnapshot *
sdkDirOverride: string option *
rangeForErrors: range
-> TcConfigBuilder

member DecideNames: string list -> outfile: string * pdbfile: string option * assemblyName: string
Expand Down Expand Up @@ -317,6 +319,9 @@ type TcConfigBuilder =

member AddLoadedSource: m: range * originalPath: string * pathLoadedFrom: string -> unit

member FxResolver: FxResolver


/// Immutable TcConfig, modifications are made via a TcConfigBuilder
[<Sealed>]
type TcConfig =
Expand Down
64 changes: 33 additions & 31 deletions src/fsharp/FxResolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type internal FxResolver(assumeDotNetFramework: bool option, projectDir: string,
else
-1, Array.empty, Array.empty

/// Find the relevant sdk version by running `dotnet --version` in the script/project location,
/// Find the relevant sdk version by running `dotnet --version` in the script/project location,
/// taking into account any global.json
let tryGetDesiredDotNetSdkVersionForDirectoryInfo() =
desiredDotNetSdkVersionForDirectoryCache.GetOrAdd(projectDir, (fun _ ->
Expand Down Expand Up @@ -763,38 +763,40 @@ type internal FxResolver(assumeDotNetFramework: bool option, projectDir: string,

// The set of references entered into the TcConfigBuilder for scripts prior to computing the load closure.
member _.GetDefaultReferences (useFsiAuxLib, assumeDotNetFramework) =
if assumeDotNetFramework then
getDotNetFrameworkDefaultReferences useFsiAuxLib, assumeDotNetFramework
else
if useSdkRefs then
// Go fetch references
match tryGetSdkRefsPackDirectory() with
| Some path ->
try
let sdkReferences =
[ yield! Directory.GetFiles(path, "*.dll")
yield getFSharpCoreImplementationReference()
if useFsiAuxLib then yield getFsiLibraryImplementationReference()
]
sdkReferences, false
with _ ->
let defaultReferences =
if assumeDotNetFramework then
getDotNetFrameworkDefaultReferences useFsiAuxLib, assumeDotNetFramework
else
if useSdkRefs then
// Go fetch references
match tryGetSdkRefsPackDirectory() with
| Some path ->
try
let sdkReferences =
[ yield! Directory.GetFiles(path, "*.dll")
yield getFSharpCoreImplementationReference()
if useFsiAuxLib then yield getFsiLibraryImplementationReference()
]
sdkReferences, false
with _ ->
if isRunningOnCoreClr then
// If running on .NET Core and something goes wrong with getting the
// .NET Core references then use .NET Core implementation assemblies for running process
getDotNetCoreImplementationReferences useFsiAuxLib, false
else
// If running on .NET Framework and something goes wrong with getting the
// .NET Core references then default back to .NET Framework and return a flag indicating this has been done
getDotNetFrameworkDefaultReferences useFsiAuxLib, true
| None ->
if isRunningOnCoreClr then
// If running on .NET Core and something goes wrong with getting the
// .NET Core references then use .NET Core implementation assemblies for running process
// If running on .NET Core and there is no Sdk refs pack directory
// then use .NET Core implementation assemblies for running process
getDotNetCoreImplementationReferences useFsiAuxLib, false
else
// If running on .NET Framework and something goes wrong with getting the
// .NET Core references then default back to .NET Framework and return a flag indicating this has been done
// If running on .NET Framework and there is no Sdk refs pack directory
// then default back to .NET Framework and return a flag indicating this has been done
getDotNetFrameworkDefaultReferences useFsiAuxLib, true
| None ->
if isRunningOnCoreClr then
// If running on .NET Core and there is no Sdk refs pack directory
// then use .NET Core implementation assemblies for running process
getDotNetCoreImplementationReferences useFsiAuxLib, false
else
// If running on .NET Framework and there is no Sdk refs pack directory
// then default back to .NET Framework and return a flag indicating this has been done
getDotNetFrameworkDefaultReferences useFsiAuxLib, true
else
getDotNetCoreImplementationReferences useFsiAuxLib, assumeDotNetFramework
else
getDotNetCoreImplementationReferences useFsiAuxLib, assumeDotNetFramework
defaultReferences

25 changes: 16 additions & 9 deletions src/fsharp/ScriptClosure.fs
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,27 @@ module ScriptPreprocessClosure =
useSimpleResolution, useFsiAuxLib,
basicReferences, applyCommandLineArgs,
assumeDotNetFramework, useSdkRefs, sdkDirOverride,
tryGetMetadataSnapshot, reduceMemoryUsage) =
tryGetMetadataSnapshot, reduceMemoryUsage) =

let projectDir = Path.GetDirectoryName filename
let isInteractive = (codeContext = CodeContext.CompilationAndEvaluation)
let isInvalidationSupported = (codeContext = CodeContext.Editing)

let rangeForErrors = mkFirstLineOfFile filename
let fxResolver = FxResolver(Some assumeDotNetFramework, projectDir, rangeForErrors=rangeForErrors, useSdkRefs=useSdkRefs, isInteractive=isInteractive, sdkDirOverride=sdkDirOverride)

let tcConfigB =
TcConfigBuilder.CreateNew
(legacyReferenceResolver, fxResolver, defaultFSharpBinariesDir, reduceMemoryUsage, projectDir,
isInteractive, isInvalidationSupported, CopyFSharpCoreFlag.No,
tryGetMetadataSnapshot)
let tcConfigB =
let tcb =
TcConfigBuilder.CreateNew(legacyReferenceResolver,
defaultFSharpBinariesDir,
reduceMemoryUsage,
projectDir,
isInteractive,
isInvalidationSupported,
CopyFSharpCoreFlag.No,
tryGetMetadataSnapshot,
sdkDirOverride,
rangeForErrors)
tcb.useSdkRefs <- useSdkRefs
tcb

applyCommandLineArgs tcConfigB

Expand All @@ -147,7 +154,7 @@ module ScriptPreprocessClosure =
| None ->
let errorLogger = CapturingErrorLogger("ScriptDefaultReferences")
use unwindEL = PushErrorLoggerPhaseUntilUnwind (fun _ -> errorLogger)
let references, assumeDotNetFramework = fxResolver.GetDefaultReferences (useFsiAuxLib, assumeDotNetFramework)
let references, assumeDotNetFramework = tcConfigB.FxResolver.GetDefaultReferences (useFsiAuxLib, assumeDotNetFramework)
// Add script references
for reference in references do
tcConfigB.AddReferencedAssemblyByPath(range0, reference)
Expand Down
37 changes: 19 additions & 18 deletions src/fsharp/fsc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,20 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,

let tryGetMetadataSnapshot = (fun _ -> None)

let fxResolver = FxResolver(None, directoryBuildingFrom, rangeForErrors=range0, useSdkRefs=true, isInteractive=false, sdkDirOverride=None)

let defaultFSharpBinariesDir = FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(FSharpEnvironment.tryCurrentDomain()).Value

let tcConfigB =
TcConfigBuilder.CreateNew(legacyReferenceResolver, fxResolver, defaultFSharpBinariesDir,
reduceMemoryUsage=reduceMemoryUsage, implicitIncludeDir=directoryBuildingFrom,
isInteractive=false, isInvalidationSupported=false,
defaultCopyFSharpCore=defaultCopyFSharpCore,
tryGetMetadataSnapshot=tryGetMetadataSnapshot)
let tcConfigB =
TcConfigBuilder.CreateNew(legacyReferenceResolver,
defaultFSharpBinariesDir,
reduceMemoryUsage=reduceMemoryUsage,
implicitIncludeDir=directoryBuildingFrom,
isInteractive=false,
isInvalidationSupported=false,
defaultCopyFSharpCore=defaultCopyFSharpCore,
tryGetMetadataSnapshot=tryGetMetadataSnapshot,
sdkDirOverride=None,
rangeForErrors=range0
)

// Preset: --optimize+ -g --tailcalls+ (see 4505)
SetOptimizeSwitch tcConfigB OptionSwitch.On
Expand All @@ -464,8 +468,8 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
// Now install a delayed logger to hold all errors from flags until after all flags have been parsed (for example, --vserrors)
let delayForFlagsLogger = errorLoggerProvider.CreateDelayAndForwardLogger exiter

let _unwindEL_1 = PushErrorLoggerPhaseUntilUnwind (fun _ -> delayForFlagsLogger)
let _unwindEL_1 = PushErrorLoggerPhaseUntilUnwind (fun _ -> delayForFlagsLogger)

// Share intern'd strings across all lexing/parsing
let lexResourceManager = new Lexhelp.LexResourceManager()

Expand All @@ -482,10 +486,7 @@ let main1(ctok, argv, legacyReferenceResolver, bannerAlreadyPrinted,
with e ->
errorRecovery e rangeStartup
delayForFlagsLogger.ForwardDelayedDiagnostics tcConfigB
exiter.Exit 1

let assumeDotNetFramework = (tcConfigB.primaryAssembly = PrimaryAssembly.Mscorlib)
tcConfigB.fxResolver <- FxResolver(Some assumeDotNetFramework, directoryBuildingFrom, rangeForErrors=range0, useSdkRefs=true, isInteractive=false, sdkDirOverride=None)
exiter.Exit 1

tcConfigB.conditionalCompilationDefines <- "COMPILED" :: tcConfigB.conditionalCompilationDefines

Expand Down Expand Up @@ -633,16 +634,16 @@ let main1OfAst

let directoryBuildingFrom = Directory.GetCurrentDirectory()

let fxResolver = FxResolver(None, directoryBuildingFrom, rangeForErrors=range0, useSdkRefs=true, isInteractive=false, sdkDirOverride=None)

let defaultFSharpBinariesDir = FSharpEnvironment.BinFolderOfDefaultFSharpCompiler(FSharpEnvironment.tryCurrentDomain()).Value

let tcConfigB =
TcConfigBuilder.CreateNew(legacyReferenceResolver, fxResolver, defaultFSharpBinariesDir,
TcConfigBuilder.CreateNew(legacyReferenceResolver, defaultFSharpBinariesDir,
reduceMemoryUsage=reduceMemoryUsage, implicitIncludeDir=directoryBuildingFrom,
isInteractive=false, isInvalidationSupported=false,
defaultCopyFSharpCore=CopyFSharpCoreFlag.No,
tryGetMetadataSnapshot=tryGetMetadataSnapshot)
tryGetMetadataSnapshot=tryGetMetadataSnapshot,
sdkDirOverride=None,
rangeForErrors=range0)

let primaryAssembly =
// temporary workaround until https://github.com/dotnet/fsharp/pull/8043 is merged:
Expand Down
Loading

0 comments on commit 28a9a43

Please sign in to comment.