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

rapid-bird-79300

05/06/2021, 9:51 PM
When using
pyproject.toml
for
black
configuration, is there a way to run
fmt
only against the file patterns in the
include
attribute?
h

hundreds-father-404

05/06/2021, 10:08 PM
Hey Nick, I think that gets overridden because Pants specifies each file explicitly to run against What's your use case? Possibly relevant: There's a new feature in 2.5.0 that you can add skip_black=True to python_library and python_tests targets to opt out of Black. Altho that's a blocklist, not allowlist
r

rapid-bird-79300

05/06/2021, 10:11 PM
Hey Eric, yeah that's what I suspected. Our use case is to do a soft rollout of formatting on a mono repo. We would only like
fmt
to run a specific set of files described in the
include
regex. I'll bump to
2.5.0
give your suggestion a try. The block list should be fine for now.
h

hundreds-father-404

05/06/2021, 10:52 PM
Great! Yeah this feature was added for precisely this reason. You'll want to use the latest, 2.5.0rc0
r

rapid-bird-79300

05/06/2021, 11:04 PM
Okay this works thank you! However, I have to groom over a lot of different BUILD files to apply this change. It would be great if this could somehow be captured in the tailor command (e.g. apply
skip_black=True
by default). Or is there another solution for this you can recommend?
h

hundreds-father-404

05/06/2021, 11:09 PM
The solution we want to give is to merge metadata, so that (totally strawman design) you could set something like:
Copy code
{
  "**/*.py": {"skip_black": True},
  "some_specific_file.py: {"skip_flake8": True},
)
But that is an enormous project to re-envision targets, which we want to do but is on the backburner atm to focus on what users prioritized for 2021: https://groups.google.com/u/1/g/pants-devel/c/F8Saug3BrFw/m/ZtWDP4YmDwAJ In the meantime, unfortunately no 😕 You can use far fewer targets with
sources="**/*.py"
, but we've found that one target per directory usually works best. https://www.pantsbuild.org/docs/targets#target-granularity-for-first-party-code
👍 1
But that is an enormous project to re-envision targets, which we want to do
(the awkwardness of targets is I think my biggest qualm with Pants atm)
r

rapid-bird-79300

05/06/2021, 11:11 PM
okay got it and that sounds like a good long term solution. For now, could the filter goal somehow help here? Maybe grab targets by regex then pass those to the
fmt
goal directly?
h

hundreds-father-404

05/06/2021, 11:14 PM
Oh yeah, you could do that with
filter --address-regex
for example. But then you need to remember to run that specific command, and doing
./pants fmt ::
will do the wrong thing A goal of Pants is to empower you to move away from bash scripts like needing to run
./run_formatters.sh
for example, that
./pants fmt ::
should do The Right Thing
r

rapid-bird-79300

05/06/2021, 11:17 PM
Okay understood. The goal for our project is to have black run on all targets eventually but may not be out of the door. Let me give the filter command a shot and hopefully that should solve our use case for now (then just throw that away and just use
./pants fmt ::
once were ready)
👍 1
so the
--address-regex
via CLI doesn't like my regexes 😅. I see the tests here: https://github.com/pantsbuild/pants/blob/d9ec8acbe867299bda9465ff4200116fc39bbab9/src/python/pants/backend/project_info/filter_targets_test.py#L[…]3 how would the CLI command look for a regex on
dir
?
h

hundreds-father-404

05/06/2021, 11:41 PM
I suspect missing the quote?
./pants filter --address-regex='^src/python/pants/util' ::
r

rapid-bird-79300

05/06/2021, 11:42 PM
omg no I was missing
::
haha yes this works now thank you!
🤘 1
h

hundreds-father-404

05/06/2021, 11:46 PM
If you're interested, it'd be great to add a warning to
./pants filter
like we have with
./pants list
!
Copy code
❯ ./pants list
WARNING: No targets were matched in goal `list`.
It's 3 lines of code: https://github.com/pantsbuild/pants/blob/bcc5a8499a8230c08055b314f70fe5c60b5970dc/src/python/pants/backend/project_info/list_targets.py#L53-L55 We'd welcome a patch 🙂 Lmk, there are 2 tweaks I'd suggest we do
r

rapid-bird-79300

05/07/2021, 12:02 AM
Yes this is a great idea and I'd be more than happy to contribute!
🙌 1
h

hundreds-father-404

05/07/2021, 12:20 AM
Yay! So the two tweaks for
list_targets.py
are: 1. Rather than using
console.print_stderr()
, I think it makes sense to use
logger.warning()
. That's how we always do warnings with that
[WARN]
prefix. To do that,
import logging
and near top of the file set
logger = logging.getLogger(__name__)
2. The wording is not as clear as it should be imo. I thought what it was trying to get at is if you run
./pants list dir::
and there are no targets there, so the result is empty. But it turns out that that will error instead with
ResolveError
. So, I think the only way this error can trigger is you left off args. Maybe this (taken from what happens when you run
./pants test
)?
Copy code
17:07:13.09 [WARN] No files or targets specified.
You'll need to tweak the wording in
list_integration_test.py
. Then, copy and paste that code into
filter_targets.py
. Add a test
test_no_targets_specified()
to
filter_targets_test.py
(note the same as no targets matching). It looks like we currently assert that stderr was empty, which will need to be adjusted
Check out https://www.pantsbuild.org/docs/contributor-setup for getting set up with contributions
r

rapid-bird-79300

05/07/2021, 12:31 AM
Perfect, the requirements are clear and this is a good amount of information to get started. Let me get my machine setup for localdev and I can get started on this tomorrow!
🚀 1
6 Views