Hi folks! For those of you that are using VS Code ...
# plugins
q
Hi folks! For those of you that are using VS Code as your IDE, how do you configure it so that it detects imports from
pants.*
when you write custom plugins? I read the docs for setting up in an IDE but that is only for first party and third party plugins. It doesn't resolve this issue
w
Copy code
# In pants.toml
backend_packages = [
    "pants.backend.plugin_development",
    ...

[python]
enable_resolves = true
interpreter_constraints = ["==3.9.*"]
tailor_pex_binary_targets = false

[python.resolves]
pants-plugins = "build-support/lockfiles/pants-plugins.lock"
python-default = "build-support/lockfiles/python-default.lock"

[python.resolves_to_interpreter_constraints]
pants-plugins = [">=3.9,<3.10"]
Copy code
# In project/pants-plugins/BUILD
pants_requirements(name="pants", resolve="pants-plugins")
Copy code
pants generate-lockfiles --resolve=pants-plugins # This is important if you've upgraded your pants version
pants export --resolve=pants-plugins

Wrote mutable virtualenv for pants-plugins (using Python 3.9.19) to dist/export/python/virtualenvs/pants-plugins/3.9.19
⬆️ Point VS code's interpreter at that
I think it's out of date though? But it'll get you most of the way. I also have this in my
,vscode/settings.json
Copy code
{
  "python.analysis.packageIndexDepths": [
    {
      "name": "pants",
      "depth": 5,
      "includeAllSymbols": true
    }
  ]
}
Not sure if this is still required, but it helped out a while back
q
Hi
I tried this and it creates venvs in
dist/export/python/virtualenvs
i have 2 resolves in my project:
python-default
and
pants-plugins
. So I ran:
pants export --py-resolve-format=symlinked_immutable_virtualenv --resolve=python-default
So it has created two venvs for it
what do I do in the vs code settings to link this with Pylance so that the imports are detected?
This is what the venvs look like:
I added the following to
.vscode/settings.json
and it works for third-party dependencies, but it still doesn't work for
pants.*
imports when I make custom pants plugins
Copy code
{
    "python.analysis.extraPaths": [
        "/home/akoul5/dev/python/pants_test_goal/dist/export/python/virtualenvs/python-default/3.10.12/lib64/python3.10/site-packages",
        "/home/akoul5/dev/python/pants_test_goal/dist/export/python/virtualenvs/pants-plugins/3.10.12/lib64/python3.10/site-packages",
    ]
}
w
Screenshot 2024-06-18 at 15.26.01.png
q
image.png
When I select it, it still doesn't change the issue
w
I don't know what I'm looking at in that picture.
q
It is the path to the venv generated by
pants export
when i export both my resolves
w
I mean, what are you expecting to happen - and what is/isnt happening?
q
So it has created the venv for both the resolves. That is fine. But the imports are still not being resolved for
pants.*
imports. First and third-party imports are working
i got the third party imports working when i put this following path in my ExtraPaths settings for VS code's Pylance:
/.../pants_test_goal/dist/export/python/virtualenvs/python-default/3.10.12/lib64/python3.10/site-packages
When I do the same thing for the venv that was created by exporting the pants-plugin resolve, it doesn't fix the vs code imports
and thats because there are no pants files in the venv lib folder
w
whats in your plugin build file
q
pants_requirements(name="pants", resolve="pants-plugins")
w
And your
backend_packages
?
q
and this is my pants.toml file:
w
I would also add this:
Copy code
[python.resolves_to_interpreter_constraints]
pants-plugins = [">=3.9,<3.10"]
Im confused through, you're saying after you export, the pantslib is not there? e.g.
Copy code
source dist/export/.../bin/activate

pip freeze
pants isn't there?
q
so when I add the python.resolves_to.... in the pants.toml file, it gives me the following error because I dont have python 3.9.*:
w
Ah, okay, I wans't sure if that would work or not 🤷 Okay, but what happens when you source the venv and activate it?
no pants libs?
q
i can activate it
source dist/export/python/virtualenvs/pants-plugins/3.10.12/bin/activat
I ran this and I can activate it
but the imports are still not working lol
w
Right, but when you run
pip freeze
- are the pants libs there, as expected?
Copy code
pantsbuild.pants==2.21.0
pantsbuild.pants.testutil==2.21.0
q
no...
I just ran it. and there is no pantsbuild.*
or pants.*
w
Whelp. That's weird.
That was after a
Copy code
pants generate-lockfiles ::
pants export ::
?
q
let me run it again
ok, thats weird
now when i run generate-lockfiles
it gives me an error for pants-plugins
w
Is it the python 3.10?
q
I think so
image.png
i think it may be because I am using python 3.10. But what I dont understand is, I ran this command before and it made the two resolves
Hey, I finally got it to work. It was the Python 3.10 issue
I installed python 3.9 and just used it for the resolve interpreter constraints and it works now
w
👍
q
Thank you!