-
-
Notifications
You must be signed in to change notification settings - Fork 746
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
Add pants-plugins/schemas
to streamline regenerating contrib/schemas
#5847
Conversation
pants-plugins/schemas/rules.py
Outdated
], | ||
output_filename=f"{CMD}.pex", | ||
internal_only=True, | ||
main=EntryPoint.parse("st2common.cmd.{CMD}:main"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using st2common.cmd.generate_schemas:main
directly instead of using the st2-generate-schemas
script because pants can't run python with a -
in the name (because pants relies on importing from the target script, and python cannot import from files with a -
in the name).
Eventually I'll get around to plumbing a change in pants to make that possible (it already works with the underlying pex, but pants hasn't learned about that arg yet). In the meantime, we just use the entrypoint itself instead of the script.
pants-plugins/schemas
to improve simplify regenerating contrib/schemas
pants-plugins/schemas
to simplify regenerating contrib/schemas
340058f
to
d4fba56
Compare
1b0ce6f
to
3a9764a
Compare
3d90bcc
to
3fc8418
Compare
3fc8418
to
e0ee91f
Compare
pants-plugins/schemas
to simplify regenerating contrib/schemas
pants-plugins/schemas
to streamline regenerating contrib/schemas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Background
This is another part of introducing pants, as discussed in various TSC meetings.
Related PRs can be found in:
Overview of this PR
This PR improves the DX (developer experience) by wiring up the
fmt
andlint
goals to generatecontrib/schemas/*.json
if needed (or for lint, warn that it needs to be done).This includes creating a
schemas
plugin for pants that adds aschemas
target.Developer Experience
Today, if someone changes one of the files used to generate the schemas in
contrib/schemas
, they will probably forget to run the Makefile target the regenerates them:make schemasgen
.I've missed running that several times. If I'm lucky, CI complains with an error telling me to run that. I've seen CI logs print an error without actually failing the build. I believe that particular Makefile bug is fixed, but that anecdote highlights a usability issue: friction around generated files.
With this PR, we add schema generation into the
fmt
goal. This also adds it to thelint
goal which will tell people iffmt
needs to run to update any code (or in this case, regenerate the schema). Then, we only need simple instructions that say something like:Fine grained results cache
After someone runs
./pants fmt ::
once, they will benefit from the pants' results cache. For the schemas plugin, here's what that means:First, the
schemas
target incontrib/schemas
depends onst2common/bin/st2-generate-schemas
:st2/contrib/schemas/BUILD
Lines 1 to 5 in d4fba56
If the
st2-generate-schemas
script changes, then pants now knows that the schemas need to be regenerated. Or, in other words, thest2-generate-schemas
file is part of the cache key forcontrib/schemas/*.json
. If that file changes, then pants will re-run the generator.st2-generate-schemas
is a python script, and we use pants' python dependency inference. So pants (effectively) also adds the python files imported byst2-generate-schemas
to the cache key. Stepping through the code, we can see what pants would infer:st2/st2common/bin/st2-generate-schemas
Line 32 in d4fba56
st2/st2common/st2common/cmd/generate_schemas.py
Lines 26 to 30 in d4fba56
Here we reach the primary definition for the schemas that end up in
contrib/schemas
: the definition comes from thest2common.model.api.*
modules. Now, if anyone changes any of these files, or any of the python bits that get imported to help define things in them, then the next time someone runs thelint
goal (or in CI) they will find out that they need to run thefmt
goal.This PR actually only wires up the
fmt
goal and took advantage of the fact that pants also runs formatters whenever it runs linters, which are side-effect free. And because pants runs the formatters in a sandbox, it doesn't have to materialize any changes during lint.Continuing the example, since pants cached the results when running the
lint
goal, runningfmt
will be very fast. Pants will just materialize the already-generated schemas from its cache.Developing pants plugins
Pants has extensive documentation on the plugin API, including how to create targets, how to write rules, and there's even a mini tutorial about how to add a formatter (adding formatters is a very common thing for plugins to do).
Why not
codegen
?Pants has something called
codegen
. But it is for code that gets generated without getting checked into the repo. You can export those changes, but they are always exported in a directory underdist/
, so we would have to add some custom logic somewhere that copies fromdist/
to thecontrib/schemas
directory.These schemas are only available in this repo; if someone wants to use them, they have to clone the repo or grab the raw file from github. So, they have to be checked into the repo. We might be able to do something different in the future, but I'm trying not to change more than I have to to introduce pants.