https://pantsbuild.org/ logo
#general
Title
# general
a

acoustic-librarian-3937

02/15/2022, 5:35 PM
Sorry if this has already been asked, but I am wondering how I can get linting to work in VS Code in a consistent way with the pants cli. In my pants.toml file, I have added some python specific linting backends like this
Copy code
[GLOBAL]
pants_version = "2.9.0"
backend_packages = [
    "pants.backend.python.lint.black",
    "pants.backend.python.lint.flake8"
]
How do I correctly set up my path such that VS code is able to find the executable for
black
and
flake8
to allow it to invoke those linters/formatters? I am creating a virtual environment the pre-export way but I don't see black or flake8 binaries in the bin directory for that venv
👋 1
h

hundreds-father-404

02/15/2022, 5:42 PM
Hi, welcome! Indeed, Pants does not yet(?) export tool lockfiles for tools like Black and Flake8. Relates to this thread https://pantsbuild.slack.com/archives/C0D7TNJHL/p1644397600652729 We're definitely eager to here about what you're looking for and to figure out how to make this more user-friendly
a

acoustic-librarian-3937

02/15/2022, 6:03 PM
Thanks for responding! I tried to read through that thread and the linked GitHub issue. I see what you mean that it could be weird for pants to install those tools into a user's virtualI environment automatically. I think in most cases, if the user has enabled the backend in pants, they want to use the tool so maybe in practice it is not too much of a concern. I think allowing users to specify that they want black as a dependency and then allowing pants to use my preferred version of that dependency when I run
./pants lint ::
would solve this for me. (I think that is what the GitHub issue is advocating for, correct me if I am misunderstanding it). I am coming from a JS background. In that world it is fairly common for folks to include an executable tool such as a formatter or linter as a dev dependency to allow the build system to use it. Ultimately it would be cool if I could click "format document" or "run linting" in VS Code, and know that it would run in the same way that
./pants fmt
or
./pants lint
would. For now, I am just running
pip install $tool
in that virtual env and hoping I get a compatible version with the same config.
h

hundreds-father-404

02/15/2022, 6:09 PM
yeah there are a couple of different angles we could take, not sure which we should pursue: 1. Let you reuse the same
python_requirement
for both your own normal code and the tool lockfile, like
[black].version
and
[black].extra_requirements
. This would make it more feasible to include Black in the result of
./pants export ::
2. Export dedicated virtualenvs per tool, which would let you point your IDE to use the Black binary from that tool's venv, for example. Pretty feasible for us to implement 3. Mix in the tool venvs with your user requirements venv. The benefit is you have only one venv w/ everything in it, but the downside is that it's likely to cause issues trying to mix them. Not very feasible fwict 4. First-class VSCode support, which we're working on via BSP https://github.com/pantsbuild/pants/issues/13260. I wonder if that will allow us to wire into VSCode's fmt support automatically, not sure cc @nutritious-hair-72580
definitely #2 is the quickest for us to do. I'm only not certain the usefulness of it, which is where I'd love your feedback and others
n

nutritious-hair-72580

02/15/2022, 6:19 PM
I think #2 would be a useful first step assuming it would be part of
./pants export ::
(or at least optionally). Looks like both VSCode and PyCharm support custom paths for black, flake8, etc. I’d put in the docs the VSCode
settings.json
example config - e.g.
Copy code
{
  "python.linting.flake8Path": "./dist/tools(?)/..."
}
I’m not sure if/how the equivalent is done programmatically in PyCharm, but it’d be pretty easy to infer from VSCode settings as a start.
👍 2
h

happy-kitchen-89482

02/15/2022, 6:20 PM
I know very little about vscode, but ideally it would run linters by invoking
./pants lint
itself rather than trying to replicate what
./pants lint
does.
Is that not easy to set up?
What would vscode expect to do if the tool was a non-python standalone binary, for example?
n

nutritious-hair-72580

02/15/2022, 6:23 PM
I don’t think
./pants lint
will give you hint/problems in the GUI when things are wrong. The VSCode extension (I’m not sure if it’s in the one python extension or a separate flake8 one) handles that.
👍 2
PyCharm similarly, you would just want it to point at the right version of Flake8 / mypy - not run pants. But you might want to run pants pre-commit.
a

acoustic-librarian-3937

02/15/2022, 6:38 PM
re:
I know very little about vscode, but ideally it would run linters by invoking 
./pants lint
 itself rather than trying to replicate what 
./pants lint
 does.
Is that not easy to set up?
AFAIK it is handled by the python extension https://marketplace.visualstudio.com/items?itemName=ms-python.python which is maintained by Microsoft and touted as the "best" option for getting python development set up in VS Code. That extension has a list of linters it knows how to work with https://code.visualstudio.com/docs/python/linting#_specific-linters which enables the hints in the UI. As @nutritious-hair-72580 mentioned those linters can be configured to use a custom path
A custom path is generally unnecessary as the Python extension resolves the path to the linter based on the Python interpreter being used (see Environments). To use a different version of a linter, specify its path in the appropriate custom path setting. For example, if your selected interpreter is a virtual environment but you want to use a linter that's installed in a global environment, then set the appropriate path setting to point to the global environment's linter.
So I think option #2, giving the tool its own virtual environment would be fine for me
👍 2
Also just wanted to say that pants is really cool and it is super cool to see you using rust for some of it ❤️
🙏 1
💯 1
❤️ 2
h

hundreds-father-404

02/15/2022, 6:54 PM
Thanks Ryan!! If you ever want to dabble in Rust, we're happy to add you to reviews or help with any changes: https://www.pantsbuild.org/v2.10/docs/contributions-rust. A couple of us (including me) learned Rust via this project Blog post coming soonish going into more detail about how specifically we use Rust + Python. https://blog.pantsbuild.org/fast-incremental-builds-speculation-cancellation/ is a neat blog too
😮 1
😍 1
a

acoustic-librarian-3937

02/15/2022, 7:02 PM
I would love to be included on some reviews! I have a decent understanding of the borrow checker and basic rust, but I am definitely lacking real world project experience, so seeing how you folks use it would be very eye opening.
❤️ 1
this is my github user https://github.com/mulchy
h

hundreds-father-404

02/15/2022, 7:10 PM
Cool! If you have interest in code changes, https://github.com/pantsbuild/pants/issues/12863 and https://github.com/pantsbuild/pants/issues/13608 could be interesting. Happy to add more thoughts if you're interested
a

acoustic-librarian-3937

02/15/2022, 7:36 PM
I might need some more info about https://github.com/pantsbuild/pants/issues/12863 but removing usages of
.unwrap()
seems like something I might be able to do without much help! I will clone the project and see what things look like
💯 1
h

happy-kitchen-89482

02/15/2022, 9:06 PM
I think the lesson here is that I need to switch from PyCharm to VSCode for a bit, so I know more about it...
a

acoustic-librarian-3937

02/15/2022, 9:13 PM
Ha I am thinking the opposite, my whole team uses PyCharm 🙂
n

nutritious-hair-72580

02/16/2022, 9:37 AM
I recently switched, only because the Python extension was using 100% CPU and I couldn’t figure out a fix. Pycharm seems a lot more helpful, especially with refactoring, but there are bits of VSCode I miss.
2 Views