0

This post will be a bit longer, as I not only describe my problem, but also show my different attempts to solve the problem.

I have a solution contaning .Net-6-Web-Api-Project (csproj) and a C++/CLI-Wrapper-Project (vcxproj). I have a reference from the C#-Project to the c++-Project. I use DevOps 2019 and VS22 on my local building agent.

I'm not able to successfully run this solution through an Azure DevOps Pipeline using the task DotNetCoreCLI@2, VSBuild@1 or a custom script as a workaround for the MSBuild@1 to publish.

VSBuild
My initial approach was to simply use the VSBuild@1 task. Using this task does not allow the pipeline to start, with the following error:

##[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 

The cause is the compatibility issue between DevOps 2019 and VS2022. The agent does not recognize VS2022 and therefore does not create system capabilities for it. Its the same issue for the MSBuild@1 and why I tried a custom script to work around, because it couldn't find MSBuild.

DotNetCoreCLI
The first error I got was:

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.

So I fixed that by adding the env variable to the task:

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

The resulting further error was:

##[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.

So I tried to fix it by using the Task UseDotNet@2, even though it doesn't make sense to me. But at the end I still get an error similar to the first error.:

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
My last hope then was my custom script that I already use in another pipeline that accesses the same agent and uses MSBuild from VS22. This is the approach I've come furthest with, as it looks like the project builds fine, but then fails because of this error.

 (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]

How to proceed with it, I do not know right now. I enabled already long paths via
Group Policy Editor→Administrative templates→All Settings→Enable Win32 long paths.

My yaml file:

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'
Perazim
  • 1,501
  • 3
  • 19
  • 42

1 Answers1

1

From your yaml file ,I understand you want to build a solution and then publish build artifact. Accroding the error of your described task, I would like to provide suggestions that you can check.

1 VSBuild@1

taskError information: No agent found in pool My_Pool which satisfies the specified demandsThis indicates there are no agents on your machine that meet the demand requirements. You should check whether exists the agent which name is My_Agend_Unity_1 and exists check for Cmd,msbuild,visualstudio,Agent.Version -gtVersion 2.153.1.

See more information refer to doc:pool definition

2  UseDotNet@2

There’s a warning warning NU1503: Skipping restore for project that indicates  the packages required for the project MyProject are not restored correctly. You should edit the affected project to add targets for restore.

Please refer to doc:NuGet Warning NU1503

About the error MSB4019,you should check whether the project path "C:\Microsoft.Cpp.Default.props" exists. Here’s a ticekt  similar to your issue.You can try to this workaround and see if it works.

3 MSBuild

MSBuildAbout error NETSDK1064, this error occurs when the build tools can't find a NuGet package that's needed to build a project. This is typically due to a package restore issue related to warning NU1503 inTask   UseDotNet@2`. You can refer this doc:NETSDK1064: Package not found to take some actions provided to resolve this error.

Dou Xu-MSFT
  • 880
  • 1
  • 1
  • 4
  • Thanks for your answer. I must honestly confess that I did not think that these suggestions will help me. However, you solved my problem with the last suggestion. Adding the command "/restore" solved the problem. I don't understand why though. Because as seen in my yaml file, I am already doing a nuget restore task. So doing twice the restore results in a successfull build :) – Perazim Jan 21 '23 at 20:51
  • Hi, glad to know the last suggestion help you solve your problem. You can check whether this nuget restore task you already added restore package successfully. – Dou Xu-MSFT Jan 26 '23 at 01:38