sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Fix Dependency Stomping in Commands Directory

Open brthor opened this issue 10 years ago • 9 comments

What is the problem?

  • Every Command and it's closure is flat packed into the bin directory
  • Command Dependency closures stomp on each other between publishes
  • Ultimately leads to issues like dotnet/sdk#4512

This has worked up until now, but is not scalable. Ultimately dotnet/sdk#4402 could solve this, but I think we need a solution in the more immediate timeframe. To resolve this I am proposing the following:

  • Fix dependency stomping by relying on the host and .deps file for closure resolution
  • dotnet compile (rather than publish) commands
  • ship a nuget cache with the closure we need in the installers, or populate on first run

Details below

A Solution for Dependency Stomping

First change all command project.jsons to be referencing the NETStandard.Library package rather than an assorted collection of libraries. This will at least reduce the variance in the number of packages being restored.

Second, change our stage2 artifact layout so only the driver (with dependencies) is in the root. This ensures that the driver will just work after an install. The remaining commands will go in another directory, to prevent them from using the driver's set of dependencies.

  • Publish only the dotnet driver to stage2/bin
  • Add an additional directory stage2/bin/commands
  • Perform dotnet-compile rather than dotnet-publish on the remaining commands, outputting to stage2/bin/commands , ensuring corehost + the deps file is emitted for each command, but omitting dependencies.

Third, add a convention to the driver to first look in ./commands/ (where . is the directory of the driver executable) to resolve paths to tool executables, before searching the path (This could also solve our issues of having too many things on the path dotnet/sdk#4366)

Where do dependencies come from?

These changes will give us the basic mechanisms to solve the dependency stomping in our output folder. The remaining issue is that the tools in the commands folder rely on resolving dependencies from some package cache (through use of the .deps file). Acquiring this default set of packages is somewhat difficult because a package cache is user specific, but native package installations (especially .deb packages) operate at a system level. For example, .deb maintainer scripts (at install time) have no reliable way of determining the user who invoked the installation.

Default Set of Nuget packages?

We need a way to deliver/restore the package cache for the current user. I've thought of a couple solutions, but am very open to other ideas or feedback.

  1. Restore packages at the first run of dotnet. (This would require packaging the project.json for each of our commands, and restoring it if we don't find it's corresponding project.lock.json)
  2. A variation on the first, take the build time package cache (which has unpacked all dependencies of our commands), put it into our packages, and copy it to the user package cache at first run. (Alternatively could put it in an archive and unpack it to save space)
  3. Add capability for resolving from some non-user specific package cache in corehost (not sure how feasible this is)

I think the 2nd would deliver a superior user experience than the first, since copying/unpacking would be much faster than a full restore, but it would increase the size of our packages. None of these solutions are perfect though.

/cc @piotrpMSFT @Sridhar-MS @anurse @davidfowl @krwq @schellap Thoughts? Feedback? Different solutions?

brthor avatar Dec 08 '15 02:12 brthor

This is actually causing pdb to not match dll in the stage2 folder for couple of assemblies which makes debugging pretty hard without playing with the assemblies. I think we should fix it ASAP as it may cause us a headache

krwq avatar Dec 08 '15 21:12 krwq

I also tried debugging and the dlls and pdbs do not match. Should I reopen this issue, or is there a newer one tracking this issue?

cdmihai avatar Dec 10 '15 23:12 cdmihai

Closed because dotnet/cli#445 should solve the problem of having different dependencies.

As for the .pdb mismatch, I don't think it is the same issue. Most likely this is related to the postcompile scripts in some way. They fail to copy over the .pdb after the first publish. Right now the .dlls get overwritten after the first publish, so it may explain the mismatch. dotnet/cli#445 might resolve this, but ultimately it needs more investigation.

dotnet/cli#444 is tracking the postcompile script issue

brthor avatar Dec 11 '15 00:12 brthor

@piotrpMSFT @Sridhar-MS @livarcocc

I think we need to start looking at this. Dependency Stomping is causing us pain.

brthor avatar Jan 28 '16 23:01 brthor

Cc @tijoytom

@piotrpMSFT this might need our attention again when we go for a cleaner RC.

schellap avatar Jan 28 '16 23:01 schellap

Does this problem arise because each project dependency has its own lock file, each with possibly a different version of the same potential transitive dependency?

In this case, one solution would be for nuget restore to accept multiple projects (that need to be built for a greater metaproject) and construct a master lock file with the resolved dependencies across multiple projects.

For example, when having A -> B -> C, with the present nuget workflow we get 3 lock files, one for each project, with possibly different versions for the same dependency (this leading to dependency stomping when the output of B and C are copied into A's output):

restore A
restore B
restore C

For example, A depends on FooLib 3.0, B depends on FooLib 2.0, and C depends on FooLib 1.0. The above scheme leads to A's FooLib 3.0 being overwritten when B and C are published into A's output.

Instead, Nuget could accept all projects at once and build one master lock file with all inter project dependency conflicts resolved: restore A B C

In this case, Nuget is aware that FooLib is encountered 3 times and picks one according to its dependency conflict resolution. No more dependency stomping.

cdmihai avatar Jan 28 '16 23:01 cdmihai

@cdmihai I like the general idea. I think we'd need to see what NuGet thinks but I can forsee at least one potential problem.

One issue we have right now is that some of our dependencies are forcing us to lower versions of FX libraries. In these cases we'd be forcing the entire toolchain to that low version rather than just the one command (which is part of the original problem).

brthor avatar Jan 29 '16 00:01 brthor

@brthor

True, the problem still persists.

I guess that to minimize this issue, restore could glob together clusters of projects that run in the same core clr instance: these project clusters are forced by the nature of the runtime to use the same dll for a dependency (can't load 2 versions of the same DLL at runtime).

The problem still remains when 2 project clusters resolve to different versions of the same potential transitive dependency. However, these 2 project clusters will never meet each other in the same VM instance so it should be safe.

I guess the conclusion would be that 2 clusters which have a dependency conflict cannot live together in the same folder.

So there could be 2 restore strategies: cluster-agnostic that forces the same dependency version for the whole tree; and cluster-aware, that groups the projects sent to restore by whether they can inhabit the same VM instance or not.

cdmihai avatar Jan 29 '16 00:01 cdmihai

moving to the 1.0.0-rc2 milestone

joshfree avatar Mar 25 '16 17:03 joshfree