I am getting the below error while trying to build a project in Visual Studio

Sri Adapa 35 Reputation points
2024-10-17T17:18:33.1866667+00:00

Severity Code Description Project File Line Suppression State Error MSB4018 The "GenerateDepsFile" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified. File name: 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' at Microsoft.Extensions.DependencyModel.DependencyContextWriter.Write(DependencyContext context, Stream stream) at Microsoft.NET.Build.Tasks.GenerateDepsFile.WriteDepsFile(String depsFilePath) at Microsoft.NET.Build.Tasks.TaskBase.Execute() at Microsoft.NET.Build.Tasks.TaskWithAssemblyResolveHooks.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. RegressionTests C:\Program Files\dotnet\sdk\6.0.135\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets 172

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,172 questions
{count} votes

Accepted answer
  1. Gowtham CP 5,210 Reputation points
    2024-10-18T05:15:06.29+00:00

    Hi Sri Adapa,

    Thanks for reaching out on Microsoft Q&A.

    To resolve the MSB4018: GenerateDepsFile error, first ensure that all your NuGet packages, especially System.Text.Json, are up to date, and confirm that your project is targeting the correct framework (NET 6.0). Next, clear the NuGet cache using dotnet nuget locals all --clear or through Visual Studio’s cache management. Then, reinstall System.Text.Json by running Install-Package System.Text.Json -Version 6.0.0. Finally, restore the project dependencies with dotnet restore and rebuild the solution.

    Hope it helps!

    If you found this answer helpful, please upvote and mark it as accepted to close the thread. Thanks!


3 additional answers

Sort by: Most helpful
  1. MA M 10 Reputation points
    2024-11-09T17:13:46.0966667+00:00

    The problem causing this error, as is often the case with Microsoft, is that they introduce updates without warning, and they are not properly tested. I hadn’t touched a single line of my project, and I got an error when compiling. After searching online for a long time, I found that an update to .NET 6 was introduced at the beginning of October (October 8th, to be specific), which is the SDK 6.0.135. This new SDK contains the bug, and it was what caused the whole issue. I forced the 6.0.100 version in my projects, and everything went back to working fine. The truth is, I don’t know if the folks at Microsoft are aware of the time wasted and frustration these changes cause.

    2 people found this answer helpful.

  2. Jiachen Li-MSFT 32,451 Reputation points Microsoft Vendor
    2024-10-18T07:17:33.5933333+00:00

    Hi @Sri Adapa

    The error you're encountering (MSB4018 "GenerateDepsFile" task failed unexpectedly) is related to a missing or mismatched version of the System.Text.Json assembly. It appears that the project is expecting version 6.0.0.0 of System.Text.Json, but it either isn't installed or can't be found.

    Ensure that your project references the correct version of System.Text.Json. To do this:

    • Open your project’s csproj file.
    • Look for a reference to System.Text.Json. It should look something like this:
    <PackageReference Include="System.Text.Json" Version="6.0.0" />
    
    • If the reference is missing, add it or update the version to 6.0.0.

    Sometimes, the issue can be due to corrupted NuGet package caches. Clear the cache and restore packages:

    • Run the following commands in the Package Manager Console or Command Prompt:
    dotnet nuget locals all --clear
    dotnet restore
    

    Try uninstalling and reinstalling the System.Text.Json package from Nuget Package Management.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Whelan, Paul 0 Reputation points
    2024-11-08T14:26:13.9233333+00:00

    I've been dealing with the same issue, finally found a solution which was to switch from using MSBuild 2019 to MSBuild 2022 and the issue went away.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.