laravelvisual-studio-codecomposer-phppsr-4intelephense

VsCode composer psr-4 autoload of external libraries does not perform code completion


I have a self-made shared library that I am including in my Laravel project.

This is how the autoload part in my composer.json looks like:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "MyLib\\Base\\": "../../../.libraries/my-lib/packages/MyLib/Base/src/" <- NOTE THIS
        }
    },

I cannot autocomplete the names of the classes (or anything) when I am typing the code.

I wrote down use MyLib\Base\Model\User; in my code and then tried to use User model and it worked perfectly, but the code keep showing the error Undefined type 'MyLib\Base\Model\User'. and highlights it with red underline as an error.

Therefore I understand that the intelliphense is not able to recognize the shared library classes although they work fine!

Any way to get around this?


Solution

  • Found this solution somewhere while I was trying a lot of things and this worked!

    Just add this below json value in .vscode/settings.json file:

    {
        "intelephense.environment.includePaths": [
            "../../../.libraries/my-lib/packages/MyLib/Base/src/"
        ]
    }
    

    Not only the error disappears, but also the code completion starts to work!