Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CopyLocalLockFileAssemblies produces broken deps.json file if PrivateAssets is set to compile #22124

Open
handerss-spotfire opened this issue Oct 18, 2021 · 2 comments

Comments

@handerss-spotfire
Copy link

Setting CopyLocalLockFileAssemblies causes runtime exceptions for projects where PrivateAssets is set to compile. This is because the resulting .deps.json file will contain a ".Reference" entry which does not contain the necessary runtime dll.

Example

This project uses a facade library which is meant to wrap some API (in this case System.Security.Cryptography.ProtectedData). To protect consuming projects from the wrapped library the package reference has its private assets set to compile.
This works well unless the Facade library adds the CopyLocalLockFileAssemblies and sets it true.
This addition causes the consuming program's .deps.json to get a reference entry added, in this case it's System.Security.Cryptography.ProtectedData.Reference/5.0.0.0, which in turn results in the program crashing with a runtime error.

The example program below can be run with dotnet run --project ./Transitive/Transitive.csproj and results in the following output:

Unhandled exception. System.PlatformNotSupportedException: Windows Data Protection API (DPAPI) is not supported on this platform.
   at System.Security.Cryptography.ProtectedData.Protect(Byte[] userData, Byte[] optionalEntropy, DataProtectionScope scope)
   at Facade.Facade.Protect(Byte[] input) in C:\tibco\Source\transitive\Facade\Facade.cs:line 8
   at Program.<Main>$(String[] args) in C:\tibco\Source\transitive\Transitive\Program.cs:line 4

Facade/Facade.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Security.Cryptography.ProtectedData" Version="5.0.0" PrivateAssets="compile" />
  </ItemGroup>
</Project>

Facade/Facade.cs

using System;
using System.Security.Cryptography;

namespace Facade
{
    public static class Facade
    {
        public static byte[] Protect(byte[] input) => ProtectedData.Protect(input, null, DataProtectionScope.CurrentUser);
    }
}

Transitive/Transitive.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Facade\Facade.csproj" />
  </ItemGroup>
</Project>

Transitive/Program.cs

using System;
using Facade;

var foo = Facade.Facade.Protect(Array.Empty<byte>());
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Oct 18, 2021
@handerss-spotfire
Copy link
Author

The solution of setting PrivateAssets to compile to protect against transitive dependencies is suggested by @nkolev92 here: #11803 (comment)

For our use case this works well except for the fact that the resulting deps.json files sometimes contain ".Reference" entries which break at runtime. This issue is one such reproducible case, but unfortunately we get these additions even without CopyLocalLockFileAssemblies. Is there anyway to turn this type of behavior off in general?

Is PrivateAssets=compile + DisableTransitiveProjectReferences the best way to protect against compiling against transitive dependencies.

@sfoslund
Copy link
Member

This issue is one such reproducible case, but unfortunately we get these additions even without CopyLocalLockFileAssemblies. Is there anyway to turn this type of behavior off in general?

Not sure, @dsplaisted do you have any guidance here?

@sfoslund sfoslund removed the untriaged Request triage from a team member label Nov 10, 2021
@sfoslund sfoslund removed their assignment Nov 10, 2021
@sfoslund sfoslund added this to the Discussion milestone Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants