Reputation: 2106
When a Visual Studio project uses , how can I access files that are part of the package?
For example, "Newtonsoft.Json" V12.0.3 contains several files besides Newtonsoft.Json.Dll - such as newtonsoft.json\12.0.3\lib\net20\Newtonsoft.Json.xml
and newtonsoft.json\12.0.3\LICENSE.md
.
I found GeneratePathProperty
- which is great, but it doesn't work when building SDK projects inside VS2017. (When building old style framework projects, it does work).
<IncludeAssets>
doesn't appear to do anything.
Part of my csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" GeneratePathProperty="true">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
</Project>
Now, if I build from the command line (msbuild.exe 15.9.21+g9802d43bc3)
msbuild /t:Restore;Rebuild /p:Configuration=Release /p:Platform="Any CPU" MyProject.sln
MyProject.csproj.nuget.g.props
contains
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<PkgNewtonsoft_Json Condition=" '$(PkgNewtonsoft_Json)' == '' ">%userprofile%\.nuget\packages\newtonsoft.json\12.0.3</PkgNewtonsoft_Json>
</PropertyGroup>
Which is great, because I can use $(PkgNewtonsoft_Json) in my project file as needed to get those files.
But, when I build from inside VS2017 (V15.9.17), my *PROPS file does not get this symbol defined :-(
-------- Update --------
OK, after rebooting and trying again it worked :-)
I had to change Targets="restore;build"
to Targets="restore;"
or I would get errors. Given Lib1 and Lib2, where Lib2 depends on Lib1, I get this error
Target CoreCompile:
Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn
CSC : error CS0006: Metadata file 'C:\src\...\build\Lib2\bin\Debug\netstandard2.0\Lib2.dll' could not be found
Done building target "CoreCompile" in project "Lib1.csproj" -- FAILED.
Then, after building the whole solution - it fails, and when I build Lib1 again it fails with
error MSB4006: There is a circular dependency in the target dependency graph involving target "Build".```
Upvotes: 0
Views: 2024
Reputation: 23808
Which is great, because I can use $(PkgNewtonsoft_Json) in my project file as needed to get those files. But, when I build from inside VS2017 (V15.9.17), my *PROPS file does not get this symbol defined
In a deep research and test, I found that it is just an issue in VS2017 IDE while VS2019 has been fixed that issue.
xxx.csproj.nuget.g.props
is generated by the restore process. And in VS2017, for projects with the new SDK, restore package mechanism of VS IDE which is different from msbuild restore mechanism had a large incompatibility problem, resulting in various situations. Fortunately, VS2019 fixes this problem.
As a suggestion, you could try these steps:
1) download the latest VS2019 and use it.
2) add a custom target which contains a task to use MSBuild to build your project into xxxxx.csproj file
.
<Target Name="OnceBuild" AfterTargets="Build">
<MSBuild Projects="$(SolutionDir)$(SolutionFileName)" Targets="restore;build" Properties="Configuration=Release;Platform=Any CPU">
</MSBuild>
</Target>
It will generate the xxx.csproj.nuget.g.props
which is created by MSBuild restore
. Although this contains an error MSB4006, it won't affect the build process in the VS IDE and you do not have to worry about it.
Update 1
Just add a custom target to run msbuild -t:store
in MSBuild
<Target Name="OnceBuild" AfterTargets="Build">
<MSBuild Projects="$(SolutionDir)$(SolutionFileName)" Targets="restore" Properties="Configuration=Release;Platform=Any CPU">
</MSBuild>
</Target>
Upvotes: 1