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

Azure DevOps中MSBuild调用mt.exe遇‘非根路径’错误的解决咨询

问题:Azure DevOps中运行mt.exe出现"not a rooted path"错误

问题背景

我在Azure DevOps中创建了MSBuild流水线,试图用mt.exe处理清单文件,流水线配置如下:

trigger:
- develop

pool:
  vmImage: windows-latest

steps:
- task: MSBuild@1
  inputs:
    solution: '**/*.csproj'

对应的mytool.csproj仅定义了MT任务:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />
  <Target Name="Build">
        <MT Sources="mytool.manifest" OutputResourceManifests="mytool.exe"></MT>
  </Target>
</Project>

流水线运行时抛出错误:

Build:
  mt.exe /outputresource:"mytool.exe" /manifest mytool.manifest
##[error]mytool.csproj(7,9): Error MSB4018: The "MT" task failed unexpectedly.
Microsoft.Build.Shared.InternalErrorException: MSB0001: Internal MSBuild Error: mt.exe unexpectedly not a rooted path
   at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Object[] args)
   at Microsoft.Build.Shared.ErrorUtilities.VerifyThrowInternalRooted(String value)
   at Microsoft.Build.CPPTasks.TrackedVCToolTask.TrackerExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands)
   at Microsoft.Build.CPPTasks.TrackedVCToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands)
   at Microsoft.Build.Utilities.ToolTask.Execute()
   at Microsoft.Build.CPPTasks.VCToolTask.Execute()
   at Microsoft.Build.CPPTasks.TrackedVCToolTask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

已通过where mt.exe确认mt.exe存在于C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64。

错误含义解释

mt.exe unexpectedly not a rooted path指MSBuild无法获取mt.exe的绝对根路径(带盘符的完整路径)。尽管mt.exe确实存在,但Azure DevOps的构建环境中,Windows SDK和VC工具链的环境变量未被正确加载,导致MT任务只能找到mt.exe这个文件名,而非完整路径,触发MSBuild的内部校验错误。

解决方案

方案1:修复MSBuild项目配置

修改mytool.csproj中的MT任务,显式指定mt.exe的工具路径,可使用环境变量保证兼容性:

<Target Name="Build">
  <MT 
    Sources="mytool.manifest" 
    OutputResourceManifests="mytool.exe"
    ToolPath="$(WindowsSdkDir)bin\$(WindowsSDKVersion)\x64"
  ></MT>
</Target>

同时,在流水线中添加步骤初始化VC开发环境,确保WindowsSdkDir和WindowsSDKVersion变量被正确设置:

steps:
- task: CmdLine@2
  inputs:
    script: '"%ProgramFiles(x86)%\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"'
- task: MSBuild@1
  inputs:
    solution: '**/*.csproj'

方案2:直接用脚本调用mt.exe(非MSBuild方式)

跳过MSBuild,直接在流水线中用CmdLine或PowerShell任务执行mt.exe:

CmdLine版本

- task: CmdLine@2
  inputs:
    script: |
      rem 自动查找mt.exe路径
      for /f "delims=" %%i in ('where mt.exe') do set "mtPath=%%i"
      echo Found mt.exe at: %mtPath%
      "%mtPath%" /outputresource:"mytool.exe" /manifest mytool.manifest

PowerShell版本

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $mtPath = (Get-Command mt.exe).Source
      Write-Host "Found mt.exe at: $mtPath"
      & $mtPath /outputresource:"mytool.exe" /manifest mytool.manifest

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 15:05:25