Importing the file 'xxx.targets' results in a circular dependency - c#

I have a NuGet package called "Contoso.Library" with a targets file: "/build/Contoso.Library.targets"
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" />
</ItemGroup>
</Project>
However, when I try and build a project containing this I get the following error:
Importing the file "C:\Program Files\dotnet\sdk\2.1.2\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets" into the file "C:\Users\rb.nuget\packages\contoso.library\1.0.0\build\contoso.library.targets" results in a circular dependency. C:\Users\rb.nuget\packages\contoso.library\1.0.0\build\contoso.library.targets
I have confirmed that if I rename the targets file to "contoso.library.targets.xxx" this error goes away, demonstrating that the targets file is where the problem lies.
For reference, here is the project file of the project consuming Contoso.Library:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Contoso.Library" Version="1.0" />
</ItemGroup>
</Project>

Turns out the answer is actually really simple - just drop the "Sdk" attribute from the Project element in the targets file:
<Project Sdk="Microsoft.NET.Sdk">
becomes
<Project>

The same error and the same solution (thanks #RB. !), but I was using Directory.Build.props file instead of a targets file.
My error:
Importing the file "C:\Program
Files\dotnet\sdk\3.1.401\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props" into
the file "<snip>\Directory.Build.props" results in a circular
dependency
Exact solution:
I removed the Sdk="Microsoft.NET.Sdk" attribute from the Directory.Build.props and the projects rebuild correctly.
HTH!

Related

System.IO.FileNotFoundException after changing TargetFramework to net6.0 while using assembly reference

I have a project A that in its csproj has TargetFramework set to net48 and uses a local dll from another project B for an assembly reference. If I build both of the projects and run the app A it works correctly, and the bin folder contains every dll for Nuget packages the project B needs.
When I change TargetFramework of both of the projects to net6.0 and build them, the bin folder of the project A doesn't contain the needed dlls anymore, and if i run the app A it stops with the exception: "System.IO.FileNotFoundExceptionn: Could not load file or assembly".
Here's an example of what the project A's csproj looked like before:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<LangVersion>latest</LangVersion>
<RootNamespace>ProjectA</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Reference Include="ProjectB">
<HintPath>..\..\SolutionB\ProjectB\bin\Release\ProjectB.dll</HintPath>
</Reference>
</ItemGroup>
And here's how it looks after switching to net6.0 (I'm including other minor changes in case it turns out to be important):
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<LangVersion>latest</LangVersion>
<RootNamespace>ProjectA</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Reference Include="ProjectB">
<HintPath>..\..\SolutionB\ProjectB\bin\Release\net6.0\ProjectB.dll</HintPath>
</Reference>
</ItemGroup>
I couldn't find the answer for why it behaves differently and what is needed to achieve the same behaviour as before, if it's even possible. If not then what is the best way to reference another project's assembly? Would really appreciate the help on this.

WPF .Net Core 3.1 WPF Conversion Not going Well (Fun Quesiton!!)

I am converting a WPF App from Framework 4.7.2 to .NetCore 3.1
Entire project all source under discussion is here:
https://github.com/BicycleMark/LatestSweeper
I am using the side by side / same folder technique so that both Framework and new Core can use same project and subfolders.
Sweeper.csproj
SweperCore.csproj
exist in same folder
the Sweeper.csproj compiles and runs just fine as before.
The SweeperCore.csproj comes back with:
Severity Code Description Project File Line Suppression State
Error NETSDK1005 Assets file 'D:\Src\Github\Sweeper\Sweeper\obj\project.assets.json' doesn't have a target for
'.NETCoreApp,Version=v3.1'. Ensure that restore has run and that you have included 'netcoreapp3.1' in the TargetFrameworks for your project. SweeperCore C:\Program Files\dotnet\sdk\3.1.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets 234
I have examined that it is not about the packages as csproj references no pkg files.
Here is the .NetCore SweperCore.csproj contents:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<OutputPath>bin_core\Debug\</OutputPath>
<IntermediateOutputPath>out_core\Debug\</IntermediateOutputPath>
</PropertyGroup>
</Project>
Here is the image from Framework app:
Problem:
The net472 and the netcoreapp3.1 projects are both using the ./obj/ folder for intermediate build files. Obviously this is a problem, if both projects are loaded in Visual Studio.
Solution
Replace the IntermediateOutputPath or BaseIntermediateOutputPath (both should work) for one project. This will make side by side usage in Visual Studio possible. I've cloned your repo and managed to build both apps sucessfully without unloading one of them.
Due to the fact that the BaseIntermediateOutputPath must be evaluated before any Microsoft.Common properties are being used, overriding it needs more work, than I've expected:
Modify the SweeperCore.csproj as followed:
Remove the Sdk Attribute in the root Project Tag:
<Project>
Add the following PropertyGroup and Import as the first childs of <Project>
<PropertyGroup>
<BaseIntermediateOutputPath>obj_core</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.props" />
Add this PropertyGroup to remove the net472 out/ folder from the core app:
<ItemGroup>
<Compile Remove="obj\**" />
<EmbeddedResource Remove="obj\**" />
<None Remove="obj\**" />
<Page Remove="obj\**" />
</ItemGroup>
Add this Import as the last child before </Project>.
<Import Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.targets" />
Original Source: https://github.com/microsoft/msbuild/issues/1603
Here I have encoded the full answer in a tip and trick on CodeProject
https://www.codeproject.com/Tips/5272662/Simple-Side-By-Side-Migration-Project-Template-for

