nice-florist-55958
08/09/2022, 3:22 PMpex_binary
, are there any options now or plans in the future to support pre/post-packaging script injection? Example: a project contains some source code that an npm packaged tool (e.g., gulp
) uses to generate html/css/js assets for a Python webserver that's packaged into a pex_binary
with the built assets bundled as a resource
. It would be nice to still be able to do ./pants package webserver
instead of A) ./build-support/prepackage_prepare_script.sh && ./pants package webserver && ./build-support/postpackage_cleanup_script.sh
or B) build assets continuously and check into source control. Presumably the cleanup step can be eliminated if Pants does all the packaging in a tmp folder.bitter-ability-32190
08/09/2022, 3:24 PMexperimental_shell_command
https://www.pantsbuild.org/v2.13/docs/reference-experimental_shell_command
except it generates `file`s under-the-hood which don't get packaged 😭
It's the main reason I've worked on a proposal to redesign how Pants understands dependencies.bitter-ability-32190
08/09/2022, 3:24 PMexperimental_shell_command
but generates resources might be the ticketnice-florist-55958
08/09/2022, 3:32 PM./pants package,run,test,etc. webserver
or would still need to run the experimental_shell_command
first before any of these goals?bitter-ability-32190
08/09/2022, 3:33 PMshell_generating_resources
target was depended on by your pex_binary
it'd package the resources (or pull from the cache if its fresh) before building the PEXnice-florist-55958
08/09/2022, 3:35 PMexperimental_shell_command
can be depended on by pex_binary
and it would be ran first for its side-effects. But this would always be ran before run, package, etc.
I assume? What about cleanup after (or just dump all the built resources into /var/tmp/...
?)bitter-ability-32190
08/09/2022, 3:36 PMBut this would always be ran beforeIt'd only be run if the thing you're trying toI assume?run, package, etc.
package
or run
depends on the outputs AND the cache isn't valid given the inputsbitter-ability-32190
08/09/2022, 3:37 PMWhat about cleanup afterPants codegenerates files into the cache, not into your repo. No cleanup needed 😉
bitter-ability-32190
08/09/2022, 3:37 PMnice-florist-55958
08/09/2022, 3:41 PMpex_binary
depends on what files experimental_shell_command
dumps into its outputs
path, so would need a new plugin?bitter-ability-32190
08/09/2022, 3:42 PMoutputs
are treated like the files
target, and Pants doesn't include files
in packaged artifacts (except archive
) 😞bitter-ability-32190
08/09/2022, 3:42 PMfile
and resource
😂nice-florist-55958
08/09/2022, 3:43 PM.<;;
bitter-ability-32190
08/09/2022, 3:44 PMnice-florist-55958
08/09/2022, 3:44 PMfile
and resource
to force you to distinguish between which API you use (e.g., open
vs. pkgutil.get_data
?)bitter-ability-32190
08/09/2022, 3:45 PMnice-florist-55958
08/09/2022, 3:45 PMpkgutil
even when using a resource
, and rather do something like Path(__file__).resolve().parent
instead.bitter-ability-32190
08/09/2022, 3:46 PMnice-florist-55958
08/09/2022, 3:50 PMpex_binary
sound (basically acknowledging you understand the disclaimer about how file APIs in Python work)?
For the plugin route -- without first reading your instruction link -- would we be adding a Python module to say ./admin/build-support/pants/plugins/experimental_shell_command2_rc.py
and then registering it in pants.toml
or some place? And said Python module, that could be implemented easily w/ inheritence of the existing target I hope or require a wholesale copy/paste/edit?bitter-ability-32190
08/09/2022, 3:52 PMfile
and resource
get elided into asset
, and how it is consumed is shifted to the consuming target, via file_deps
or resource_deps
)bitter-ability-32190
08/09/2022, 3:53 PMFor the plugin route ...Yeah, that's it in a nutshell. There's a handful of plumbing work to get things stood up, but once all cylinders are firing you'll be blasting away. It's worth mentioning once you get cozy with the plugin API and Pants internals, you can do some really fun things 🙂 At some point we had like 12 plugins in-repo (I've been slowly upstreaming them)
bitter-ability-32190
08/09/2022, 3:53 PMor require a wholesale copy/paste/edit?Unfortunately this 😞
nice-florist-55958
08/09/2022, 3:54 PMbitter-ability-32190
08/09/2022, 3:56 PMnice-florist-55958
08/09/2022, 5:55 PMShellCommandTarget
are? I can't find them by searching (https://github.com/pantsbuild/pants/search?q=ShellCommandTarget&type=code). I was able to locate the target class definition, however.bitter-ability-32190
08/09/2022, 5:56 PMbitter-ability-32190
08/09/2022, 5:56 PMnice-florist-55958
08/09/2022, 5:57 PMShellCommandTarget
:
from pants.backend.shell.target_types import ( ShellCommandCommandField, ShellCommandLogOutputField, ShellCommandOutputsField, ShellCommandRunWorkdirField, ShellCommandSourcesField, ShellCommandTimeoutField, ShellCommandToolsField,)
But will keep readingbitter-ability-32190
08/09/2022, 5:58 PMbitter-ability-32190
08/09/2022, 5:58 PMbitter-ability-32190
08/09/2022, 5:58 PMnice-florist-55958
08/09/2022, 6:02 PMnice-florist-55958
08/09/2022, 6:02 PMbitter-ability-32190
08/09/2022, 6:02 PMbitter-ability-32190
08/09/2022, 6:03 PMnice-florist-55958
08/09/2022, 6:05 PMnice-florist-55958
08/09/2022, 6:11 PMbitter-ability-32190
08/09/2022, 6:11 PMbitter-ability-32190
08/09/2022, 6:12 PMbitter-ability-32190
08/09/2022, 6:12 PMnice-florist-55958
08/09/2022, 6:27 PMshellrc
with shellrc.register
and shellrc.shell_command_rc
modules. In the latter, I'm defining a new target ShellCommandRCTarget
and a rule run_shell_rc_command
that accepts a new GeneratedResourcesFromShellRCCommandRequest
subclass of GenerateSourcesRequest
.
Besides those changes, can I just import (reuse) the dependencies from pants.backends.shell
or do I need to duplicate those too?bitter-ability-32190
08/09/2022, 6:38 PMnice-florist-55958
08/09/2022, 6:44 PMmyplugins.shellrc.__init__.ShellCommandRCTarget
re-use pants.backend.shell.target_types.ShellCommandSourcesField
in its core_fields
attribute?bitter-ability-32190
08/09/2022, 6:45 PMnice-florist-55958
08/09/2022, 8:46 PMexperimental_shell_command
outputs do not get copied into the pex, but that enabling my new plugins.shellrc
backend and using the new shell_command_rc
target does! (Although somewhat confusingly when the new backend is enabled, experimental_shell_command
is inherting the new behavior -- I am sure this has to do w/ me reusing components of the shipped backend and masking behavior).
Two questions:
1. I am getting bizzare npm
errors trying to find node_modules
in a place it does not exist - it looks like it has something to do w/ the environment sandboxing -- can you point me to where I can just eliminate any environment scrubbing/tool symlinking? I just want to inherit the user's shell when running the script (for now) -- I think this is what experimental_run_shell_command
does?
2. The outputs directory is the full path relative to the pants.toml
root, which does not work well with the stripped source root of the Python code that depends on the generated resources. So the generated assets might get put in ./proj/app/myproject/scripts/build-support/assets
, but the code that depends on them is in ./myprojec/webserver.py
because ./proj/app/myproject/src
is a source root containing package myproject
. Is there any relocation mechanism available so something like f"{Path(__file__).resolve().parent[n]}/proj/app/myproject/scripts/build-support/assets"
isn't needed? I would want the files copied to ./proj/app/myproject/src/myproject/assets
so we'd use f"{Path(__file__).resolve().parent}/assets"
nice-florist-55958
08/09/2022, 9:32 PMbitter-ability-32190
08/09/2022, 9:40 PMrelocated_resources
target plugin?
Lolol so sorry for the mess (look up relocated_files
)nice-florist-55958
08/09/2022, 10:50 PMpants.core.target_types
, but can't find anything about operations on them by searching the repo for the target or any of its fields/aliases. At this point I have added a RelocatedResource version of RelocatedFile target/fields to the new backend, but not clear how that would automatically result in the engine relocating resources.bitter-ability-32190
08/10/2022, 12:39 AMrelocated_files
is just a file codegenerator which generates the files at a different location.
Don't forget the `rule`s operate on fields, so grepping for `RelocatedFilesSourcesField`leads to RelocateFilesViaCodegenRequest
leads to relocate_files
nice-florist-55958
08/10/2022, 12:45 AMbitter-ability-32190
08/10/2022, 12:48 AMnice-florist-55958
08/10/2022, 1:14 AMRelocatedFilesSourcesField
(alias="_sources"
)bitter-ability-32190
08/10/2022, 1:15 AMnice-florist-55958
08/10/2022, 1:15 AMnice-florist-55958
08/10/2022, 1:45 AMsrc
stage (so I have to strip a prefix relative to the SCM root) and not at the dest
stage (i.e., I have to replace the stripped prefix with something that takes into account source root stripping). I expect that is what we want and expect, but not sure if I am overriding any fancy relocation logic that applies only to resources.nice-florist-55958
08/10/2022, 2:16 AMshellrc
that exposes relocated_resources
and shell_command_rc
, allowing you to generate assets at test/package/run/etc. time from a build script. Main benefits are 1) Same idiomatic usage of pants goals locally and in CI and 2) in asset-heavy builds, no need to continually update and check into source control or sandwich pants commands between build/cleanup scripts and 3) Helps provide a glue for builds that are predominately not Python, but for which you still want the illusion of the benefits of using Pant's Python backend (e.g., fatdist a nodejs app as a pex_binary w/ an npm install/setup script) 🙂
That was a fun intro to plugins. Since this is just a very natural extension of the existing pants.backend.shell
and pants.core.target_types
modules, might be better to just submit a PR extending those?