VSCode 终端中的 PHP 版本/可执行文件错误,但在 Mac 终端中运行良好

问题描述 投票:0回答:5

我刚刚将 Mac M1 更新到 Big Sur 11.5.2,VSCode 中的某些内容似乎已损坏。 我无法使用已安装的最新 home-brew php。

在 VSCode 中,它指向 /usr/bin/php,这是用 php 构建的 Mac,这不是我在 home-brew 中使用的那个。我尝试了一切并改变了路径,但仍然是同样的事情。

我检查了一个与我类似的问题,它建议的是使用我已经在做的 Homebrew,所以我不确定我在这里做错了什么。

我正在 VSCode 终端中运行 PHPUnit 测试,但收到以下错误:

/Users/themyth/App/Sites/MapFramework/map -> ./vendor/bin/phpunit tests                           
/usr/bin/php declares an invalid value for PHP_VERSION.
This breaks fundamental functionality such as version_compare().
Please use a different PHP interpreter.
/Users/themyth/App/Sites/MapFramework/map ->

但是,当我通过转到同一个文件夹在 Mac 终端中运行相同的操作时,它运行得很好:

/Users/themyth/app/Sites/MapFramework/Map -> ./vendor/bin/phpunit tests                                            
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.0.9
Configuration: /Users/themyth/App/Sites/MapFramework/Map/phpunit.xml

...R                                                                4 / 4 (100%)

Time: 00:00.004, Memory: 6.00 MB

There was 1 risky test:

1) tests\map\core\exception\MapExceptionTest::testDisplayMethodShowsBasicStaticHtml
This test did not perform any assertions

/Users/themyth/App/Sites/MapFramework/Map/tests/map/core/exception/MapExceptionTest.php:16

OK, but incomplete, skipped, or risky tests!
Tests: 4, Assertions: 4, Risky: 1.
/Users/themyth/app/Sites/MapFramework/Map -> 

当我在两个终端中执行 which php 时,我得到不同的结果:

在 Mac 终端中:

/Users/themyth/app/Sites/MapFramework/Map -> which php
/opt/homebrew/bin/php

在 VSCode 终端中:

/Users/themyth/App/Sites/MapFramework/map -> which php
/usr/bin/php
/Users/themyth/App/Sites/MapFramework/map -> 

如何将 VSCode 指向正确的 PHP 版本?我认为这在进行大苏尔更新之前不会发生,而且我不知道要编辑什么。

我尝试打开settings.json,但找不到任何有关此信息,并且我不知道如何编辑它。

如有任何建议,我们将不胜感激。


编辑1:

我认为这个问题是在我在 VSCode 上安装 PHPIntellisense 后发生的,但不能确定,我所知道的是它之前是有效的。 我不知道如何配置 VSCode 以指向已安装并在常规终端中完美运行的自制 PHP


编辑2

我尝试编辑settings.json,但没有什么区别:

{ "workbench.colorTheme": "Monokai Dimmed", "security.workspace.trust.untrustedFiles": "open", "redhat.telemetry.enabled": false, "php.validate.executablePath": "/opt/homebrew/bin/php", "php.executablePath": "/opt/homebrew/bin/php" }


编辑3

我已经完全卸载了 VSCode 并重新安装了所有内容。从头开始,同样的问题也会发生。 这就是当前的 settings.json 文件的样子:

{ "workbench.colorTheme": "Default Dark+", "php.validate.executablePath": "/opt/homebrew/bin/php" }
当我在 VSCode 的终端中执行 which php 时,我总是得到相同的结果 /usr/bin/php

同样的问题仍然存在,我不确定这是否是因为 OSX 更新。

如何将 VSCode 终端配置为与 Mac 终端完全相同?

我还打开了另一个项目,我正在其中使用 Symfony,并且我正在尝试创建一个基本控制器,但它也不起作用。 我现在已将问题编辑为更通用:

php bin/console make:控制器测试 我收到以下错误:

Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0". You are running 7.3.24-(to be removed in future macOS)
最令人沮丧的是,这看起来如此简单,我不明白我需要做什么来解决这个问题

visual-studio-code phpunit macos-big-sur apple-m1 php-8
5个回答
10
投票
我也遇到了同样的问题。打开你的终端并写下:

nano ~/.zshrc
在文件顶部有:

# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH
将此行放在下面:

export PATH=/opt/homebrew/opt/[email protected]/bin:$PATH
保存并关闭,重新启动终端即可正常工作。

注意:我写的是

[电子邮件受保护],但是您可以对使用自制程序安装的所有版本执行此操作


0
投票
您可能正在尝试使用 XDebug 来运行/调试您的代码。那么尽管做了上述的操作,还是不行!我必须编辑 launch.json 以添加“runtimeExecutable”(我在两个块中都做了),所以它看起来像这样(使用brew --prefix php找到正确的使用路径):

{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Launch currently open script", "type": "php", "request": "launch", "program": "${file}", "cwd": "${fileDirname}", "port": 0, "runtimeArgs": [ "-dxdebug.start_with_request=yes" ], "runtimeExecutable": "/opt/homebrew/bin/php", "env": { "XDEBUG_MODE": "debug,develop", "XDEBUG_CONFIG": "client_port=${port}" } }, { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9000, "runtimeExecutable": "/opt/homebrew/bin/php" } ] }
    

0
投票
您检查过用户设置settings.json吗?查找“php.executablePath”:“c:\php80\php.exe”,


0
投票
如果您使用 Mamp,只需选中此复选框

如果您没有使用 Mamp,请检查 ~/ 文件夹中的 .zshrc 文件


0
投票
如果您使用 XAMPP 而不是 Homebrew,请按照以下 2 个步骤操作: 1.nano~/.zshrc 2.导出 PATH=/Applications/XAMPP/bin:$PATH

© www.soinside.com 2019 - 2024. All rights reserved.