vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Indexing problem - `Undefined type` errors after using something from composer (without reloading vscode)

Open kanlukasz opened this issue 4 years ago • 18 comments

To Reproduce

  1. Init empty project
  2. Create file e.g. test.php
  3. Run composer require phpmailer/phpmailer
  4. Paste PHPMailer simple example into test.php
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '[email protected]';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Expected behavior This error should disappear because the namespaces are correct I need to reload VSCode so that these errors disappear

Screenshot without reloading obraz

Screenshot after reloading obraz

Platform and version

Version: 1.45.1 (system setup)
Commit: 5763d909d5f12fe19f215cbfdd29a91c0fa9208a
Date: 2020-05-14T08:27:35.169Z
Electron: 7.2.4
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.18363

kanlukasz avatar Jun 02 '20 15:06 kanlukasz

Do you need to reload or just close/ reopen file or edit it for them to disappear?

bmewburn avatar Jun 02 '20 23:06 bmewburn

I need reload Close / reopen file or edit doesn't help

kanlukasz avatar Jun 03 '20 06:06 kanlukasz

Undefined type 'PHPUnit\Framework\TestCase'.intelephense(1009) even after reload

edit: 1.4.1 seems to be indexing it fine

rilex037 avatar Jun 10 '20 11:06 rilex037

I tested with 1.4.1. The problem still exists. Nothing has changed

kanlukasz avatar Jun 16 '20 05:06 kanlukasz

I have similar problems with it, sometimes it works sometimes it doesn't. image image image image image I have no idea what that might be. I have tried restarting vs code, closing the file, making changes, even changing the php version.

mreduar avatar Sep 01 '20 18:09 mreduar

I've just run across this exact same problem for the first time. Previous composer updates have been fine but this time I suddenly got Undefined Type errors for a vendor package that previously worked fine.

image

matt-gribben avatar Nov 11 '20 21:11 matt-gribben

Same here. Not sure if it matters, in my case "vendor" folder is below the workspace.

image

image

bmarotta avatar Dec 01 '20 17:12 bmarotta

Dunno if the problem is related but I'm facing the same issue even with something as simple as a FileException catch.

Screenshot from 2021-02-22 15-14-52

fbnlsr avatar Feb 22 '21 14:02 fbnlsr

Still present in 1.7.1.

juanferrer avatar Sep 26 '21 11:09 juanferrer

My error gone after restart

Chandan-Kalita avatar Oct 31 '21 11:10 Chandan-Kalita

I just realised that I have the same issue in v 1.63 of vs code, v1.80 of the extension

I recently switch from phpstorm to vs code and did not really managed to figure out if this behaviour was vs code of this plugin, buy two day ago I reinstalled my mac and now I only have this extension in VSCode.

I'm using Laravel and the ide-helper, not sure if that an issue or an benefit

The issue disappears after restart of vscode, but it is not convenient to restart several times

rabol avatar Dec 12 '21 08:12 rabol

Same problem. VS Code: 1.63.0 PHP Intelephense v1.8.0 Peek 2021-12-14 14-47

Restarting doesn't solve unfortunately.

Stardog avatar Dec 14 '21 19:12 Stardog

I've encountered the same problem. What worked for in VS Code me was this:

Bildschirmfoto 2022-04-28 um 20 32 59

So open the command palette and make intelephense re-index the workspace.

nxcco avatar Apr 28 '22 18:04 nxcco

Thanks @nxcco this helped. and make sense in my case since I updated the composer content while vsCOde was opened... Maybe a trigger missing to tell intelephese about external update of the source base?

Stilmant avatar May 04 '22 08:05 Stilmant

There should be a filewatcher that detects the addition and removal of files. Does anyone have vendor files excluded in the files.watcherExclude setting?

bmewburn avatar May 11 '22 04:05 bmewburn

Closing VS Code and Re-Opening it will solve this issue.

osik1 avatar Aug 05 '22 14:08 osik1

There should be a filewatcher that detects the addition and removal of files. Does anyone have vendor files excluded in the files.watcherExclude setting?

I have the default setting and have never changed it: image

kanlukasz avatar Aug 05 '22 14:08 kanlukasz

Closing VS Code and Re-Opening it will solve this issue.

It doesn't fix anything, it's a workaround (which is inconvenient anyway)

kanlukasz avatar Aug 05 '22 14:08 kanlukasz

I found that adding "vendor" to files.exclude in .vscode/settings.json would prevent type acquisition from Composer packages. Also, make sure to restart VSCode every time you do something to test whether it fixes the problem - you can't trust VSCode to reload types on the fly it seems.

Jemt avatar Nov 11 '22 06:11 Jemt

For me I installed some PHP Extensions like PHP and PHP Intellisense, Extension Pack, etc, and restarted VS code, the error was gone

kaustubhya avatar Dec 27 '22 10:12 kaustubhya

tracking composer issue in #2213

bmewburn avatar Jan 14 '23 07:01 bmewburn