Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper function / @rule to inject environment variables with absolute paths into hermetic process executions #8076

Closed
cosmicexplorer opened this issue Jul 19, 2019 · 1 comment

Comments

@cosmicexplorer
Copy link
Contributor

#8036 (comment) describes one case where we wanted to be able to use a feature (a java agent) which required setting the runtime library path (DY)?LD_LIBRARY_PATH variable to make a compiled shared library available to the java invocation. We were able to work around this issue by using the -agentpath: option instead, but in general there are many settings for many programs, typically set via environment variables, which require absolute paths on most operating systems (such as PATH, CPATH, LIBRARY_PATH, and other variables used to configure compilation of C/C++ code).

See #6893 for another place we want to use these variables, and work around it by instead invoking /bin/sh to interpret a lengthy generated command line:

result = yield Get(ExecuteProcessResult, ExecuteProcessRequest(
argv=tuple([
'/bin/sh', '-c',
' && '.join(sub_commands),
]),

^The above is a little brittle, and #8036 (comment) describes one bikesheddable alternative -- a helper function or @rule:

def execute_process_with_env_vars_interpolated(execute_process_request, env_to_command):
  with temporary_directory() as tempdir:
    with open(os.path.join(tempdir, "script"), "w") as f:
      lines = ["{key}={value}".format(key=key, value=value) for key, value in sorted(env_to_command.items())]
      lines.append("exec " + shlex(execute_process_request.argv))
      f.write("\n".join(lines))
    captured_directory_digest = capture(tempdir, "script")
    merged_directory_digest = merge(captured_directory_digest, execute_process_request.directory_digest)
  return execute_process_request.with(argv="./script", directory_digest=merged_directory_digest)
@Eric-Arellano
Copy link
Contributor

Eric-Arellano commented Oct 12, 2020

This is achieved via PantsEnvironment, and I believe also BinaryPaths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants