Add pants-plugins/schemas to streamline regenerating contrib/schemas#5847
Merged
cognifloyd merged 13 commits intomasterfrom Jan 10, 2023
Merged
Add pants-plugins/schemas to streamline regenerating contrib/schemas#5847cognifloyd merged 13 commits intomasterfrom
pants-plugins/schemas to streamline regenerating contrib/schemas#5847cognifloyd merged 13 commits intomasterfrom
Conversation
cognifloyd
commented
Dec 12, 2022
pants-plugins/schemas/rules.py
Outdated
| ], | ||
| output_filename=f"{CMD}.pex", | ||
| internal_only=True, | ||
| main=EntryPoint.parse("st2common.cmd.{CMD}:main"), |
Member
Author
There was a problem hiding this comment.
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/schemaspants-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/schemaspants-plugins/schemas to streamline regenerating contrib/schemas
amanda11
approved these changes
Jan 5, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
fmtandlintgoals to generatecontrib/schemas/*.jsonif needed (or for lint, warn that it needs to be done).This includes creating a
schemasplugin for pants that adds aschemastarget.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
fmtgoal. This also adds it to thelintgoal which will tell people iffmtneeds 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
schemastarget incontrib/schemasdepends onst2common/bin/st2-generate-schemas:st2/contrib/schemas/BUILD
Lines 1 to 5 in d4fba56
If the
st2-generate-schemasscript changes, then pants now knows that the schemas need to be regenerated. Or, in other words, thest2-generate-schemasfile is part of the cache key forcontrib/schemas/*.json. If that file changes, then pants will re-run the generator.st2-generate-schemasis a python script, and we use pants' python dependency inference. So pants (effectively) also adds the python files imported byst2-generate-schemasto 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 thelintgoal (or in CI) they will find out that they need to run thefmtgoal.This PR actually only wires up the
fmtgoal 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
lintgoal, runningfmtwill 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/schemasdirectory.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.