From 2da5db8ca406a61152822bb0faa1af9770cb2e8a Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Thu, 9 Mar 2023 12:41:42 +0100 Subject: [PATCH] Workaround not thread-safe FS usage See https://github.com/dotnet/fsharp/issues/12683 --- .../FSharp.DependencyManager.fs | 140 +++++++++--------- 1 file changed, 74 insertions(+), 66 deletions(-) diff --git a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs index cae76f9cbf8..82e50c92480 100644 --- a/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs +++ b/src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs @@ -198,7 +198,7 @@ module FSharpDependencyManager = |> List.distinct |> (fun l -> l, binLogPath, timeout) - let computeHashForResolutionInputs + let _computeHashForResolutionInputs ( scriptExt: string, directiveLines: (string * string) seq, @@ -289,14 +289,14 @@ type ResolveDependenciesResult member _.Roots = roots [] -type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bool) = +type FSharpDependencyManager(outputDirectory: string option, _useResultsCache: bool) = let key = "nuget" let name = "MsBuild Nuget DependencyManager" let generatedScripts = ConcurrentDictionary() - let projectDirectory, cacheDirectory = + let getProjectDirectory () = let createDirectory directory = lazy try @@ -335,19 +335,20 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo | Some v -> Path.Combine(specialDir, v) | _ -> specialDir - createDirectory (Path.Combine(root, "Projects", path)), createDirectory (Path.Combine(root, "Cache")) + createDirectory (Path.Combine(root, "Projects", path)) let deleteScripts () = - try -#if !DEBUG - if projectDirectory.IsValueCreated then - if Directory.Exists(projectDirectory.Value) then - Directory.Delete(projectDirectory.Value, true) -#else - () -#endif - with _ -> - () + () +// try +// #if !DEBUG +// if projectDirectory.IsValueCreated then +// if Directory.Exists(projectDirectory.Value) then +// Directory.Delete(projectDirectory.Value, true) +// #else +// () +// #endif +// with _ -> +// () let emitFile fileName (body: string) = try @@ -386,7 +387,7 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo let packageReferenceText = String.Join(Environment.NewLine, packageReferenceLines) - let projectPath = Path.Combine(projectDirectory.Value, "Project.fsproj") + let projectPath = Path.Combine(getProjectDirectory().Value, "Project.fsproj") let nugetPath = Path.Combine(projectDirectory.Value, "NuGet.config") let generateAndBuildProjectArtifacts = @@ -415,38 +416,38 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo generateAndBuildProjectArtifacts - let tryGetResultsForResolutionHash hash (projectDirectory: Lazy) : PackageBuildResolutionResult option = - match hash with - | Some hash when useResultsCache = true -> - let resolutionsFile = - Path.Combine(cacheDirectory.Value, (hash + ".resolvedReferences.paths")) - - if File.Exists(resolutionsFile) then - let resolutions, references, loads, includes = - let resolutions = getResolutionsFromFile resolutionsFile - let references = (findReferencesFromResolutions resolutions) |> Array.toList - let loads = (findLoadsFromResolutions resolutions) |> Array.toList - let includes = (findIncludesFromResolutions resolutions) |> Array.toList - resolutions, references, loads, includes - - if verifyFilesExist (references) then - Some - { - success = true - projectPath = Path.Combine(projectDirectory.Value, "Project.fsproj") - stdOut = [||] - stdErr = [||] - resolutionsFile = Some resolutionsFile - resolutions = resolutions - references = references - loads = loads - includes = includes - } - else - None - else - None - | _ -> None + // let tryGetResultsForResolutionHash hash (projectDirectory: Lazy) : PackageBuildResolutionResult option = + // match hash with + // | Some hash when useResultsCache = true -> + // let resolutionsFile = + // Path.Combine(cacheDirectory.Value, (hash + ".resolvedReferences.paths")) + // + // if File.Exists(resolutionsFile) then + // let resolutions, references, loads, includes = + // let resolutions = getResolutionsFromFile resolutionsFile + // let references = (findReferencesFromResolutions resolutions) |> Array.toList + // let loads = (findLoadsFromResolutions resolutions) |> Array.toList + // let includes = (findIncludesFromResolutions resolutions) |> Array.toList + // resolutions, references, loads, includes + // + // if verifyFilesExist (references) then + // Some + // { + // success = true + // projectPath = Path.Combine(projectDirectory.Value, "Project.fsproj") + // stdOut = [||] + // stdErr = [||] + // resolutionsFile = Some resolutionsFile + // resolutions = resolutions + // references = references + // loads = loads + // includes = includes + // } + // else + // None + // else + // None + // | _ -> None do AppDomain.CurrentDomain.ProcessExit |> Event.add (fun _ -> deleteScripts ()) @@ -469,8 +470,9 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo |] member _.ClearResultsCache() = - Directory.Delete(cacheDirectory.Value, true) - Directory.CreateDirectory(cacheDirectory.Value) |> ignore + // Directory.Delete(cacheDirectory.Value, true) + // Directory.CreateDirectory(cacheDirectory.Value) |> ignore + () member _.ResolveDependencies ( @@ -490,18 +492,23 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo | _ -> "#r @\"" let generateAndBuildProjectArtifacts = - let resolutionHash = - FSharpDependencyManager.computeHashForResolutionInputs ( - scriptExt, - packageManagerTextLines, - targetFrameworkMoniker, - runtimeIdentifier - ) + let configIncludes = + generateSourcesFromNugetConfigs scriptDirectory (getProjectDirectory().Value) timeout + + let directiveLines = Seq.append packageManagerTextLines configIncludes + + // let resolutionHash = + // FSharpDependencyManager.computeHashForResolutionInputs ( + // scriptExt, + // directiveLines, + // targetFrameworkMoniker, + // runtimeIdentifier + // ) let fromCache, resolutionResult = - match tryGetResultsForResolutionHash resolutionHash projectDirectory with - | Some resolutionResult -> true, resolutionResult - | None -> + // match tryGetResultsForResolutionHash resolutionHash projectDirectory with + // | Some resolutionResult -> true, resolutionResult + // | None -> false, prepareDependencyResolutionFiles ( scriptDirectory, @@ -513,11 +520,12 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo ) match resolutionResult.resolutionsFile with - | Some file -> + | Some _ -> let generatedScriptPath = - match resolutionHash with - | Some hash -> Path.Combine(cacheDirectory.Value, hash) + scriptExt - | None -> resolutionResult.projectPath + scriptExt + // match resolutionHash with + // | Some hash -> Path.Combine(cacheDirectory.Value, hash) + scriptExt + // | None -> + resolutionResult.projectPath + scriptExt // We have succeeded to gather information -- generate script and copy the results to the cache if not (fromCache) then @@ -526,9 +534,9 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo emitFile generatedScriptPath generatedScriptBody - match resolutionHash with - | Some hash -> File.Copy(file, Path.Combine(cacheDirectory.Value, hash + ".resolvedReferences.paths"), true) - | None -> () + // match resolutionHash with + // | Some hash -> File.Copy(file, Path.Combine(cacheDirectory.Value, hash + ".resolvedReferences.paths"), true) + // | None -> () ResolveDependenciesResult( resolutionResult.success,