# Buggy code
async def find_binary(request: BinaryPathRequest) -> BinaryPaths:
    # TODO(John Sirois): Replace this script with a statically linked native binary so we don't
    #  depend on either /bin/bash being available on the Process host.

    # Note: the backslash after the """ marker ensures that the shebang is at the start of the
    # script file. Many OSs will not see the shebang if there is intervening whitespace.
    script_path = "./script.sh"
    script_content = dedent(
        """\
        #!/usr/bin/env bash

        set -euo pipefail

        if command -v which > /dev/null; then
            command which -a $1
        else
            command -v $1
        fi
        """
    )
    script_digest = await Get(
        Digest,
        CreateDigest(
            [FileContent(script_path, script_content.encode(), is_executable=True)]
        ),
    )

    search_path = create_path_env_var(request.search_path)
    result = await Get(
        FallibleProcessResult,
        # We use a volatile process to force re-run since any binary found on the host system today
        # could be gone tomorrow. Ideally we'd only do this for local processes since all known
        # remoting configurations include a static container image as part of their cache key which
        # automatically avoids this problem.
        UncacheableProcess(
            Process(
                description=f"Searching for `{request.binary_name}` on PATH={search_path}",
                level=LogLevel.DEBUG,
                input_digest=script_digest,
                argv=[script_path, request.binary_name],
                env={"PATH": search_path},
            )
        ),
    )

    binary_paths = BinaryPaths(binary_name=request.binary_name)
    if result.exit_code != 0:
        return binary_paths

    found_paths = result.stdout.decode().splitlines()
    if not request.test_args:
        return dataclasses.replace(
            binary_paths, paths=[BinaryPath(path) for path in found_paths]
        )

    results = await MultiGet(
        Get(
            FallibleProcessResult,
            UncacheableProcess(
                Process(
                    description=f"Test binary {path}.",
                    level=LogLevel.DEBUG,
                    argv=[path, *request.test_args],
                )
            ),
        )
        for path in found_paths
    )
    return dataclasses.replace(
        binary_paths,
        paths=[
            BinaryPath.fingerprinted(path, result.stdout)
            for path, result in zip(found_paths, results)
            if result.exit_code == 0
        ],
    )


# Traceback (pants.engine.internals.scheduler.ExecutionError)
$ ./pants fmt src/python/pants::
Compiling engine v0.0.1 (XXX)
Finished release [optimized + debuginfo] target(s) in 2m 49s
12:11:49 [INFO] initializing pantsd...
12:12:00 [INFO] pantsd initialized.
12:12:08.46 [INFO] Completed: Building black.pex with 2 requirements: black==19.10b0, setuptools
12:12:08 [WARN] /Users/tdyas/TC/pants/src/python/pants/base/exception_sink.py:359: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
process_title=setproctitle.getproctitle(),

12:12:08 [ERROR] 1 Exception encountered:

Engine traceback:
in select
in `fmt` goal
in pants.backend.python.lint.python_fmt.format_python_target
in Format with Black
in pants.engine.process.fallible_to_exec_result_or_raise
Traceback (most recent call last):
File "XXX/pants/src/python/pants/engine/process.py", line 229, in fallible_to_exec_result_or_raise
raise ProcessExecutionFailure(
pants.engine.process.ProcessExecutionFailure: Process 'Run Black on 413 files.' failed with exit code 127.
stdout:

stderr:
env: python3.7: No such file or directory

Traceback (most recent call last):
File "XXX/pants/src/python/pants/bin/local_pants_runner.py", line 255, in run
engine_result = self._run_v2()
File "XXX/pants/src/python/pants/bin/local_pants_runner.py", line 166, in _run_v2
return self._maybe_run_v2_body(goals, poll=False)
File "XXX/pants/src/python/pants/bin/local_pants_runner.py", line 183, in _maybe_run_v2_body
return self.graph_session.run_goal_rules(
File "XXX/pants/src/python/pants/init/engine_initializer.py", line 117, in run_goal_rules
exit_code = self.scheduler_session.run_goal_rule(
File "XXX/pants/src/python/pants/engine/internals/scheduler.py", line 552, in run_goal_rule
self._raise_on_error([t for _, t in throws])
File "XXX/pants/src/python/pants/engine/internals/scheduler.py", line 511, in _raise_on_error
raise ExecutionError(
pants.engine.internals.scheduler.ExecutionError: 1 Exception encountered:

Engine traceback:
in select
in `fmt` goal
in pants.backend.python.lint.python_fmt.format_python_target
in Format with Black
in pants.engine.process.fallible_to_exec_result_or_raise
Traceback (most recent call last):
File "XXX/pants/src/python/pants/engine/process.py", line 229, in fallible_to_exec_result_or_raise
raise ProcessExecutionFailure(
pants.engine.process.ProcessExecutionFailure: Process 'Run Black on 413 files.' failed with exit code 127.
stdout:

stderr:
env: python3.7: No such file or directory

# Fixed code
async def find_binary(request: BinaryPathRequest) -> BinaryPaths:
    # TODO(John Sirois): Replace this script with a statically linked native binary so we don't
    #  depend on either /bin/bash being available on the Process host.

    # Note: the backslash after the """ marker ensures that the shebang is at the start of the
    # script file. Many OSs will not see the shebang if there is intervening whitespace.
    script_path = "./script.sh"
    script_content = dedent(
        """\
        #!/usr/bin/env bash

        set -euo pipefail

        if command -v which > /dev/null; then
            command which -a $1
        else
            command -v $1
        fi
        """
    )
    script_digest = await Get(
        Digest,
        CreateDigest(
            [FileContent(script_path, script_content.encode(), is_executable=True)]
        ),
    )

    search_path = create_path_env_var(request.search_path)
    result = await Get(
        FallibleProcessResult,
        # We use a volatile process to force re-run since any binary found on the host system today
        # could be gone tomorrow. Ideally we'd only do this for local processes since all known
        # remoting configurations include a static container image as part of their cache key which
        # automatically avoids this problem.
        UncacheableProcess(
            Process(
                description=f"Searching for `{request.binary_name}` on PATH={search_path}",
                level=LogLevel.DEBUG,
                input_digest=script_digest,
                argv=[script_path, request.binary_name],
                env={"PATH": search_path},
            ),
            scope=ProcessScope.PER_SESSION,
        ),
    )

    binary_paths = BinaryPaths(binary_name=request.binary_name)
    if result.exit_code != 0:
        return binary_paths

    found_paths = result.stdout.decode().splitlines()
    if not request.test:
        return dataclasses.replace(
            binary_paths, paths=[BinaryPath(path) for path in found_paths]
        )

    results = await MultiGet(
        Get(
            FallibleProcessResult,
            UncacheableProcess(
                Process(
                    description=f"Test binary {path}.",
                    level=LogLevel.DEBUG,
                    argv=[path, *request.test.args],
                ),
                scope=ProcessScope.PER_SESSION,
            ),
        )
        for path in found_paths
    )
    return dataclasses.replace(
        binary_paths,
        paths=[
            BinaryPath.fingerprinted(path, result.stdout)
            if request.test.fingerprint_stdout
            else BinaryPath(path, result.stdout.decode())
            for path, result in zip(found_paths, results)
            if result.exit_code == 0
        ],
    )