且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么即使我使用MSBuild任务也找不到项目文件?

更新时间:2023-08-22 16:44:28

基于我的测试,当错误的csproj文件路径映射到 .sln中时,我可以重现类似的问题.文件.

您可以检查您的 .sln文件->项目字段.

例如:

文件结构

.csproj文件位于WebApplication2文件夹下,因此项目相对路径为 WebApplication2 \ WebApplication2.csproj .

.sln文件

  VisualStudioVersion = 16.0.30611.23MinimumVisualStudioVersion = 10.0.40219.1Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}")="WebApplication2","WebApplication2 \ WebApplication2.csproj","{497AD76F-222C-4BEB-BDCB-401B0E80B5CE}"结束项目 

更新:

与Cataster讨论后,可以通过在Msbuild任务中指定 **/*.csproj 来解决导入项目的问题.

例如:

I created an Azure Devops Build pipeline and i am trying to build my ASP.NET MVC and Angular hybrid site project on bitbucket (git).

The project first gets checked out, and nuget restores the necessary packages, however, i cannot get the .NET to build!

Ive stumbled on this post here that basically advises against using the dotnet build task and recommended just using Visual Studio Build or MSBuild tasks instead.

Nether are working for me though!

  • I tried VS2017-win2016 hosted agent, didnt work.
  • I tried Windows 2019 hosted agent, didnt work.

I am getting this error:

Bobby.ProjectA\Bobby.ProjectA.csproj(713,3): Error MSB4019: The imported project "C:\Program Files\dotnet\sdk\5.0.100\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\5.0.100\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk. 
Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1 

 Dotnet command failed with non-zero exit code on the following projects : D:\a\1\s\Bobby.ProjectA.Tests\Bobby.ProjectA.Tests.csproj,D:\a\1\s\Bobby.ProjectA\Bobby.ProjectA.csproj 
    D:\a\1\s\Bobby.ProjectA\BobbyProjectA\BobbyProjectA.sln.metaproj(0,0): Error MSB3202: The project file "D:\a\1\s\Bobby.ProjectA\BobbyProjectA\Bobby.ProjectA\Bobby.ProjectA.csproj" was not found. 
    D:\a\1\s\Bobby.ProjectA\BobbyProjectA\BobbyProjectA.sln.metaproj(0,0): Error MSB3202: The project file "D:\a\1\s\Bobby.ProjectA\BobbyProjectA\Bobby.ProjectA.Tests\Bobby.ProjectA.Tests.csproj" was not found. 
    Process 'msbuild.exe' exited with code '1'. 

Since Ubuntu is faster, i also tried that, however, as expected Visual Studio Build task fails with that since VS is not installed on the machine. So i tried with MSBuild and that worked fine with Ubuntu, however, still getting same error that the project file was not found...

Project "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln.metaproj : error MSB3202: The project file "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/Bobby.ProjectA/Bobby.ProjectA.csproj" was not found. [/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln]
/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln.metaproj : error MSB3202: The project file "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/Bobby.ProjectA.Tests/Bobby.ProjectA.Tests.csproj" was not found. [/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln]
Done Building Project "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln" (default targets) -- FAILED.

Build FAILED.

"/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln" (default target) (1) ->
(Build target) -> 
  /home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln.metaproj : error MSB3202: The project file "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/Bobby.ProjectA/Bobby.ProjectA.csproj" was not found. [/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln]
  /home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln.metaproj : error MSB3202: The project file "/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/Bobby.ProjectA.Tests/Bobby.ProjectA.Tests.csproj" was not found. [/home/vsts/work/1/s/Bobby.ProjectA/BobbyProjectA/BobbyProjectA.sln]

I pretty much exhausted my searches and not sure how to solve this.

Heres some screenshots that show my pipeline tasks:

I also thought maybe x64 architecture would have something to do with it, but that didnt resolve the issue...

Ive stumbled on some other interesting posts like this one, but im not sure what adding the project to source countrol means in the answer given, considering my csproj files are already hosted in the repo and being checked out before any tasks run...

My project structure and .sln path (Project):

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bobby.ProjectA", "Bobby.ProjectA\Bobby.ProjectA.csproj", "{12345FE4-4CB9-3245-9A45-DB9A4AF1DFEB}"

Based on my test, I could reproduce the similar issue when the wrong csproj file path is mapped in the .sln file.

You could check your .sln file -> project field.

For example:

File structure

The .csproj file is under the WebApplication2 folder, so the project relative path is WebApplication2\WebApplication2.csproj.

.sln file

VisualStudioVersion = 16.0.30611.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication2", "WebApplication2\WebApplication2.csproj", "{497AD76F-222C-4BEB-BDCB-401B0E80B5CE}"
EndProject

Update:

After discussion with Cataster, the import Project issue can be resolved by specifying **/*.csproj in Msbuild task.

For example: