When I run `./pants lint ::` everything is fine. B...
# general
b
When I run
./pants lint ::
everything is fine. But when I run pylint through VSCode using
python.linting.pylintPath=./dist/export/python/virtualenvs/tools/pylint/bin/pylint
, I get pylint's
impor-error
because the
tools/pylint
apparently doesn't understand my imports, despite
PYTHONPATH
being defined in
.env
. BTW, the
.env
looks fine as VSCode itself has no issue resolving those imports. Any idea how I could troubleshoot this further?
Let's imagine this is not an issue. Then I see another one: how is pylint supposed to be able to import dependencies installed in the python-default virtualenv? 🤔
So far, my understanding is that I would need to install pylint in python-default... Is there a better solution?
What I do no understand is how
./pants lint
works? (it does work fine!) If it uses
pylint
from the
tools/pylint
virtualenv, how can it have access to the dependencies installed in the
python-default
virtualenv?
@happy-kitchen-89482 @enough-analyst-54434 @bitter-ability-32190 any idea?
b
You're wise to try and have the pylint env ALSO have the puthon-default PYTHONPATH. I suspect that's the only way to make it work.
@hundreds-father-404 perhaps we could add a setting to have this already be the case for tool envs? You opt in and specify a resolve name (or it defaults) and we also tack on either additional installations or some equal magic (like a .pth file?)
b
What I do not understand is how does
./pants lint --lint-only=pylint ::
work? There must be a clean way to run pylint since that's working!
b
The lint command builds a little sandbox for the files it's running on using just pylint and the minimal set of requirements. It isn't quite a venv, since we use PEX for this
b
Could you point me to the piece of code doing that? I'd really like to find a way to make pylint work in VSCode!
b
It's, very very deep and likely won't suffice for your needs. Use --keep-sandboxes=always on your lint command after dirtying a file, it'll log the sandbox it will have leaked. You can poke around it to see exactly how the pylint process looks in terms of requirements
👍 1
The issue is pants uses a PEX file for constructing the execution environment, which VS Code doesn't understand
In general, we hope to have better editor support. But it's a large undertaking and nobody has "backed" it yet
b
My understanding is that VSCode just runs pylint and parses its output
b
That is true
You could get this working I think with one additional command. PEXs are like a single executable file* containing everything needed to run. To that end you could point VS Code to a PEX containing both pylint reqs and Python-default reqs. I wouldn't know the right incantation for that though
Instead of defining pylintPath can you specify a PYTHONPATH for pylint in vs code? You could also try specifying both venvs with a separator
b
PYTHONPATH is not enough, I"d also need each projet's own dependencies
I also tried to use a small wrapper script to run pylint through pants from pylint, but for some reason I don't understand it didnt work either:
Copy code
$ cat .vscode/scripts/pylint.sh 
#!/bin/sh -eu
env PANTS_PYLINT_ARGS="$@" -- ./pants lint --lint-only=pylint ::
b
So by PYTHONPATH I meant point it to the pylint and python-default venvs
🙏 1
💡 1
As for the script, I imagine it is because the pants output != The pylint output
b
Oh, thank you so much @bitter-ability-32190. It was that simple!
b
@hundreds-father-404 @happy-kitchen-89482 we'll have to keep this one in our back pocket for future users. It's a very easy way to editor support after running export
👍 2
b
In case it can help somebody else integrate pants with VSCode (or another IDE), here is my (awk-ish) script: https://github.com/pmuller/pants-example-monorepo/blob/main/scripts/setup-pants-ide-env.sh It generates a
.env
file with proper
$PATH
(all roots + all exported virtualenvs) and
$PYTHONPATH
(all exported virtualenvs). It also generates a
.vscode/settings.json
file to define the
defaultInterpreterPath
. Works well here!
🙏 1