Error NETSDK1004 occurs when building any C++/CLI project.
See original GitHub issueDescribe the bug
When building any C++ project using the CLR Class Library (.NET) template, the project cannot be successfully built with the MSBuild command line. Instead, it can only be successfully built using Visual Studio 2022. An error, NETSDK1004, occurs when attempting to build the project with MSBuild.
error NETSDK1004: Assets file 'obj\project.ass
ets.json' not found. Run a NuGet package restore to generate this file.
To Reproduce
- Open Visual Studio 2022 (17.5.3, or 17.6.0 Preview 2.0)
- Create a new C++ project using the CLR Class Library (.NET) template
- Create Git Repository for this solution with default .gitignore template
- Close the Visual Studio instance
- ‼️important‼️ Use
git clean -xdf
to clean the entire repo, excluding the project files - Open the Terminal and use
msbuild -restore
to build the solution
Here is a repository containing only the newly created project.
Exceptions
Here’s the output:
C:\Program Files\dotnet\sdk\7.0.300-preview.23122.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolut
ion.targets(266,5): error NETSDK1004: Assets file 'D:\WIP\Desktop\Walterlv.IssueDemo\Walterlv.IssueDemo\obj\project.ass
ets.json' not found. Run a NuGet package restore to generate this file. [D:\WIP\Desktop\Walterlv.IssueDemo\Walterlv.Iss
ueDemo\Walterlv.IssueDemo.vcxproj]
0 Warning(s)
1 Error(s)
If you want more detail output, I’ll post it here.
Known Workarounds
- If we open the solution with Visual Studio 2022, simply opening it will ensure that the MSBuild command line works correctly. Opening the project using Visual Studio will create some files within the project, and even after Visual Studio is closed, the MSBuild command line will still function properly.
- When building a C++ project using the CLR Class Library (.NET Framework) template, the MSBuild command line works as expected.
By referring to the official documentation, we can find a workaround by using this command line: devenv SolutionName.sln /Build.
Further technical details
I’ve tested this issue in two Windows machines and both failed:
- .NET SDK 7.0.202 + Visual Studio 2022 (17.5.3)
- .NET SDK 7.0.300-preview.23122.5 + Visual Studio 2022 (17.6.0 Preview 2.0)
Here is an info of the 7.0.202 one:
$ dotnet --info
.NET SDK:
Version: 7.0.202
Commit: 6c74320bc3
运行时环境:
OS Name: Windows
OS Version: 10.0.22621
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.202\
Host:
Version: 7.0.4
Architecture: x64
Commit: 0a396acafe
.NET SDKs installed:
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.102 [C:\Program Files\dotnet\sdk]
8.0.202 [C:\Program Files\dotnet\sdk]
Issue Analytics
- State:
- Created 5 months ago
- Reactions:3
- Comments:14 (2 by maintainers)
Top Results From Across the Web
NETSDK1004: Assets file not found - .NET CLI
This error occurs when the assets file project.assets.json is not found during build. The full error message is similar to the following ...
Read more >Error NETSDK1004 project.assets.json' not found. Nuget ...
This is my first time working with pipelines. I have a library repo that is a .NET Standard library intended to be shared...
Read more >Quick Fix for the NETSDK1004 Compile Error
One step fixes the compile Error NETSDK1004: "Assets file 'C:project.assets.json' not found. Run a NuGet package restore to generate this file."
Read more >Assets file '...\project.assets.json' not found." in Pipeline ...
As a result, this error occurs when building a project with a tool such as msbuild that does not automatically restore packages.
Read more >error NETSDK1004: Assets file '...' not found. Run a NuGet ...
Determining projects to restore is printed once per run. It's possible that some runs are failing but others succeed.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gureedo @walterlv @GitDaBytes @bharath-brat thanks for reporting the issue! FYI in Visual Studio 2022 17.7 Preview 2 (released today) there are fixes for CLR .NET projects (aka C++/CLI projects), in particular the projects participating in NuGet’s
PackageReference
(that is<EnableManagedPackageReferenceSupport>true</>
), are now nuget-restored correctly (not skipped anymore), and also they follow correctly the compatibility rules for the TargetPlatformMoniker (identifier and version).Let us know if you can try it out and report any feedback, thank you.
FYI - this is also affecting any .NET 6 C++ / CLR projects. All of the above hold true. setting <EnableManagedPackageReferenceSupport>false</EnableManagedPackageReferenceSupport> stops the project compiling in VS2022, but does allow the build to work in our DevOps Build Pipeline.
the error from VS2022 is error : Your project does not reference “.NETCoreApp,Version=v6.0” framework. Add a reference to “.NETCoreApp,Version=v6.0” in the “TargetFrameworks” property of your project file and then re-run NuGet restore.
Attempting the other workaround msbuild /t:restore project.vcxproj appears to only restore pure csharp projects referenced by my C++ dll, and does not generate an assets file for the C++ project.