visual-studio-codephpunitmacos-big-surapple-m1php-8

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


Solution

  • 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/php@8.0/bin:$PATH
    

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

    NB: I write php@8.0 but you can do this with all versions you install with homebrew