Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Unify" fmt and lint rules for formatters #14903

Merged
merged 11 commits into from
Mar 30, 2022

Conversation

thejcannon
Copy link
Member

(proposal: https://docs.google.com/document/d/1KqrPP6VVi-Kq8EUqfo2IFffaHxdysCMPwrInoewM-LM/edit?usp=sharing)

This PR makes changes to lint.py to create FmtRequests for the purpose of linting, coercing the FmtResult into LintResult.

The core changes:

  • lint.py now constructs FmtRequests in addition to the normal lint request types, and coerces the FmtResult into a LintResult
  • FmtResult's message is now a simple tabbed list of files that changed. This will be used for both fmt and lint.
  • The FmtResult stdout and stderr are now debug logged. Unfortunately there's some out-of-order logging, but I think this is OK. Most users don't care.

The formatter plugin changes:

  • Removing the lint rules for formatters 🎉
  • Which in turn removes the check_only which elides the SetupRequest in each formatter
  • Elided some argv generation

This does not fix the issue pointed out in #14489 we can solve that later.


Beofre and after (using 4 cores)

Benchmark 1: ./pants --process-execution-local-parallelism=4 --no-pantsd --no-local-cache fmt lint ::
  Time (mean ± σ):     40.734 s ±  0.456 s    [User: 106.734 s, System: 13.017 s]
  Range (min … max):   40.375 s … 41.519 s    5 runs
Benchmark 1: ./pants --process-execution-local-parallelism=4 --no-pantsd --no-local-cache fmt lint ::
  Time (mean ± σ):     34.397 s ±  0.407 s    [User: 74.738 s, System: 9.009 s]
  Range (min … max):   33.987 s … 35.071 s    5 runs

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@thejcannon
Copy link
Member Author

Commits are useful in order

Comment on lines +57 to +58
if request.prior_formatter_result is None
else request.prior_formatter_result
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future change I think I'll refactor so this isn't needed. It's so confusing 😵

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please!! I can't remember why this was necessary. But it's so copy pasta now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR Because these rules run for fmt (in which case the formatter should run on the prior result) and lint (in which case there is no "prior formatter result")

@thejcannon
Copy link
Member Author

Also, it'd be a breaking user change so likely not worth it, but I wish the formatters were in a fmt directory in-repo 😞

if isinstance(batch_results, FmtResult):
return (
LintResult(
1 if batch_results.did_change else 0,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bummer we have to invent an error code here 😒

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
Copy link
Member

@stuhood stuhood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! This is a great cleanup.

src/python/pants/core/goals/fmt.py Outdated Show resolved Hide resolved
src/python/pants/core/goals/fmt.py Show resolved Hide resolved
@thejcannon
Copy link
Member Author

As a follow-up we can issue a deprecation warning if our lint and fmt requests share a type (e.g. remove your lint code)

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
@thejcannon
Copy link
Member Author

As a follow-up we can issue a deprecation warning if our lint and fmt requests share a type (e.g. remove your lint code)

Oh hey someone has already done that, indirectly 😂 AmbiguousRequestNamesError.

@Eric-Arellano
Copy link
Contributor

This does not fix the issue pointed out in #14489 we can solve that later.

How so? I don't think this is enough to block on, but is it solvable?

@thejcannon
Copy link
Member Author

This does not fix the issue pointed out in #14489 we can solve that later.

How so? I don't think this is enough to block on, but is it solvable?

Well this actually perpetuates that issue into lint, as the tools will be trying to write to disc for lint.

It'll need to be addressed, but my approach would just be to give a fallback message about the formatter failing to format a file, which implies a file would fail lint anyways. E.g. black attempted to write to disk but failed. Please run ./pants fmt to fix formatting.

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work. Not only really good code, but you did an excellent job with the proposal & google doc etc. So exciting 🎉

Comment on lines +57 to +58
if request.prior_formatter_result is None
else request.prior_formatter_result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please!! I can't remember why this was necessary. But it's so copy pasta now.

src/python/pants/core/goals/fmt.py Show resolved Hide resolved
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants