Friday, 2 March 2018

project.json doesn't have a runtimes section, add '“runtimes”: { “win”: { } }' to project.json within a .Net Class Library

Recently I've started experiencing weird build errors when switch branches on a product.
Error : Your project.json doesn't have a runtimes section. You should add '"runtimes": { "win": { } }' to your project.json and then re-run NuGet restore.
It's a weird sounding error and the fact it mentions project.json makes it sound like a hangup from when .Net Core and Standard were using project.json files before they became .csproj files again.

My first thought to resolve this is always try a clean and build, particularity when swapping branches things could get left hanging around but this doesn't work.

I took a closer look at the projects affected and the cause is to do with one of the branches of our product. It's an early development branch of a new feature where the project has become .Net Standard 2.0 but also targets .Net 4.5, where as the support and main dev branch are still the full framework class libraries.  The cause is most certainly to do with how msbuild produces different artifacts upon each branch.

The solution: simply delete the obj folder from the affected projects when you switch branches, there are no left over artifacts then affecting the build.

For those that are interested or if you don't want to be deleting your obj folder all the time the error actually only happens if there is a project.assets.json within the obj folder as even the new .csproj generates this file. Delete it when you switch branches and your builds will work again.

[Update 16/03/2018]
This error can also seen as
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(186,5): error : Your project is not referencing the ".NETPortable,Version=v4.5,Profile=Profile7" framework. Add a reference to ".NETPortable,Version=v4.5,Profile=Profile7" in the "frameworks" section of your project.json, and then re-run NuGet restore.

Again its the same issue, delete the project.assets.json and everything will build fine. 

No comments:

Post a Comment