Imported project target was not found- How to fix

I've opened a solution and one project failed to load because of missing depency target.

The targets was MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2 and the message in the output windows was this:

C:\Users\xx\Source\Repos\XXVS2\XX.UI\XX.UI.csproj : error  : The imported project "C:\Users\xx\Source\Repos\XXVS2\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Users\xx\Source\Repos\XXVS2\\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk.  C:\Users\xx\Source\Repos\XXVS2\XX.UI\XX.UI.csproj

In the project the build target file was imported via the <Import> statement.

<Import Project="$(SolutionDir)\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" />

The issue here is, that if Visual Studio can't find the target file VS can't load the project. But we need VS to load the project, because it's a nuget package where the target file is in. VS will only download the nuget packages for this project, if the project was loaded.

To enable this, we need to add a Condition to the project, then VS can open it and on build it willl download the nuget packages and everything is fine.

Your import should look like this to successfuly load the project in VS.

Import Project="$(SolutionDir)\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" Condition="Exists('$(SolutionDir)\packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.2\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets')" />

Source and more about this issue: https://devblogs.microsoft.com/dotnet/nuget-package-restore-issues/