Skip to content

Commit

Permalink
Workaround not thread-safe FS usage
Browse files Browse the repository at this point in the history
  • Loading branch information
auduchinok committed Sep 27, 2023
1 parent d8fd149 commit 2da5db8
Showing 1 changed file with 74 additions and 66 deletions.
140 changes: 74 additions & 66 deletions src/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module FSharpDependencyManager =
|> List.distinct
|> (fun l -> l, binLogPath, timeout)

let computeHashForResolutionInputs
let _computeHashForResolutionInputs
(
scriptExt: string,
directiveLines: (string * string) seq,
Expand Down Expand Up @@ -289,14 +289,14 @@ type ResolveDependenciesResult
member _.Roots = roots

[<DependencyManager>]
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<string, string>()

let projectDirectory, cacheDirectory =
let getProjectDirectory () =
let createDirectory directory =
lazy
try
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -415,38 +416,38 @@ type FSharpDependencyManager(outputDirectory: string option, useResultsCache: bo

generateAndBuildProjectArtifacts

let tryGetResultsForResolutionHash hash (projectDirectory: Lazy<string>) : 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<string>) : 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 ())

Expand All @@ -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
(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 2da5db8

Please sign in to comment.