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

Sri Adapa 25 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,166 questions
{count} vote

Accepted answer
  1. Gowtham CP 5,050 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!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,051 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

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.