You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps 2019+VS2022:混合项目编译发布问题求助

问题描述

使用本地构建代理搭配Azure DevOps 2019和VS2022,现有包含.NET6 Web API项目(csproj)和C++/CLI包装器项目(vcxproj)的解决方案,且C#项目引用C++项目。尝试通过Azure DevOps Pipeline的VSBuild@1、DotNetCoreCLI@2任务或替代MSBuild@1的自定义脚本完成编译发布均失败,具体问题如下:

VSBuild任务失败

初始尝试VSBuild@1任务时,管道无法启动,报错:

##[Error 1]
No agent found in pool My_Pool which satisfies the specified demands:
     agent.name -equals My_Agend_Unity_1
     Cmd
     msbuild
     visualstudio
     Agent.Version -gtVersion 2.153.1 

原因是Azure DevOps 2019与VS2022存在兼容性问题,代理无法识别VS2022,未创建对应系统能力,MSBuild@1任务也存在相同问题,因此尝试自定义脚本绕过。

DotNetCoreCLI任务失败

首次报错:

error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the expression in the Import declaration "\Microsoft.Cpp.Default.props" is correct, and that the file exists on disk.

添加env变量修复后:

env:
  PATH: 'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170'

又出现新错误:

##[error]Error: Unable to locate executable file: 'dotnet'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.

尝试使用UseDotNet@2任务后,仍出现类似首次的错误:

MSBuild version 17.3.2+561848881 for .NET
C:\agent\_work\2\s\XXX\YYY\CPPWrapper\MyProject.vcxproj : warning NU1503: Skipping restore for project "C:\agent\_work\2\s\XXX\YYY\CPPWrapper\MyProject.vcxproj". The project file may be invalid or missing targets required for restore. [C:\agent\_work\2\s\XXX\YYY\MySolution.sln]
  Determining projects to restore...
  "C:\agent\_work\2\s\XXX\YYY\DotNet6Project\MyProject.csproj" restored (in "2,4 sec").
C:\agent\_work\2\s\XXX\YYY\CPPWrapper\MyProject.vcxproj(21,3):error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the expression in the Import declaration "\Microsoft.Cpp.Default.props" is correct, and that the file exists on disk.
C:\agent\_work\2\s\XXX\YYY\CPPWrapper\MyProject.vcxproj(21,3): error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the expression in the Import declaration "\Microsoft.Cpp.Default.props" is correct, and that the file exists on disk.
##[error]Error: The process 'C:\agent\_work\_tool\dotnet\dotnet.exe' failed with exit code 1
##[error]Dotnet command failed with non-zero exit code on the following projects : C:\agent\_work\2\s\XXX\YYY\MySolution.sln
##[section]Finishing: Build & Publish XXX Service - DotNetCoreCLI@2

MSBuild自定义脚本失败

使用已在其他管道中验证过的自定义脚本调用VS22的MSBuild,编译看似正常,但最终报错:

(ResolvePackageAssets Target) -> C:\Program Files\dotnet\sdk\7.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5):
error NETSDK1064: Package "Microsoft.EntityFrameworkCore.Analyzers",
Version 6.0.4, not found. It may have been deleted after the NuGet restore.
Otherwise, the NuGet restore may have been only partially completed due to limitations on the maximum path length.
[C:\agent\_work\2\s\XXX\YYY\DotNet6Project\MyProject.csproj]

已通过Group Policy Editor→Administrative templates→All Settings→Enable Win32 long paths开启长路径,仍无法解决问题。

附YAML配置

pool:
  name: 'My_Pool'
  demands:
  - agent.name -equals My_Agent

variables:
  buildPlatform: 'x64'
  buildConfiguration: 'Release'
  solution: '$(System.DefaultWorkingDirectory)/XXX/YYY/MySolution.sln'
  DotNet6Project: '$(System.DefaultWorkingDirectory)/XXX/YYY/DotNet6Project/MyProject.csproj'
  CPPWrapper: '$(System.DefaultWorkingDirectory)/XXX/YYY/CPPWrapper/MyProject.vcxproj'

steps:
- task: NuGetToolInstaller@0
  displayName: 'NuGet Tool Installer - NuGetToolInstaller@0'
  name: 'NuGetToolInstaller'
  inputs:
    versionSpec: '>=6.1.0'

- task: NuGetCommand@2
  displayName: 'NuGet Restore - NuGetCommand@2'
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    noCache: true

- task: BatchScript@1
  displayName: 'Run BatchScript to create DLLs, Libs & Header - BatchScript@1'
  inputs:
    filename: '$(System.DefaultWorkingDirectory)/ICP/ZZZ/build_release.bat'
  env:
    PATH: 'C:\Program Files\CMake\bin'

- task: PowerShell@2
  displayName: 'Run Powershell Script to unpack Packages from BatchScript for ZZZWrapper - PowerShell@2'
  inputs:
    filePath: '$(System.DefaultWorkingDirectory)/XXX/YYY/CPPWrapper/install_ZZZ_package.ps1'

# Workaround for MSBuild@1
- script: |
    @echo off
    setlocal enabledelayedexpansion
    for /f "usebackq tokens=*" %%i in (`"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe`) do (set msbuild_exe=%%i)
    "!msbuild_exe!" "$(solution)" /p:Configuration="$(buildConfiguration)" /p:Platform="$(buildPlatform)" /p:PackageLocation="$(build.artifactStagingDirectory)" /t:rebuild
  displayName: 'Build - Script'

# ---------- VSBuild ----------------
#- task: VSBuild@1
#  inputs:
#    solution: '$(solution)'
#    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
#    platform: '$(buildPlatform)'
#    configuration: '$(buildConfiguration)'

# ---------- DotNetCoreCLI ----------
#- task: UseDotNet@2
#  inputs:
#    packageType: 'sdk'
#    version: '6.x'

#- task: DotNetCoreCLI@2
#  displayName: 'Build & Publish - DotNetCoreCLI@2'
#  inputs:
#    command: 'publish'
#    publishWebProjects: false
#    projects: '$(solution)'
#    arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
#    zipAfterPublish: false
#  env:
#    PATH: 'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Build Artifacts - PublishBuildArtifacts@1'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'XXXArtifact'
    publishLocation: 'Container'

内容的提问来源于stack exchange,提问作者Perazim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:10:38