Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidmatson opened this issue Apr 30, 2024 · 6 comments · May be fixed by #39216
Open

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

davidmatson opened this issue Apr 30, 2024 · 6 comments · May be fixed by #39216
Assignees
Labels
category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`)

Comments

@davidmatson
Copy link
Contributor

davidmatson commented Apr 30, 2024

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

  • OS: Windows 11
  • Compiler: VS 2022 Version 17.9.6

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
Copy link
Contributor

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. 🤔

@FrankXie05 FrankXie05 added the requires:more-information This Issue requires more information to solve label May 6, 2024
@davidmatson
Copy link
Contributor Author

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.

@FrankXie05 FrankXie05 added category:vcpkg-feature The issue is a new capability of the tool that doesn’t already exist and we haven’t committed and removed requires:more-information This Issue requires more information to solve labels May 10, 2024
@berryzplus
Copy link

I think the following code can serve as a temporary workaround.

    <!-- Search %PATH% for pwsh.exe if it is available. -->
    <Exec
      Condition="'$(VcpkgXUseBuiltInApplocalDeps)' != 'true'"
      Command="pwsh.exe $(_ZVcpkgAppLocalPowerShellCommonArguments) 2&gt;NUL"
      IgnoreExitCode="true"

$(_ZVcpkgAppLocalPowerShellCommonArguments) may generate output.
Since we only want to ignore the error output, 2>NUL is preferable to >NUL 2>&1.

May I make a pull request?

@berryzplus
Copy link

This issue may caused by 11610ea(#35012).

vcpkg.props defines $(VcpkgXUseBuiltInApplocalDeps) as false,
but noone defines $(VcpkgXUseBuiltInApplocalDeps) as true.

@berryzplus
Copy link

berryzplus commented Jun 10, 2024

@FrankXie05

The cause of the bug lies in the following part.

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

At line 229, it incorrectly calls pwsh.exe without checking if pwsh.exe is included in the %PATH%.

On Windows, when you want to check if a specific command is in the %PATH%, you can use the WHERE command.

By inserting the following code snippet before line 229, the bug can be resolved.

      Command="where.exe pwsh.exe &gt;NUL 2&gt;&amp;1"
      IgnoreExitCode="true"
      UseCommandProcessor="false">
      <Output TaskParameter="ExitCode"
              PropertyName="_ZSearchPwshInPathExitCode" />
    </Exec>
    <!-- Execute PowerShell Core commands to ensure compatibility with legacy code. -->
    <Exec
      Condition="'$(_ZSearchPwshInPathExitCode)' == '0'"
@JonLiu1993 JonLiu1993 added category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`) and removed category:vcpkg-feature The issue is a new capability of the tool that doesn’t already exist and we haven’t committed labels Jun 11, 2024
@JonLiu1993 JonLiu1993 linked a pull request Jun 11, 2024 that will close this issue
7 tasks
@shadowstormone
Copy link

At line 229, just rename from pwsh.exe to powershell.exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`)
5 participants