Skip to content

Commit ba8a100

Browse files
committed
feedback
1 parent e6d87e2 commit ba8a100

File tree

6 files changed

+78
-23
lines changed

6 files changed

+78
-23
lines changed

src/fsharp/Microsoft.DotNet.DependencyManager/DependencyProvider.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ type DependencyProvider (assemblyProbingPaths: AssemblyResolutionProbe, nativePr
420420
))
421421
match result with
422422
| Ok res ->
423-
dllResolveHandler.RefreshPathsInEnvironment()
423+
dllResolveHandler.RefreshPathsInEnvironment(res.Roots)
424424
res
425425
| Error (errorNumber, errorData) ->
426426
reportError.Invoke(ErrorReportType.Error, errorNumber, errorData)

src/fsharp/Microsoft.DotNet.DependencyManager/Microsoft.DotNet.DependencyManager.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<PropertyGroup>
66
<OutputType>Library</OutputType>
7-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
7+
<TargetFramework>netstandard2.0</TargetFramework>
88
<IsPackable>true</IsPackable>
99
<AssemblyName>Microsoft.DotNet.DependencyManager</AssemblyName>
1010
<NoWarn>$(NoWarn);45;55;62;75;1204</NoWarn>

src/fsharp/Microsoft.DotNet.DependencyManager/NativeDllResolveHandler.fs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,36 +107,43 @@ type NativeDllResolveHandlerCoreClr (nativeProbingRoots: NativeResolutionProbe)
107107
()
108108

109109
type NativeDllResolveHandler (nativeProbingRoots: NativeResolutionProbe) =
110-
let addedPaths = ConcurrentBag<string>()
111110
let handler:IDisposable option =
112111
if isRunningOnCoreClr then
113112
Some (new NativeDllResolveHandlerCoreClr(nativeProbingRoots) :> IDisposable)
114113
else
115114
None
116115

117-
member internal _.RefreshPathsInEnvironment() =
118-
for probePath in (nativeProbingRoots.Invoke()) do
119-
let path =
120-
let p = Environment.GetEnvironmentVariable("PATH")
121-
if not(p.EndsWith(";", StringComparison.OrdinalIgnoreCase)) then p + ";"
122-
else p
116+
let appendSemiColon (p:string) =
117+
if not(p.EndsWith(";", StringComparison.OrdinalIgnoreCase)) then
118+
p + ";"
119+
else
120+
p
121+
122+
let addedPaths = ConcurrentBag<string>()
123123

124-
if not (probePath.Contains(path + ";")) then
125-
Environment.SetEnvironmentVariable("PATH", path + probePath + ";")
126-
addedPaths.Add path
124+
let addProbeToProcessPath probePath =
125+
let probe = appendSemiColon probePath
126+
let path = appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
127+
if not (path.Contains(probe)) then
128+
Environment.SetEnvironmentVariable("PATH", path + probe)
129+
addedPaths.Add probe
130+
131+
let removeProbeFromProcessPath probePath =
132+
if not(String.IsNullOrWhiteSpace(probePath)) then
133+
let probe = appendSemiColon probePath
134+
let path = appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
135+
if path.Contains(probe) then Environment.SetEnvironmentVariable("PATH", path.Replace(probe, ""))
136+
137+
member internal _.RefreshPathsInEnvironment(roots: string seq) =
138+
for probePath in roots do
139+
addProbeToProcessPath probePath
127140

128141
interface IDisposable with
129142
member _.Dispose() =
130143
match handler with
131144
| None -> ()
132145
| Some handler -> handler.Dispose()
133146

134-
for probePath in addedPaths do
135-
let path =
136-
let p = Environment.GetEnvironmentVariable("PATH")
137-
if not(p.EndsWith(";", StringComparison.OrdinalIgnoreCase)) then p + ";"
138-
else p
139-
140-
Environment.SetEnvironmentVariable("PATH", path.Replace(probePath + ";", ""))
141-
let mutable _p = null
142-
addedPaths.TryTake(ref _p) |> ignore
147+
let mutable probe:string = null
148+
while (addedPaths.TryTake(&probe)) do
149+
removeProbeFromProcessPath probe

src/fsharp/Microsoft.DotNet.DependencyManager/NativeDllResolveHandler.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ type NativeDllResolveHandler =
1414
/// Construct a new NativeDllResolveHandler
1515
new: nativeProbingRoots: NativeResolutionProbe -> NativeDllResolveHandler
1616

17-
member internal RefreshPathsInEnvironment: unit -> unit
17+
member internal RefreshPathsInEnvironment: string seq -> unit
1818

1919
interface IDisposable

tests/FSharp.Compiler.Private.Scripting.UnitTests/DependencyManagerInteractiveTests.fs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,55 @@ x |> Seq.iter(fun r ->
657657
try Assembly.Load("NoneSuchAssembly") |> ignore with _ -> ()
658658
Assert.False (assemblyFound, "Invoke the assemblyProbingRoots callback -- Error the AssemblyResolve still fired ")
659659

660+
[<Fact>]
661+
member __.``Verify that Dispose cleans up the native paths added``() =
662+
let nativeProbingRoots () = Seq.empty<string>
663+
664+
let appendSemiColon (p:string) =
665+
if not(p.EndsWith(";", StringComparison.OrdinalIgnoreCase)) then
666+
p + ";"
667+
else
668+
p
669+
670+
let reportError =
671+
let report errorType code message =
672+
match errorType with
673+
| ErrorReportType.Error -> printfn "PackageManagementError %d : %s" code message
674+
| ErrorReportType.Warning -> printfn "PackageManagementWarning %d : %s" code message
675+
ResolvingErrorReport (report)
676+
677+
let mutable initialPath:string = null
678+
let mutable currentPath:string = null
679+
let mutable finalPath:string = null
680+
do
681+
initialPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
682+
use dp = new DependencyProvider(NativeResolutionProbe(nativeProbingRoots))
683+
let idm = dp.TryFindDependencyManagerByKey(Seq.empty, "", reportError, "nuget")
684+
let mutable currentPath:string = null
685+
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
686+
let result = dp.Resolve(idm, ".fsx", [|"r", "Microsoft.Data.Sqlite,3.1.7"|], reportError, "netstandard2.0")
687+
Assert.Equal(true, result.Success)
688+
currentPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
689+
finalPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
690+
Assert.True(currentPath <> initialPath) // The path was modified by #r "nuget: ..."
691+
Assert.Equal(finalPath, initialPath) // IDispose correctly cleaned up the path
692+
693+
initialPath <- null
694+
currentPath <- null
695+
finalPath <- null
696+
do
697+
initialPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
698+
let mutable currentPath:string = null
699+
use dp = new DependencyProvider(NativeResolutionProbe(nativeProbingRoots))
700+
let idm = dp.TryFindDependencyManagerByKey(Seq.empty, "", reportError, "nuget")
701+
let result = dp.Resolve(idm, ".fsx", [|"r", "Microsoft.Data.Sqlite,3.1.7"|], reportError, "netcoreapp3.1")
702+
Assert.Equal(true, result.Success)
703+
currentPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
704+
finalPath <- appendSemiColon (Environment.GetEnvironmentVariable("PATH"))
705+
Assert.True(currentPath <> initialPath) // The path was modified by #r "nuget: ..."
706+
Assert.Equal(finalPath, initialPath) // IDispose correctly cleaned up the path
707+
708+
()
660709

661710
[<Fact>]
662711
member __.``Verify that #help produces help text for fsi + dependency manager``() =

vsintegration/Vsix/VisualFSharpFull/VisualFSharpFull.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
<NgenArchitecture>All</NgenArchitecture>
9696
<NgenPriority>2</NgenPriority>
9797
<Private>True</Private>
98-
<AdditionalProperties>TargetFramework=netstandard2.0</AdditionalProperties>
9998
</ProjectReference>
10099
<ProjectReference Include="$(FSharpSourcesRoot)\fsharp\FSharp.Core\FSharp.Core.fsproj">
101100
<Project>{DED3BBD7-53F4-428A-8C9F-27968E768605}</Project>

0 commit comments

Comments
 (0)