How do I debug system_binary? ```system_binary( ...
# general
p
How do I debug system_binary?
Copy code
system_binary(
    name="git",
    binary_name="git",
)
Leads to
Copy code
pants run .build:build-libcamera
17:37:36.50 [INFO] Completed: Testing candidate for `git` at `/bin/git`
17:37:36.50 [INFO] Completed: Testing candidate for `git` at `/usr/bin/git`
17:37:36.51 [ERROR] 1 Exception encountered:

Engine traceback:
  in `run` goal

ValueError: Could not find a binary with name `git`. The following paths were searched: /usr/bin, /bin, /usr/local/bin, /opt/homebrew/bin.
But..
Copy code
which git
/usr/bin/git
Maybe git is a funky binary.
e
Add
--keep-sandboxes=always
and see for yourself what "Testing candidate for
git
at `/usr/bin/git`" sees,
p
Hmm looks normal?
Copy code
pants run .build:libcamera --keep-sandboxes=always
17:52:03.84 [INFO] Initialization options changed: reinitializing scheduler...
17:52:08.36 [INFO] Scheduler initialized.
17:52:08.47 [INFO] Preserving local process execution dir /tmp/pants-sandbox-5nreNe for Searching for `bash` on PATH=/usr/bin:/bin:/usr/local/bin
17:52:08.51 [INFO] Preserving local process execution dir /tmp/pants-sandbox-gSV1Op for Test binary /usr/bin/bash.
17:52:08.51 [INFO] Preserving local process execution dir /tmp/pants-sandbox-KntnE3 for Test binary /bin/bash.
17:52:08.57 [INFO] Preserving local process execution dir /tmp/pants-sandbox-Ee4YW5 for Searching for `git` on PATH=/usr/bin:/bin:/usr/local/bin:/opt/homebrew/bin
17:52:08.61 [INFO] Preserving local process execution dir /tmp/pants-sandbox-Ce8jj4 for Testing candidate for `git` at `/bin/git`
17:52:08.61 [INFO] Preserving local process execution dir /tmp/pants-sandbox-usMJKB for Testing candidate for `git` at `/usr/bin/git`
17:52:08.80 [INFO] Completed: Testing candidate for `git` at `/bin/git`
17:52:08.80 [INFO] Completed: Testing candidate for `git` at `/usr/bin/git`
17:52:08.82 [ERROR] 1 Exception encountered:

Engine traceback:
  in `run` goal

ValueError: Could not find a binary with name `git`. The following paths were searched: /usr/bin, /usr/bin, /bin, /usr/local/bin, /opt/homebrew/bin.
e
Copy code
Preserving local process execution dir /tmp/pants-sandbox-usMJKB for Testing candidate for `git` at `/usr/bin/git`
So cd to
/tmp/pants-sandbox-usMJKB
There will be a
__run.sh
script that encodes what Pants tried.
You can play with that, check error codes, print things, etc.
p
Ahhh neat!
git by itself returns exit code of 1
e
What does Pants try though? Not
git
presumably
git --version
or something?
p
Well I had
Copy code
system_binary(
    name="git",
    binary_name="git",
)
e
Or is this you?
Ah it's you.
p
It just tried /usr/bin/git, which returns 1
Lol
e
Almost certainly there is a place to configure a test.
Pants internal use of system binary does a test of the binary of some sort.
p
Yeah we can add the fingerprint_args to get it to work, then test it too for versions
e
So what is the contents of
__run.sh
exactly?
p
It was
Copy code
cat __run.sh 
#!/bin/bash
# This command line should execute the same process as pants did internally.
export 
cd /tmp/pants-sandbox-usMJKB
/usr/bin/git
e
Ok, yeah. So I'm surprised this works for people generally. It seems to me most tools error when invoked with no args - but maybe not.
Seems like a trap alot of folks would fall into at any rate like you did here.
I would think no fingerprint args means do not try at all I guess, just check executable and no more - don't actually run.
That or always require fingerprint args - even if empty - to get the user to ack they know what they're doing.
p
Yeah. I hit it with npm last night.. same thing returns exit code of 1
e
Ok, that seems like a wart to me then in this whole mechanism. That said I have not gone and read high-level docs - maybe this is spelled out.
b
I've tripped over this too. I filed https://github.com/pantsbuild/pants/issues/19013
p
Actually I can't get this to work..
Copy code
system_binary(
    name="npm",
    binary_name="npm",
    fingerprint=r"9.2.0",
    fingerprint_args=["--version"],
)
Copy code
cat /tmp/pants-sandbox-MjbXO2/__run.sh 
#!/bin/bash
# This command line should execute the same process as pants did internally.
export 
cd /tmp/pants-sandbox-MjbXO2
/usr/bin/npm --version
Copy code
pants run teletom/frontend:npm --keep-sandboxes=always
17:52:33.49 [INFO] Preserving local process execution dir /tmp/pants-sandbox-ewKBkO for Testing candidate for `npm` at `/bin/npm`
17:52:33.49 [INFO] Preserving local process execution dir /tmp/pants-sandbox-MjbXO2 for Testing candidate for `npm` at `/usr/bin/npm`
17:52:33.55 [INFO] Completed: Testing candidate for `npm` at `/bin/npm`
17:52:33.55 [INFO] Completed: Testing candidate for `npm` at `/usr/bin/npm`
17:52:33.55 [ERROR] 1 Exception encountered:

Engine traceback:
  in `run` goal

ValueError: Could not find a binary with name `npm` with output matching `9.2.0` when run with arguments `--version`. The following paths were searched: /usr/bin, /bin, /usr/local/bin, /opt/homebrew/bin.
Copy code
bash /tmp/pants-sandbox-MjbXO2/__run.sh | tail
declare -x XDG_RUNTIME_DIR="/run/user/1000"
declare -x XDG_SEAT="seat0"
declare -x XDG_SESSION_CLASS="user"
declare -x XDG_SESSION_DESKTOP="i3"
declare -x XDG_SESSION_ID="3"
declare -x XDG_SESSION_TYPE="x11"
declare -x XDG_VTNR="2"
declare -x XMODIFIERS="@im=ibus"
declare -x _="/usr/bin/bash"
9.2.0

echo $?
0
e
Did you mess with fingerprint? Add a newline etc?
p
like add a \n? 🤔
e
Yes!!!
Maybe dumb, but sort of obvious debug step.
p
There are no newlines or tabs 😾
e
So you actually tried
fingerprint_args=["--version\n"],
?
p
Oh I was trying on fingerprint regex
e
Sorry - typo, but you see what I mean?
fingerprint=r"9.2.0\n"
b
Two potential sources of issues 1. The
fingerprint
is matched with
re.match
, i.e. needs to be at the start. 2. in 2.16
__run.sh
isn't fully hermetic and leaks env vars from your shell, so the behaviour inside pants may be different to running it outside I think 2 might be the problem here:
npm
needs
node
available to run, and so the
fingerprint_dependencies=[...]
field will need to include a
node
system_binary
Based on the
export
being empty, I think switching the last line in
__run.sh
to
env -i /usr/bin/npm --version
will properly reproduce the in-pants behaviour
p
Ahhh good call on node
Copy code
system_binary(
    name="npm",
    binary_name="npm",
    fingerprint=r"9.2.0",
    fingerprint_args=["--version"],
    fingerprint_dependencies=[":node"],
)
And thank you @enough-analyst-54434 for the help 😃