I am trying to write a pants plugin in vscode and ...
# general
q
I am trying to write a pants plugin in vscode and the python extension can't detect where
pants.*
imports come from and keeps giving me a warning. I can add
# type: ignore
next to the imports to suppress the warning but that doesn't seem like a good solution. Is there a better way to fix this? Such as how we detect First Party sources?
b
q
Yes I have this in the BUILD file inside
pants-plugins/
This is what I mean: It doesnt detect these modules and the IDE complains
b
Does exporting a venv for third party sources work?
q
Could you explain what you mean by that?
I cloned the Example-python repo by pantsbuild
and that uses pytests as a 3rd party dependency. That also gives a IDE issue when I try to import it
it works fine when I run/package it with pants. It just gives an issue on the IDE
same thing with any
pants.*
modules
that I import in
pants-plugins/*
files
b
Sorry, I’m on mobile so a bit brief. The page you linked about first party sources also has section on third party dependencies.
q
Yes I have tried that and it doesn't change anything
I still get the same issue
b
Can you describe exactly what you tried? I understand this is frustrating and sorry for the trouble, but remote support is hard without all the information.
q
No Problem. So I tried to run
pants export --py-resolve-format=symlinked_immutable_virtualenv --resolve=python-default
which is what the link says to do for third party dependencies. I am working with the
example-python
demo repo which has a BUILD file in the root directory which has
python_requirements(name="reqs")
which takes all the packages mentioned in the
requirements.txt
file and creates a
python_requirement
target (as stated in the docs).
The IDE is able to pick up first party packages after I follow what the link says. However, it doesn't work for the third party libraries
also, now I am getting another error when I try to run
pants generate-lockfiles
:
b
After running export, did you tell your editor how to find the resulting venv in
dist/
.? It’s not a conventional path like
venv
or
.venv
so may not be picked up automatically
For that error, ensuring the plugins are in their own resolve, separate to your use code, is probably the best path. This stops the pants requirements from interfering with your real code. The link above discusses one way to do this, if you aren’t familiar
q
No, I didn't do that (point the IDE to the export). Is there any steps available to show how to do this in vscode?
Hi. Quick update. so once I run the
export
command, I get a path and if I put that path on vscode's pylance's settings under
Extra Paths
it works - but only for 3rd party libraries. When I write my own plugin and import from
pants.*
It doesn't recognize those imports
b
I'm not sure of the details of VS code, but seems like you're getting good answers over in https://pantsbuild.slack.com/archives/C01CQHVDMMW/p1718658445384289 , so I'll step out! 🙂
q
Yes, thank you! I still trying that out but thank you for your help!