How to pack the products from multiple projects into one nuget without any nuspec file?

I am using Sdk projects targeting .NET Framework 4.7.2. The project structure is:
SmokeTests
|
+--UITests
| |
| +--Common
|
+ NonUITests
|
+--Common
where:
the SmokeTests project references both UITests and NonUITests using ProjectReferences
UITests and NonUITests both reference Common using a ProjectReference
UITests, NonUITests and Common reference some NuGet packages using PackageReferences
the SmokeTests project has no source code, but it does have some content files.
I use the SmokeTests project as a roll up project. When I build it, its bin\debug\net472 directory contains all the binaries and symbols I want to have in the nuget package, i.e.:
The NuGet dependencies of UITests, NonUITests and Common
The dlls of UITests, NonUITests and Common
The PDBs
The SmokeTests csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<PackageId>SmokeTests</PackageId>
<NoPackageAnalysis>true</NoPackageAnalysis>
<IncludeBuildOutput>true</IncludeBuildOutput>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ContentTargetFolders>content</ContentTargetFolders>
</PropertyGroup>
<ItemGroup>
<Content Include="**\*.ps1" Exclude="PSTests\run.ps1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NonUITests\NonUITests.csproj" />
<ProjectReference Include="..\UITests\UITests.csproj" />
</ItemGroup>
</Project>
I also have a Directory.Build.props file:
<Project>
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowedReferenceRelatedFileExtensions>.pdb</AllowedReferenceRelatedFileExtensions>
<Version>$([System.Text.RegularExpressions.Regex]::Match($(BuildName), `\d+\.\d+(?:.\d+)?(?:.\d+)?`))</Version>
<Version Condition="'$(Version)' == ''">1.0.0.0</Version>
<SourceRevisionId>$(Revision)</SourceRevisionId>
<Company>My Company, Inc.</Company>
<Copyright>Copyright © $([System.DateTime]::Now.Year) by My Company, Inc. All rights reserved.</Copyright>
<Product>Smoke Tests</Product>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
When I build the solution, the produced SmokeTests.1.0.0.nupkg file does not contain any of the binaries, except the SmokeTests.dll itself. Clearly not what I want.
How can I get everything from bin\debug\net472 into the produced NuGet package without specifying a nuspec file?
I can always hack an after build step that would manipulate the nupkg file, but I want a proper way to do it.
EDIT 1
Judging by the amount of responses either the question is plain stupid or nobody uses .Net core. Posted it here as well - https://social.msdn.microsoft.com/Forums/vstudio/en-US/aa25cb08-ff95-4d81-b0c3-9c4a395f9999/how-to-pack-the-products-from-multiple-projects-into-one-nuget-without-any-nuspec-file?forum=msbuild
Could it be that SO lost its charm?
EDIT 2
I added the following properties to PublicLib.csproj to produce a NuGet package:
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>$(AssemblyName)</PackageId>
<NoPackageAnalysis>true</NoPackageAnalysis>
<IncludeBuildOutput>true</IncludeBuildOutput>
<AllowedOutputExtensionsInPackageBuildOutputFolder>.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
And it almost works, here is the content of the PublicLib.1.0.0.nupkg\lib\netstandard2.0 folder:
But IncludedLib.pdb is missing.
So, after long hours of inspecting the NuGet.Build.Tasks.Pack.targets I have arrived at the following code for my roll up project SmokeTests:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>$(AssemblyName)</PackageId>
<NoPackageAnalysis>true</NoPackageAnalysis>
<IncludeBuildOutput>true</IncludeBuildOutput>
<AllowedOutputExtensionsInPackageBuildOutputFolder>.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<ContentTargetFolders>content</ContentTargetFolders>
<GenerateNuspecDependsOn>MyCustomizePacking</GenerateNuspecDependsOn>
</PropertyGroup>
<Target Name="MyCustomizePacking" Returns="#(NuGetPackInput);#(_BuildOutputInPackage);#(_TargetPathsToSymbols)">
<ItemGroup>
<NuGetPackInput Remove="#(BuiltProjectOutputGroupKeyOutput);#(DebugSymbolsProjectOutputGroupOutput)"/>
<_BuildOutputInPackage Remove="#(BuiltProjectOutputGroupKeyOutput)"/>
<_TargetPathsToSymbols Remove="#(DebugSymbolsProjectOutputGroupOutput)"/>
<NuGetPackInput Include="#(ReferenceCopyLocalPaths);#(AllItemsFullPathWithTargetPath)" />
<_BuildOutputInPackage Include="%(ReferenceCopyLocalPaths.Identity)" >
<TargetFramework>$(TargetFramework)</TargetFramework>
</_BuildOutputInPackage>
<_BuildOutputInPackage Include="%(AllItemsFullPathWithTargetPath.Identity)" >
<TargetFramework>$(TargetFramework)</TargetFramework>
</_BuildOutputInPackage>
</ItemGroup>
</Target>
<ItemGroup>
<Content Include="**\*.ps1" Exclude="PSTests\run.ps1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NonUITests\NonUITests.csproj" />
<ProjectReference Include="..\UITests\UITests.csproj" />
</ItemGroup>
</Project>
The only way I could find to pack all my project and package references into the NuGet was to hook into the process through GenerateNuspecDependsOn by injecting my target MyCustomizePacking. This target does the following:
Remove the SmokeTests.dll and SmokeTests.pdb from the relevant item groups, because this is a roll up project with no code on its own, I do not need its dll or pdb inside the package.
Include #(ReferenceCopyLocalPaths) and #(AllItemsFullPathWithTargetPath) in the relevant item groups.
Seems to work,but I am not happy with my implementation. I wish there was better support for this.

