Hello, I’m trying to write a plugin to provide cus...
# plugins
b
Hello, I’m trying to write a plugin to provide custom setup_py() Kwargs based on this doc. The custom rule should run a git
Process
to read tags and populate the
version
kwarg. However, I’m not sure what
input_digest
and/or
working_dir
should be provided to the Git process 🧵
the process fails since cannot locate the
.git
dir
h
Hello!
the process fails since cannot locate the .git  dir
Oh, hm...that will indeed be a problem because Pants ignores
.git
, which is almost always the right thing to do, but not here.
PathGlobs
won't be able to see that directory as a result 🤔 -- So, someone has done Git before by sidestepping the Rules API abstractions. Which you can do if you mark things as uncacheable. Use
@_uncacheable_rule
instead of
@rule
, and then in that function you can do normally unsafe things like
subprocess.run(["git", ...)
It must be marked uncacheable because this is not safe to ever cache, e.g. Pants will not monitor the filesystem to know when to invalidate the cache Does that make sense?
e
You can avoid going behind the Pants rule systems back with
@_uncacheable_rule
/ subprocess by leveraging GIT_DIR / GIT_WORK_TREE (c.f.: https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables) Here's a fully working demo where you can continually run
./pants git --work-tree=/some/other/repo -- describe
and get back new results every time you go over to that repo and
git reset
it to some new head: https://github.com/pantsbuild/pants/pull/12071/files#diff-31fcb2cbc80628cb417c19f397c0aac0d5578741d7e3954253ff68065d007726R61
🙌 2
h
Neat! Thanks John! To check my understanding, setting
GIT_DIR
and
GIT_WORK_TREE
allows the process to escape the chroot and read from the build root's .git folder, right?
e
Even if we could capture the .git dir as a Process input, we wouldn't want to today. That would be way too slow given our run-ins with the problems of tens of thousands of files and sandboxes. This is known to add many hundreds of ms to execution times.
👍 1
b
thanks John and Eric. This is helpful. I’ll try with the Git env vars
🤞 1
h
This is neat! is this worth documenting, in the context of setup_py or elsewhere?
h
I think likely! I really want to rewrite our example-plugin repo and have it use a dedicated folder of self-contained examples per plugin hook, rather than trying to ram everything into a coherent cohesive Bash plugin So here, I want multiple distinct examples of the setup-py plugin hook, and one should include Git
e
I'm not sure. At the end of the day this is just a git feature. The thing to document is probably escaping the sandbox for read-only operations which involves a combo of absolute paths and cache_scope=NEVER
👍 2
That's the real lesson here.