SanderRonde / phpstan-vscode

PHPStan plugin for VSCode
https://marketplace.visualstudio.com/items?itemName=SanderRonde.phpstan-vscode
MIT License
47 stars 8 forks source link

"Scan project" does not respect path mapping #27

Closed raustin-m closed 1 year ago

raustin-m commented 1 year ago

I'm using this extension in docker with Laravel Sail, and I've got a path mapping set up in my settings:

    "phpstan.paths": {
        "/home/austin/repos/project": "/var/www/html"
    },

This works fine for single file checks, but if I run PHPStan: Scan project for errors from the command palette I get back errors in VS Code for files that don't exist because the paths haven't been mapped back, they're still the /var/www/html/file.php format. This leads to errors in the Problems window that I can't jump to and can't ever clear.

I tried to poke around a little to see if I could fix this myself but there seems to be a lot going on here in this extension, if you could maybe point me to the right place to look I may be able to open a PR for this.

SanderRonde commented 1 year ago

Ah yeah I see where this is going wrong. Would be pretty nice if you can open a PR for this since it's not super trivial for me to set up a similar environment. If I did it it would be mostly me trying and asking you to test it.

It's going wrong on this line. The returned fs-path to errors object does not apply path mapping. You can find an example of the path mapping being applied over here (to the singular file scan). I think it should be as easy as just changing the key of the map to ConfigurationManager.applyPathMapping(this._config, URI.from({ scheme: 'file', path: filePath, })).toString().

raustin-m commented 1 year ago

@SanderRonde Thanks for the pointer! I had a bit of an issue implementing your snippet directly due to applyPathMapping() being async while where I needed to use it was in a sync function, but I think I solved that alright. Let me know on the PR if you have any issues and I can make adjustments.

raustin-m commented 1 year ago

Also, I should note that in order for this to work I actually had to add the reverse mapping to my paths as well:

    "phpstan.paths": {
        "/home/austin/repos/project": "/var/www/html",
        "/var/www/html": "/home/austin/repos/project"
    },

If I don't have the first one, my config file can't be loaded in the container, and without the second, my errors don't get mapped correctly on the way back out. I think this is fine for me, but thought I should make note of the behavior.