Self Contained EXE not finding correct path on OS X

So I've made a self-contained release of a project with Visual Studio Code. The EXE is working on ubuntu and windows, but can't get it to work on os-x.
I'm using a simple data.json to store some data. The error that occurs on the os-x release, is that it can't find the correct path to data.json. I get this message:
Could not find file '/Users/User/data.json'.
That is not where I store the release or data.json. I store it in the root-directory of the project. Seems like the application is looking for the json-file in my system root directories. How do I fix this?
My XML csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.json" Version="10.0.3" />
</ItemGroup>
</Project>

How to build .xproj before .csproj

This is a follow-up from this question.
As I'm not able to directly reference my .xproj in my .csproj in Visual Studio, I am forced to directly reference the built net461 dll. So in this situation, I want my .csproj to pre-compile the .xproj before running the .csproj itself.
I've tried modifying the .csproj manually and adding:
<ItemGroup>
<ProjectReference Include="..\SomeFolder\SomeProject.xproj">
<Project>{1A94B405-2D01-4A09-90D5-A5B31180A03B}</Project>
<Name>SomeProjectNamespace</Name>
</ProjectReference>
</ItemGroup>
But when I build the .csproj, it doesn't build the .xproj.
I won't be able to debug .xproj when I run my .csproj, which is bad enough... but at least when I run the .csproj I want to be sure I have the last version of my .xproj compiled.
xproj <=> csproj references in Visual Studio 2015 were always buggy and never worked well. As far as I know, there are no good workarounds.
That said, Visual Studio 2017 RC added support for building .NET Core and .NET Standard with csproj. This deprecates xproj and project.json.
With this new format, a project to project reference would look like this:
<!-- App.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj" />
</ItemGroup>
</Project>
<!-- Library.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.1</TargetFramework>
</PropertyGroup>
</Project>

Categories

Resources