microsoft / vcpkg

C++ Library Manager for Windows, Linux, and MacOS
MIT License
21.76k stars 6.05k forks source link

vcpkg.targets outputs error message in success case for pwsh.exe search #38502

Open davidmatson opened 2 weeks ago

davidmatson commented 2 weeks ago

Describe the bug When building using scripts\buildsystems\msbuild\vcpkg.targets and using Windows PowerShell (rather than PowerShell Core), the build outputs an error message when it actually succeeds.

Environment

To Reproduce Steps to reproduce the behavior:

  1. Build a vcxproj that imports scripts\buildsystems\msbuild\vcpkg.targets on a system without PowerShell Core installed.
  2. See the Build output window in VS.

Expected behavior No error message appears.

Additional context I suspect the code here is the root cause:

    <!-- Search %PATH% for pwsh.exe if it is available. -->
    <Exec
      Condition="'$(VcpkgXUseBuiltInApplocalDeps)' != 'true'"
      Command="pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments)"
      IgnoreExitCode="true"
      UseCommandProcessor="false">
      <Output TaskParameter="ExitCode"
              PropertyName="_ZVcpkgAppLocalExitCode" />
    </Exec>

Updating that code to use something like

      Command="cmd /c pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments) >NUL 2>&1"

appears likely to fix the problem.

FrankXie05 commented 1 week ago

the build outputs an error message when it actually succeeds.

@davidmatson Thanks for posting this issue, could you please provide me with the error log you output?

Command="cmd /c pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments) >NUL 2>&1"

This will only cover up the error but not fix it. 🤔

davidmatson commented 1 week ago

the build outputs an error message when it actually succeeds. @ davidmatson Thanks for posting this issue, could you please provide me with the error log you output?

Sure; the error message is simply:

'pwsh.exe' is not recognized as an internal or external command,

Command="cmd /c pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments) >NUL 2>&1" This will only cover up the error but not fix it. 🤔

I think the problem is just that the current command attempts to invoke a program that may or may not exist, and then uses the exit code to tell whether it did or not, but that command will naturally error out if the program doesn't exist. If the command is intended just to see if it will fail or not, having the "error" message output isn't great.

Another option would be to use where:

Command="cmd /c where pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments) >NUL 2>&1"

But redirecting stderr is still necessary; where also outputs text to stderr if the program does not exist. If the goal is to determine whether the command exists, I don't think suppressing error output hides the problem; the problem is the spurious error message in build logs.