https://pantsbuild.org/ logo
#general
Title
# general
s

salmon-barista-63163

06/22/2021, 4:19 PM
I am trying to run some pytests with pants 2.0.2. These specific tests run a subprocess command in a function that interacts with
git
. I am getting the following error:
Copy code
fatal: not a git repository (or any of the parent directories): .git
I have found that this is because with pants 2 the root where the tests run is
Copy code
/private/var/folders/89/r169_6497n15qnqm0bfhnjlr0000gp/T/process-executionzY6hQB
which does not include my “.git” config files. With pants v1 all of these tests ran without issue and im assuming the root was the “build root”. I have tried adding a global
resource
that includes the
git
files necessary but that is not working. Is this as simple as changing the
rootdir
of the global pants options? I would like to not have things running “inside” of my build root as it could cause other issues. Any other suggestions?
e

enough-analyst-54434

06/22/2021, 5:58 PM
Well, not too satisfying - ie: no Pants magic, but one route is to re-write the tests to be hermetic and set up their own git repo. For example: https://github.com/pantsbuild/pants/blob/main/src/python/pants/vcs/git_test.py
h

happy-kitchen-89482

06/22/2021, 6:00 PM
Alternatively, having test
.git
files checked in as resources should work, although there may be some futzing with the paths.
e

enough-analyst-54434

06/22/2021, 6:04 PM
A thrird option is to plumb GIT_DIR and GIT_WORK_TREE env vars to point back at the build root. I'm not sure if this can be done portably with pants. Perhaps via pants.toml which supports "$(buildroot)s" interpolation.
Concretely, this would be the pants.toml edit in the pants repo itself:
Copy code
$ git diff
diff --git a/pants.toml b/pants.toml
index 985d8734e..05febd0aa 100644
--- a/pants.toml
+++ b/pants.toml
@@ -163,6 +163,8 @@ extra_env_vars = [
   "PATH",
   # We'd always like complete backtraces in tests.
   "RUST_BACKTRACE=1",
+  "GIT_DIR=%(buildroot)s/.git",
+  "GIT_WORK_TREE=%(buildroot)s",
 ]
 
 [coverage-py]
h

hundreds-father-404

06/22/2021, 6:16 PM
Resources won't work because pants_ignore option ignores .git, which is necessary to keep I believe
John's recommendation sounds good. Only issue I see is that it will cache when it may not be safe - pants won't be able to track when the .git folder should change I think we'd need to add a field to python_tests to mark the tests as uncacheable
e

enough-analyst-54434

06/22/2021, 6:29 PM
The env var value is part of the cache key, so things should just work.
h

hundreds-father-404

06/22/2021, 6:30 PM
But the env var doesn't change when .git folder changes, right?
e

enough-analyst-54434

06/22/2021, 6:30 PM
Aha, yes - Eric is right if the tests depend on the state of the git repo.
If they just use it to branch from a known old commit, do some work, then clean up, you should be good.
👍 1
🔥 1
s

salmon-barista-63163

06/22/2021, 8:23 PM
all my tests do not depend on state so this will work! thanks guys as always
❤️ 1
I will let you know how it works out
🤞 1
h

happy-kitchen-89482

06/23/2021, 4:26 PM
@hundreds-father-404 This wouldn't be the real
.git
but a test one that is checked into the repo under some resources dir
e

enough-analyst-54434

06/24/2021, 12:49 AM
Pants aside, developer to developer, I'd strongly (re) consider just having tests set up their own hermetic git repos unless there is some really pressing reason this won't work.
2
It sounds mildly terrifying to have tests operating on your project git repo.
1
s

salmon-barista-63163

06/24/2021, 9:16 PM
oh trust me…. i didnt write these and i do not like these tests…. something i inherited. we do not setup any repos… we use git for diffs in sub proceseses in some of our clis.