Using .NET Core, I'm trying to create a new Nuget without it installing the it's own dependencies.
This is my new NuGet package csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<TargetFrameworks>net461</TargetFrameworks>
<PackageId>MyFirstNuget</PackageId>
<Version>1.0.1-prerelease</Version>
<Authors></Authors>
<Company></Company>
<Description></Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SccLocalPath>.</SccLocalPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SomePackage" Version="7.12.4" />
</ItemGroup>
</Project>
When I install the package on a project it installs the package "SomePackage"
Is there an option not to install all the dependencies?
When working with PackageReference there are some ways to control dependency assets.
As stated here:
IncludeAssets: These assets will be consumed
ExcludeAssets: These assets will not be consumed
PrivateAssets: These assets will be consumed but won't flow to the parent project
In your case you need to use PrivateAssets like this:
<ItemGroup>
<PackageReference Include="SomePackage" Version="7.12.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
since your project uses SomePackage, but you don't want SomePackage installed when you use your new NuGet package.
I think PrivateAssets can help you. Try changing your PackageReference to
<PackageReference Include="UmbracoCms" Version="7.12.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Related
I'm making a nuget package which only includes references to a bunch of analyzers and a ruleset file.
I am struggling with getting it to add the <CodeAnalyzersRuleSet> tag to the .csproj file during package install.
After searching for a few hours I stumbled upon this 5 year old question which attempts to solve the same thing but I can't get it to work.
I've configured my NuGet project as follows:
Foo.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- Author, Description, ect removed for brevity -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsyncFixer" Version="1.6.0">
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
build\Foo.targets file:
<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)Foo.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
</Project>
And then the build\Foo.ruleset itself.
I run dotnet pack (dotnet version 7.0.102) in order to create the nuget package.
During package install the only thing that happens is that the package is added like a normal <PackageReference>, and I get all the analyser warnings but it doesn't add the ruleset file, and it doesn't add the <CodeAnalyzerRuleSet> tag to the .csproj file.
I've inspected the nuget package and it doesn't include the .targets and .ruleset files unless I add the following to my .csproj file as well:
<ItemGroup>
<None Include="build\**" Pack="True" PackagePath="build\" />
</ItemGroup>
This adds the files to the .nupkg but they are still not added or applied to my project during package install.
Any help or pointers are greatly appreciated.
Running MacOS Monterey version 12.1
IDE Jetbrains Rider version 2021.3.2
Using Microsoft EF Core version 6.0.1 on Macbook Pro M1. Trying to save to the database using EF Core but getting the following error message back.
It's an Azure Functions App that I am running locally and attempting to connect locally.
I've got Migrations working through EntityFramework but just cannot see to get the dbContext to connect to the DB when a function is called through the app.
'System.DllNotFoundException: Unable to load shared library 'e_sqlite3' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libe_sqlite3, 0x0001): tried: 'libe_sqlite3' (no such file), '/usr/local/lib/libe_sqlite3' (no such file), '/usr/lib/libe_sqlite3' (no such file), '/Users/<name>/RiderProjects/API/project.API.Admin/bin/Debug/net6.0/libe_sqlite3' (no such file), '/usr/local/lib/libe_sqlite3' (no such file), '/usr/lib/libe_sqlite3' (no such file)'
I've tried installing 'SQLitePCLRaw.bundle_e_sqlite3 2.0.8-pre20220111224339' package but I still get the same problem.
Here is my CSPROJ file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>V4</AzureFunctionsVersion>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\iAi.Services\iAi.Services.csproj" />
</ItemGroup>
</Project>
Any help on how to get SQLite working on Mac using EntityFramework and .NET 6 is greatly appreciated.
Try to add Nuget package SQLitePCLRaw.bundle_e_sqlite3 2.0.7
I'm trying to understand how dependencies work in .NET Core.
Let's say I have 2 projects. The Project1 has this definition:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Project2\Project2.csproj" />
</ItemGroup>
</Project>
And this single class which uses the Newtonsoft.Json dependency:
public class Wizard
{
public void DoMagic()
{
var settings = Newtonsoft.Json.JsonConvert.DefaultSettings;
}
}
As you can see, it also references Project2, which has the following definition:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>
When I remove the Newtonsoft.Json package reference from the Project1, I would expect it to no longer compile... But it does compile! As it seems, it's able to use the Newtonsoft.Json that is a dependency of Project2.
So I have 2 questions:
I've done my tests with an "internal" project reference for convenience, but does this work the same way with external package references (i.e. NuGet)?
Can anybody explain the rationale here? It seems risky to me, a change of a dependency in any of the dependencies of my project can break my project, if it is using that sub-dependency. In other words, why is this allowed? It seems to be always a bad idea to use implicitly a dependency of a dependency, because it could change and break your code, so why does the framework allow this?
Yes, that's how it works across everything.
You can use PrivateAssets to control this:
https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets
The tree of my solution looks like :
Project A
References Nuget Package "Some Package"
Project B
References Project A
When building, project B produces a package, let's call it PackageB
In ProjectB.csproj I have used the following:
<ProjectReference Include="ProjectA.csproj">
<PrivateAssets>All</PrivateAssets>
</ProjectReference>
Meaning PackageB, in addition to ProjecdtB.dll, includes ProjectA.dll
However it does not include "Some Package", so when I launch a client that references PackageB, I get a runtime error complaining that the dll contains in "Some Package" is missing.
How can I make sure "Some Package" is added as a depencency of PackageB. I'd like to do this in csproj, without relying on a nuspec file. Is this possible ?
[EDIT]
In order to get ProjectA included in the PackageB, I also need to mention that I'm using the Teronis.MSBuild.Packaging.ProjectBuildInPackage.
Thanks for using my package.
Let's first assume the following:
<!-- Project A -->
<ItemGroup>
<PackageReference Include="SomePackage" Version="*" />
</ItemGroup>
<!-- Project B -->
<ItemGroup>
<ProjectReference Include="ProjectA" PrivateAssets="all">
<!--<PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />-->
</ItemGroup>
You are telling NuGet that you don't want to have Project A to be picked up as NuGet-dependency. This is implicit, you don't have control about that. The down-side is the assmeblies of Project A, but not the assemblies of the packages of Project A, are not present in package of Project B.
By removing PrivateAssets="all" you disable the implicit behaviour of NuGet and the Project A will be picked up as NuGet dependency and EACH non-dependency package (also called transitive package).
Now let's asumme this:
<!-- Project A -->
<ItemGroup>
<PackageReference Include="SomePackage" Version="*" />
</ItemGroup>
<!-- Project B -->
<ItemGroup>
<ProjectReference Include="ProjectA" PrivateAssets="all">
<PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />
</ItemGroup>
By having installed my package I assist in the implicit behaviour of NuGet: By not picking up Project A as NuGet-dependency copy over the direct assemblies produced by Project A to the bin-folder of Project B. This has the following drawback:
Because of the implicit behaviour and the usage of my package you have assemblies of Porject A in Project B that are in need of the assemblies provided by packages (in your example "Some Package") you referenced in Project A. So a workaround is to add the packages from Project A in Project B explicitly as shown here:
<!-- Project A -->
<ItemGroup>
<PackageReference Include="SomePackage" Version="*" />
</ItemGroup>
<!-- Project B -->
<ItemGroup>
<ProjectReference Include="ProjectA" PrivateAssets="all">
<PackageReference Include="Teronis.MSBuild.Packaging.ProjectBuildInPackage" Version="0.1.7" />
<!-- Use the SAME version like in Project A. -->
<PackageReference Include="SomePackage" Version="*" />
</ItemGroup>
This should solve your problem. Please provide feedback when it does NOT work.
In the PackageReference documentation, PrivateAssets is described as:
These assets will be consumed but won't flow to the parent project
Meaning the dependencies of Project A won't be copied over in Package B.
If you remove the PrivateAssets node, it should flow the dependencies properly.
I'm making a core package for all the test projects in several solutions. A dependency graph is like:
MyTests.csproj -> MyTestFramework (nuget package) -> JUnitTestLogger (nuget package)
The problem is JUnitTestLogger.dll has to be copied to the output folder on the build of MyTests.csproj, or it just doesn't work. OK, I add:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
to MyTests.csproj and build again. As a result, there is every transitive dependency dll in the output folder... except for JUnitTestLogger.dll. I've checked its source code JUnitTestLogger.csproj and found nothing special.
What's wrong with this package, why is it not copied? I'm asking here, not on Github because of low activity at the project's repo.
MyTests.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MyTestFramework" Version="1.2.3" />
</ItemGroup>
</Project>
MyTestFramework.csproj (part):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>true</IsPackable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
</ItemGroup>
</Project>
Auto-generated MyTestFramework.nuspec (part):
<dependencies>
<group targetFramework=".NETCoreApp2.2">
<dependency id="JUnitTestLogger" version="1.1.0" exclude="Build,Analyzers" />
</group>
</dependencies>
I'm a maintainer on JunitXml.TestLogger (A newer package than the one you mentioned, but both were forked from the same source in Github/Spekt and share some code). The main thing I am aware of that is unusual with the test loggers is that they are referenced in your project, but not used by the code.
I haven't seen this specific issue, but there have been several in the past like this one where the library isn't copied on build. When I first used these libraries I had to put extra steps in my CI builds to copy the library in. From what I recall, my solution was the same as the one in the issue, which was to switch from msbuild to dotnet build. So maybe that or one of the other closed issues there will give a clue.