diff --git a/integrationtests/Paket.IntegrationTests/BindingRedirect.fs b/integrationtests/Paket.IntegrationTests/BindingRedirect.fs index b0e4b0647d..b4619f5e1a 100644 --- a/integrationtests/Paket.IntegrationTests/BindingRedirect.fs +++ b/integrationtests/Paket.IntegrationTests/BindingRedirect.fs @@ -316,4 +316,19 @@ let ``#1248 redirects off with --redirects``() = let originalConfig = File.ReadAllText(originalConfigPath) config |> shouldEqual originalConfig + +[] +let ``#1477 assembly redirects lock files``() = + let scenario = "i001474-restore-no-locks" + prepare scenario + let p = Paket.Dependencies.Locate (Path.Combine(scenarioTempPath scenario, "paket.dependencies")) + p.Install(true,true) + p.Restore() + + try + Directory.Delete(scenarioTempPath scenario, true) + with e -> + failwith "could not delete directory, i.e. restore holds on to files" + + \ No newline at end of file diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary.sln b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary.sln new file mode 100644 index 0000000000..d319700402 --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{673951A0-5216-4FBD-BA08-7FA057D5CC0D}" + ProjectSection(SolutionItems) = preProject + ..\paket.dependencies = ..\paket.dependencies + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyClassLibrary", "MyClassLibrary\MyClassLibrary.csproj", "{CD678800-8DFC-4BB1-911A-7234F492EA29}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CD678800-8DFC-4BB1-911A-7234F492EA29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CD678800-8DFC-4BB1-911A-7234F492EA29}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CD678800-8DFC-4BB1-911A-7234F492EA29}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CD678800-8DFC-4BB1-911A-7234F492EA29}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Class1.cs b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Class1.cs new file mode 100644 index 0000000000..2580be441d --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Class1.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyClassLibrary +{ + public class Class1 + { + } +} diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/MyClassLibrary.csprojtemplate b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/MyClassLibrary.csprojtemplate new file mode 100644 index 0000000000..01fe857954 --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/MyClassLibrary.csprojtemplate @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {CD678800-8DFC-4BB1-911A-7234F492EA29} + Library + Properties + MyClassLibrary + MyClassLibrary + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + ..\..\packages\Newtonsoft.Json\lib\net45\Newtonsoft.Json.dll + True + True + + + + + \ No newline at end of file diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Properties/AssemblyInfo.cs b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..7920bce104 --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MyClassLibrary")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MyClassLibrary")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("cd678800-8dfc-4bb1-911a-7234f492ea29")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/paket.references b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/paket.references new file mode 100644 index 0000000000..17d7ab6bfc --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/MyClassLibrary/MyClassLibrary/paket.references @@ -0,0 +1 @@ +Newtonsoft.Json framework: net451 \ No newline at end of file diff --git a/integrationtests/scenarios/i001474-restore-no-locks/before/paket.dependencies b/integrationtests/scenarios/i001474-restore-no-locks/before/paket.dependencies new file mode 100644 index 0000000000..bdd652ff0c --- /dev/null +++ b/integrationtests/scenarios/i001474-restore-no-locks/before/paket.dependencies @@ -0,0 +1,3 @@ +source https://www.nuget.org/api/v2/ + +nuget Newtonsoft.Json \ No newline at end of file diff --git a/src/Paket.Core/InstallProcess.fs b/src/Paket.Core/InstallProcess.fs index c08a3f73aa..43539ff804 100644 --- a/src/Paket.Core/InstallProcess.fs +++ b/src/Paket.Core/InstallProcess.fs @@ -159,9 +159,27 @@ let CreateModel(root, force, dependenciesFile:DependenciesFile, lockFile : LockF |> Seq.concat |> Seq.toArray +/// Since Assembly.ReflectionOnlyLoadFrom holds on to file handles indefintely, we use +/// ReflectionOnlyLoad(bytes[]) instead. However, in order to prevent +/// CLR from loading same assemblies multiple times (results in exn), we use a static +/// dictionary for caching assemblies, based on their filename. +module private LoadAssembliesSafe = + + let loadedLibs = new Dictionary<_,_>() + + let reflectionOnlyLoadFrom library = + let key = FileInfo(library).FullName.ToLowerInvariant() + match loadedLibs.TryGetValue key with + | (true,v) -> v + | _ -> + let assemblyAsBytes = File.ReadAllBytes library + let v = Assembly.ReflectionOnlyLoad assemblyAsBytes + loadedLibs.[key] <- v + v + /// Applies binding redirects for all strong-named references to all app. and web.config files. -let private applyBindingRedirects (loadedLibs:Dictionary<_,_>) isFirstGroup createNewBindingFiles cleanBindingRedirects redirects root groupName findDependencies extractedPackages = +let private applyBindingRedirects isFirstGroup createNewBindingFiles cleanBindingRedirects redirects root groupName findDependencies extractedPackages = let dependencyGraph = ConcurrentDictionary<_,Set<_>>() let projects = ConcurrentDictionary<_,ProjectFile option>(); let referenceFiles = ConcurrentDictionary<_,ReferencesFile option>(); @@ -209,18 +227,7 @@ let private applyBindingRedirects (loadedLibs:Dictionary<_,_>) isFirstGroup crea |> Seq.choose(fun (library,redirects) -> try let key = FileInfo(library).FullName.ToLowerInvariant() - let assembly = - match loadedLibs.TryGetValue key with - | true,v -> v - | _ -> - let tempFile = Path.GetTempFileName() - if File.Exists(tempFile) then - File.Delete tempFile - File.Copy(library,tempFile) - let v = Assembly.ReflectionOnlyLoadFrom library - loadedLibs.Add(key,v) - File.Delete tempFile - v + let assembly = LoadAssembliesSafe.reflectionOnlyLoadFrom library Some (assembly, BindingRedirects.getPublicKeyToken assembly, assembly.GetReferencedAssemblies(), redirects) with exn -> None) @@ -388,8 +395,7 @@ let InstallIntoProjects(options : InstallerOptions, forceTouch, dependenciesFile processContentFiles root project usedPackages gitRemoteItems options project.Save forceTouch - let loadedLibs = new Dictionary<_,_>() - + let first = ref true let redirects = @@ -408,9 +414,10 @@ let InstallIntoProjects(options : InstallerOptions, forceTouch, dependenciesFile |> Option.bind (fun p -> p.Settings.CreateBindingRedirects) (snd kv.Value,packageRedirects)) - |> applyBindingRedirects loadedLibs !first options.CreateNewBindingFiles options.Hard (g.Value.Options.Redirects ++ redirects) (FileInfo project.FileName).Directory.FullName g.Key lockFile.GetAllDependenciesOf + |> applyBindingRedirects !first options.CreateNewBindingFiles options.Hard (g.Value.Options.Redirects ++ redirects) (FileInfo project.FileName).Directory.FullName g.Key lockFile.GetAllDependenciesOf first := false + /// Installs all packages from the lock file. let Install(options : InstallerOptions, forceTouch, dependenciesFile, lockFile : LockFile) = let root = FileInfo(lockFile.FileName).Directory.FullName