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

Refactor: DRY adhoc_tool code_quality_tool #20255

Merged
merged 7 commits into from
Dec 30, 2023

Conversation

gauthamnair
Copy link
Contributor

@gauthamnair gauthamnair commented Dec 2, 2023

Before moving to step 2 of the plan described in #17729 (comment) , cleaning up a gross duplication of rule code that I introduced in #20135 between adhoc_tool and the new code_quality_tool.

This PR extracts the shared logic into the concept of a ToolRunner and a rule to hydrate it in adhoc_process_support.

Both adhoc_tool and code_quality_tool have the latent idea of a tool runner and a considerable machinery to build it. Starting from something like

@dataclass(frozen=True)
class ToolRunnerRequest:
    runnable_address_str: str
    args: tuple[str, ...]
    execution_dependencies: tuple[str, ...]
    runnable_dependencies: tuple[str, ...]
    target: Target

they need to assemble things like locate the actual runnable by str and figure out what should be its base digest, args, env, etc. and also co-locate the execution and runnable dependencies. We now capture that information as a "runner":

@dataclass(frozen=True)
class ToolRunner:
    digest: Digest
    args: tuple[str, ...]
    extra_env: Mapping[str, str]
    append_only_caches: Mapping[str, str]
    immutable_input_digests: Mapping[str, Digest]

After this, adhoc_tool and code_quality_tool diverge in what they do with it. adhoc_tool uses this runner to generate code and code_quality_tool uses it to run batches of lint/fmt/fix on source files.

Food for thought ...

It should not escape our attention that this ToolRunner could also be surfaced as a Target, to be used by adhoc_tool and code_quality_tool rather than each specifying all these fields together. It would also help to reduce confusion when handling all the kinds of 'dependencies' arguments that adhoc_tool takes.

@gauthamnair gauthamnair added the category:internal CI, fixes for not-yet-released features, etc. label Dec 2, 2023
@huonw huonw requested a review from chrisjrn December 3, 2023 21:33
Copy link
Contributor

@lilatomic lilatomic left a comment

Choose a reason for hiding this comment

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

Looks good! I spotted that AdhocToolNamedCachesField doesn't seem to be ported over, though.

src/python/pants/backend/adhoc/adhoc_tool.py Show resolved Hide resolved
Copy link
Contributor

@huonw huonw left a comment

Choose a reason for hiding this comment

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

Nice one, thanks for waiting!

It should not escape our attention that this ToolRunner could also be surfaced as a Target, to be used by adhoc_tool and code_quality_tool rather than each specifying all these fields together. It would also help to reduce confusion when handling all the kinds of 'dependencies' arguments that adhoc_tool takes.

Yeah, I agree :) I think it matches with how I've noticed people interpreting adhoc_tool too: "here's a prepackaged command that could be invoked with pants run ... (or similar)". That is to say, it feels like that's potentially a very useful building block 👍

@huonw
Copy link
Contributor

huonw commented Dec 30, 2023

(I hit the "update branch" button to trigger a full CI rerun with latest code, since it's been so long, to avoid for "merge skew".)

@huonw huonw enabled auto-merge (squash) December 30, 2023 05:32
@huonw huonw merged commit 3ffbba3 into pantsbuild:main Dec 30, 2023
24 checks passed
huonw added a commit that referenced this pull request Feb 25, 2024
… to target location (#20581)

This PR fixes #20575 by adjusting the shared infrastructure for
`adhoc_tool` and `code_quality_tool` to resolve the
`execution_dependencies` field relative to the
`adhoc_tool`/`code_quality_tool` target location, rather than relative
to the `runnable=...` arg. I think this was a minor typo in #20255, and
we didn't have tests covering it.

I imagine it's common that the runnable and `..._tool` targets are often
in the same BUILD file (in which case the behaviour is the same either
way), but it's not impossible to have them be different (e.g. my work
repo has a a few shared runnable that are used by more than one
`adhoc_tool`). I think being relative to the target is easier to reason
about, and this was the old behaviour of `adhoc_tool`.

This PR also adds tests to confirm the behaviour of
`execution_dependencies` (including its relative-path resolution
behaviour), as well as the behaviour of `runnable_dependencies` while
I'm there.
WorkerPants pushed a commit that referenced this pull request Feb 25, 2024
… to target location (#20581)

This PR fixes #20575 by adjusting the shared infrastructure for
`adhoc_tool` and `code_quality_tool` to resolve the
`execution_dependencies` field relative to the
`adhoc_tool`/`code_quality_tool` target location, rather than relative
to the `runnable=...` arg. I think this was a minor typo in #20255, and
we didn't have tests covering it.

I imagine it's common that the runnable and `..._tool` targets are often
in the same BUILD file (in which case the behaviour is the same either
way), but it's not impossible to have them be different (e.g. my work
repo has a a few shared runnable that are used by more than one
`adhoc_tool`). I think being relative to the target is easier to reason
about, and this was the old behaviour of `adhoc_tool`.

This PR also adds tests to confirm the behaviour of
`execution_dependencies` (including its relative-path resolution
behaviour), as well as the behaviour of `runnable_dependencies` while
I'm there.
huonw added a commit that referenced this pull request Feb 26, 2024
… to target location (Cherry-pick of #20581) (#20608)

This PR fixes #20575 by adjusting the shared infrastructure for
`adhoc_tool` and `code_quality_tool` to resolve the
`execution_dependencies` field relative to the
`adhoc_tool`/`code_quality_tool` target location, rather than relative
to the `runnable=...` arg. I think this was a minor typo in #20255, and
we didn't have tests covering it.

I imagine it's common that the runnable and `..._tool` targets are often
in the same BUILD file (in which case the behaviour is the same either
way), but it's not impossible to have them be different (e.g. my work
repo has a a few shared runnable that are used by more than one
`adhoc_tool`). I think being relative to the target is easier to reason
about, and this was the old behaviour of `adhoc_tool`.

This PR also adds tests to confirm the behaviour of
`execution_dependencies` (including its relative-path resolution
behaviour), as well as the behaviour of `runnable_dependencies` while
I'm there.

Co-authored-by: Huon Wilson <huon@exoflare.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants