bazelbuild / bazelbuild/bazel

Allow TemplateDict to accept Files and expand their content

Open
#22,550 5 comments 1 reaction 0 assignees View on GitHub
not stale P3 team-Rules-API type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the feature request:

Allow TemplateDict to accept File objects and have it expand them to the content of the file.

This idea comes from wanting to concat files together in an action. Normally this requires `run_shell`, which poses multi-platform complications.

So instead of this:

```
plain_zip =
# bootstrap_template is "#!/bin/bash ...shell code ..."
ctx.actions.expand_template(template=bootstrap_template, output=bootstrap, ...)
ctx.actions.run_shell(
command = "cat {bootstrap} {zip} > {output}".format(
bootstrap = bootstrap.path,
zip = plain_zip.path
output = executable_zip.path
)
)
```

One could do this:

```
computed = TemplateDict()
computed.add_file("%ZIP%", plain_zip)
ctx.actions.expand_template(
# execeutable_zip_template is "#!/bin/bash ...shell..\n%ZIP%"
template=executable_zip_template,
output=executable_zip.path,
computed_substitutions = computed
)
```

Similarly, this would come in handy for a two-stage bootstrap I need to create: stage one is some shell code that computes some values to feed into the second stage python file. Basically a template that looks like

```
#!bin/bash
foo="%something%"
bar=
file=
exec $file
```

Having it all in one file is sort of annoying. Being able to split it in two would be handy.

### Which category does this issue belong to?

Rules API

### What underlying problem are you trying to solve with this feature?

Being able to use file content in template actions

### Which operating system are you running Bazel on?

linux

### What is the output of `bazel info release`?

7.x

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reading the TemplateDict and ctx.actions.expand_template entry points described in the issue, comparing them with the existing run_shell approach. Done means TemplateDict accepts File objects and expands their contents during template expansion, supporting the file-concatenation and two-stage examples without run_shell.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.