Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong PHP Version/Executable in VSCode terminal but works perfectly in Mac terminal

I just updated my Mac M1 to Big Sur 11.5.2 and something in VSCode seems to have broken. I am unable to use the latest home-brew php which is installed.

In VSCode its pointing to /usr/bin/php which is Macs built in php, that's not the one im using with home-brew. I tried everything and changed the path but still the same thing.

I checked the one similar question to mine and all it suggests is to use Homebrew which I already am doing so Im not sure what I am doing wrong here.

I am running PHPUnit tests in the VSCode terminal and I am getting the following error:

/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 ->

However when I run the same thing in the Mac terminal by going to the same folder it works perfectly:

/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 -> 

When I do which php in both terminals I get a different result:

In Mac terminal:

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

In VSCode terminal:

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

How can I point VSCode to the right version of PHP? I don't think this happened before doing the Big Sur update and I am not sure what to edit.

I tried to open settings.json but I can't find any info about this and I am not sure how to edit this.

Any advice would be appreciated.


Edit 1:

I think this issue happened since I installed PHPIntellisense on VSCode but can't be sure, what I do know is that it was working before. I dont know how to configure VSCode to point to the home-brew PHP which is already installed and working perfectly in the regular terminal


Edit 2

I tried to edit settings.json and it made no difference:

{
    "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"
}

Edit 3

I have completely uninstalled VSCode and reinstalled everything. To start from fresh and the same problem happens. This is what the current settings.json file looks like:

{
    "workbench.colorTheme": "Default Dark+",
    "php.validate.executablePath": "/opt/homebrew/bin/php"
}

When I do which php in the terminal in VSCode I always get the same thing /usr/bin/php

The same problem is continuing, Im not sure if this is because of the OSX update.

How could I configure the VSCode terminal to be exactly like the Mac terminal?

I also opened another project where I am using Symfony and I am trying to create a basic controller & that doesn't work too. I have edited the question to be more generic now:

php bin/console make:controller test I get the following error:

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)

What is most frustrating is this seems so straight forward I can't understand what I need to do to fix this

like image 532
theMyth Avatar asked Aug 23 '21 21:08

theMyth


People also ask

How do I run PHP code in Visual Studio Code?

Configuring VS Code To Run PHP Code. So, we’ve got everything together and VSCode open, let’s add the settings. 1. Go to File -> Preferences -> Settings. 2. Add the following JSON to the User Settings on the Right Hand Side of the IDE. Replacing C:\xampp\php\ with your path to your php.exe.

Is there a command to skip the shell in VS Code?

There is a hardcoded list of commands, which skip being processed by the shell and instead get sent to the VS Code keybinding system. Customize this list with the terminal.integrated.commandsToSkipShell setting.

Where does VSCode point to on Mac?

In VSCode its pointing to /usr/bin/php which is Macs built in php, that's not the one im using with home-brew. I tried everything and changed the path but still the same thing.

How do I use the Visual Studio Code terminal?

Visual Studio Code includes a fully-featured integrated terminal that conveniently starts at the root of your workspace. It provides integration with the editor to support features like links and error detection. Use the Ctrl+` keyboard shortcut with the backtick character. Use the View > Terminal menu command.


1 Answers

I got the same problem. Open your terminal and write this:

nano ~/.zshrc

At the top of the file you have this:

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

Put this line just under:

export PATH=/opt/homebrew/opt/[email protected]/bin:$PATH

Save and close, restart your terminal and it will normally work.

NB: I write [email protected] but you can do this with all versions you install with homebrew

like image 181
Maxent Avatar answered Nov 28 '22 07:11

Maxent