bazel-contrib / bazel-contrib/rules_jsonnet

Support user definitions in substrings

Open
#126 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
72
Forks
87
PR merge metrics
No merged PRs in 30d

Description

I'm using jsonnet to generate docker-compose configs. I'd like to pass bazel defines/vars into the generated files.

Maybe something like this:
```py
ext_strs = {
"foo_cmd": " ".join([
"--key0=val0",
"--key1=val1",
"--mode=$(foo_mode)",
]),
"bar_cmd": " ".join([
"--key2=val2",
"--key3=val3",
"--mode=$(bar_mode)",
]),
},
```

Unfortunately, `jsonnet.bzl` only does substitution if the `$(...)` is the entire string.
https://github.com/bazelbuild/rules_jsonnet/blob/12979862ab51358a8a5753f5a4aa0658fec9d4af/jsonnet/jsonnet.bzl#L128

I was thinking of using a `select` statement, but that's broken inside dicts:
https://github.com/bazelbuild/bazel/issues/3902

I can promote variables to top-level:

```py
ext_strs = {
"foo_cmd": " ".join([
"--key0=val0",
"--key1=val1",
]),
"foo_mode": "$(foo_mode)",
"bar_cmd": " ".join([
"--key2=val2",
"--key3=val3",
]),
"bar_mode": "$(bar_mode)",
},
```

But this makes "foo_mode" and "bar_mode" required, since jsonnet doesn't support default values for extVar: https://github.com/google/go-jsonnet/issues/247

This is unpleasant, since we have several `jsonnet_to_json` rules, reusing the same `foo.jsonnet`, enabling & disabling various containers. I'd like to avoid enumerating all custom args for containers that have been explicitly disabled.

The request I'll make here is to add support for `$(...)` within larger strings.

Contributor guide

Open the contributing guide

Research direction

Start in jsonnet/jsonnet.bzl around line 128 and trace how substitutions are handled when a value contains more than a single $(...) expression. Define the expected behavior for embedded user definitions such as $(foo_mode) in larger strings, then inspect the repository's existing rule tests to determine where coverage belongs and verify the generated configuration output.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.