Skip to content

Allow File values in env of ctx.actions.run[_shell]#29815

Draft
fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:path-mapped-env
Draft

Allow File values in env of ctx.actions.run[_shell]#29815
fmeum wants to merge 1 commit into
bazelbuild:masterfrom
fmeum:path-mapped-env

Conversation

@fmeum

@fmeum fmeum commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

The values of the env dict of ctx.actions.run and ctx.actions.run_shell can now be Files in addition to strings. Such values are expanded to the file's exec path at execution time, after path mapping has been applied. Compared to expanding the path during analysis, this allows environment variables referencing input or output paths to benefit from cross-configuration cache hits with --experimental_output_paths=strip.

Implementation notes:

  • ActionEnvironment stores fixed variables in a single ImmutableMap<String, /* String | Artifact */ Object>. All analysis-time views of the environment (getFixedEnv(), resolve(), aquery, extra actions, Action.env in Starlark) resolve Artifact values to their unmapped exec paths, so their types and behavior are unchanged.
  • The spawn's environment re-resolves artifact values with the spawn's PathMapper (ActionEnvironment#resolveArtifactValues).
  • The action key fingerprints artifact values with PathMapper.forActionKey, mirroring how command lines are fingerprinted. Environments without artifact values produce byte-identical fingerprints as before.
  • use_default_shell_env and --action_env interact with File-valued variables exactly as with string-valued ones.

Tested via unit tests for ActionEnvironment, analysis tests for the mapped spawn environment and the Starlark API, and an end-to-end test in path_mapping_test.sh that verifies a remotely executed action reads its input through the mapped env var and gets a cross-configuration remote cache hit.

@fmeum fmeum requested a review from a team as a code owner June 11, 2026 18:16
@fmeum fmeum requested review from mai93 and removed request for a team June 11, 2026 18:16
@github-actions github-actions Bot added team-Performance Issues for Performance teams team-Configurability platforms, toolchains, cquery, select(), config transitions team-Starlark-Integration Issues involving Bazel's integration with Starlark, excluding builtin symbols awaiting-review PR is awaiting review from an assigned reviewer team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts labels Jun 11, 2026
@fmeum fmeum marked this pull request as draft June 11, 2026 19:40
@fmeum fmeum force-pushed the path-mapped-env branch 2 times, most recently from 6d9eb15 to ba7e934 Compare June 12, 2026 11:47
@fmeum fmeum force-pushed the path-mapped-env branch 3 times, most recently from a574a16 to 9285526 Compare June 12, 2026 12:33
The values of the `env` dict of `ctx.actions.run` and
`ctx.actions.run_shell` can now be `File`s in addition to strings. Such
values are expanded to the file's exec path at execution time, after
path mapping has been applied. Compared to expanding the path during
analysis, this allows environment variables referencing input or output
paths to benefit from cross-configuration cache hits with
`--experimental_output_paths=strip`.

`ActionEnvironment` stores fixed variables in a single
`ImmutableMap<String, /* String | Artifact */ Object>` and
`Action#getEffectiveEnvironment` now returns such a map, with `String`
values used as is and `Artifact` values resolved to their (possibly
path-mapped) exec paths only when a spawn is created. This also covers
shadowed actions: artifact-valued variables contributed by a shadowed
action are mapped with the shadowing action's path mapper.
Analysis-time consumers (aquery, extra actions, `Action.env` in
Starlark) resolve artifact values to their unmapped exec paths via
`ActionEnvironment#resolveValues`, so their output is unchanged. The
action key fingerprints artifact values with `PathMapper.forActionKey`,
mirroring how command lines are fingerprinted. Environments without
artifact values produce byte-identical fingerprints as before.
@fmeum fmeum force-pushed the path-mapped-env branch from 9285526 to cc76d4a Compare June 17, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions team-Performance Issues for Performance teams team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts team-Starlark-Integration Issues involving Bazel's integration with Starlark, excluding builtin symbols

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant