2

I have a project MySdk.csproj that I'm packing at build time via dotnet pack. I want to express that this project has a transitive dependency on Trans.nupkg, but I don't actually want to import all the assets from Trans.nupkg during the build of MySdk.csproj. I DO want a consumer (call it Consumer.csproj) of MySdk.nupkg to get all the assets from Trans.nupkg.

If I ExcludeAssets for Trans.nupkg from MySdk.nupkg then the nuspec and nupkg for MySdk.nupkg reflects the exclusion and when Consumer.csproj references MySdk.nupkg it won't get the transitive assets.

Is there a way to accomplish this?

Thanks.

UPDATE:

If I try to set PrivateAssets to None and ExcludeAssets to build for Trans.nupkg reference from MySdk.csproj like this:

<PackageReference Include="Trans" Version="1.0.*" PrivateAssets="None" ExcludeAssets="build" />

the generated nuspec in MySdk.nupkg looks like this:

<dependency id="Trans" version="1.0.0" include="Runtime,Compile,Native,ContentFiles,Analyzers" />

which means that when Consumer.csproj adds a reference like this:

<PackageReference Include="MySdk" Version="1.0.0" />

the transitive reference back to Trans.nupkg won't include the custom build targets, which is the opposite of what I'm trying to accomplish.

Jeff
  • 35,755
  • 15
  • 108
  • 220

1 Answers1

3

While ExcludeAssets controls which assets the MySdk.csproj will consume, PrivateAssets specifies which assets will not flow across a transitive dependency.

The default for PrivateAssets is contentfiles;analyzers;build which is the reason that the Consumer.csproj will not get built-time assets (.pros/.targets files), roslyn analysers and content files by default.

To change, this reference the Trans package setting PrivateAssets to none which will make Consumer.csproj behave as if it directly referenced the Trans package (example also excluding build-time dependencies from the MySdk project):

<PackageReference Include="trans" PrivateAssets="none" ExcludeAssets="contentfiles;build" />

For more information see Controlling dependency assets.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 1
    Right but the problem is in reverse. I DO want Consumer.csproj to get the build assets from Trans.nupkg. I DONT want MySdk.csproj to run the build assets from Trans.csproj when building. Basically I want to say that the nuspec for MySdk should have a dependency on Trans.nupkg...but I don’t want to use Trans.nupkg’s build assets during the build of MySdk.csproj. I’d just use a nuspec directly and run nuget pack via CLI but that doesn’t work for Sdk style projects - it’s a known bug – Jeff May 20 '18 at 18:51
  • that is the exact goal of the combination of PrivateAssets=none (forward all assets to consuming projects) and ExcludeAssets=contentfiles;build (don't use build logic of this project only) – Martin Ullrich May 20 '18 at 19:03
  • 1
    I had tried that...doesn't work :/ see updated answer – Jeff May 20 '18 at 22:55
  • Any update on this, because I also had the expectation, that actually PrivateAssets are driving the show for the project where it is set, and ExcludeAssets is driving it for all consuming projects as well. But apparently it turned out it is not the fact. Do you have any update on this? – fl0tschi Jan 22 '20 at 16:35
  • PrivateAssets are basically stopping things from flowing transitively and Include-/ExcludeAssets is controlling what the current project gets (separately). If you have a different and specific Szenario you could open a new question. – Martin Ullrich Jan 22 '20 at 17:07
  • I will do that. It looks like it is misbehaving in Non-SDK projects, the PrivateAssets part. Tested it already with a small project but maybe there is an alternative. Lets see – fl0tschi Feb 07 '20 at 10:02