Skip to content

Commit

Permalink
Tidy up computation of import paths (#182)
Browse files Browse the repository at this point in the history
* Reformat BUILD using buildifier

* Tidy up computation of import paths

Right now you see that jsonnet_to_json() and jsonnet_to_json_test()
implicitly call jsonnet with "-J .". This means that if these rules are
used in combination with libraries that are declared in the root module,
everything works as expected. But as soon as libraries are used from
other modules, importing them becomes more tedious.

The goal of this change is to ensure that if a Jsonnet project can be
built within the root module, that module can also safely be used as a
child module. We solve this by automatically adding the workspace root
to the set of import paths for which one or more source files exist.
Furthermore, by considering the root of every source file, we no longer
need to use bin_dir and genfiles_dir.

Fixes: #44
Fixes: #86
Fixed: #139
Fixes: #154
Fixes: #178
  • Loading branch information
EdSchouten committed Apr 4, 2024
1 parent 1839d26 commit 0a60c2f
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
other_module
20 changes: 16 additions & 4 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ jsonnet_to_json_test(
name = "tla_code_files_test",
size = "small",
src = "tla_code_files.jsonnet",
golden = "tla_code_files_golden.json",
tla_code_files = {
"tla_code_file_input.json": "tla_file",
},
golden = "tla_code_files_golden.json",
)

jsonnet_to_json_test(
Expand Down Expand Up @@ -235,15 +235,27 @@ jsonnet_to_json_test(
jsonnet_to_json_test(
name = "strings_test",
src = "strings.jsonnet",
golden = "strings_golden.txt",
extra_args = ["--string"],
canonicalize_golden = False,
extra_args = ["--string"],
golden = "strings_golden.txt",
)

jsonnet_to_json_test(
name = "output_file_contents_smoke_test",
src = "wordcount.jsonnet",
golden = "wordcount_golden.json",
deps = [":workflow"],
output_file_contents = False,
deps = [":workflow"],
)

jsonnet_to_json_test(
name = "other_module_test",
src = "other_module.jsonnet",
canonicalize_golden = False,
extra_args = ["--string"],
golden = "other_module_golden.txt",
deps = [
"@other_module//:hello",
"@other_module//:world",
],
)
6 changes: 6 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ local_path_override(
module_name = "rules_jsonnet",
path = "..",
)

bazel_dep(name = "other_module", version = "0.0.0")
local_path_override(
module_name = "other_module",
path = "other_module",
)
1 change: 1 addition & 0 deletions examples/other_module.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(import 'hello.jsonnet') + ' ' + (import 'world.jsonnet')
19 changes: 19 additions & 0 deletions examples/other_module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")

jsonnet_library(
name = "hello",
srcs = ["hello.jsonnet"],
visibility = ["//visibility:public"],
)

genrule(
name = "world_src",
outs = ["world.jsonnet"],
cmd = "echo \\'world\\' > $@",
)

jsonnet_library(
name = "world",
srcs = ["world.jsonnet"],
visibility = ["//visibility:public"],
)
6 changes: 6 additions & 0 deletions examples/other_module/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module(
name = "other_module",
version = "0.0.0",
)

bazel_dep(name = "rules_jsonnet", version = "0.5.0")
1 change: 1 addition & 0 deletions examples/other_module/hello.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'hello'
1 change: 1 addition & 0 deletions examples/other_module_golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
6 changes: 5 additions & 1 deletion jsonnet/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ bzl_library(
bzl_library(
name = "jsonnet",
srcs = ["jsonnet.bzl"],
deps = [":bzl_srcs"],
visibility = ["//visibility:public"],
deps = [
":bzl_srcs",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:shell",
],
)

py_binary(
Expand Down
39 changes: 23 additions & 16 deletions jsonnet/jsonnet.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ jsonnet_go_dependencies()
```
"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

_JSONNET_FILETYPE = [
Expand All @@ -59,13 +61,17 @@ _JSONNET_FILETYPE = [
".json",
]

def _add_prefix_to_imports(label, imports):
imports_prefix = ""
if label.workspace_root:
imports_prefix += label.workspace_root + "/"
if label.package:
imports_prefix += label.package + "/"
return [imports_prefix + im for im in imports]
def _get_import_paths(label, files, imports):
return [
# Implicitly add the workspace root as an import path.
paths.join(".", file.root.path, label.workspace_root)
for file in files
] + [
# Explicitly provided import paths.
paths.join(".", file.root.path, label.workspace_root, label.package, im)
for file in files
for im in imports
]

def _setup_deps(deps):
"""Collects source files and import flags of transitive dependencies.
Expand Down Expand Up @@ -97,7 +103,10 @@ def _jsonnet_library_impl(ctx):
"""Implementation of the jsonnet_library rule."""
depinfo = _setup_deps(ctx.attr.deps)
sources = depset(ctx.files.srcs, transitive = [depinfo.transitive_sources])
imports = depset(_add_prefix_to_imports(ctx.label, ctx.attr.imports), transitive = [depinfo.imports])
imports = depset(
_get_import_paths(ctx.label, ctx.files.srcs, ctx.attr.imports),
transitive = [depinfo.imports],
)
transitive_data = depset(
transitive = [dep.data_runfiles.files for dep in ctx.attr.deps],
)
Expand Down Expand Up @@ -210,12 +219,10 @@ def _jsonnet_to_json_impl(ctx):
[
"set -e;",
toolchain.jsonnet_path,
] + ["-J %s" % im for im in _add_prefix_to_imports(ctx.label, ctx.attr.imports)] +
["-J %s" % im for im in depinfo.imports.to_list()] + [
"-J .",
"-J %s" % ctx.genfiles_dir.path,
"-J %s" % ctx.bin_dir.path,
] + other_args +
] +
["-J " + shell.quote(im) for im in _get_import_paths(ctx.label, [ctx.file.src], ctx.attr.imports)] +
["-J " % shell.quote(im) for im in depinfo.imports.to_list()] +
other_args +
["--ext-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_strs.items()] +
["--ext-str '%s'" %
Expand Down Expand Up @@ -385,8 +392,8 @@ def _jsonnet_to_json_test_impl(ctx):
other_args = ctx.attr.extra_args + (["-y"] if ctx.attr.yaml_stream else [])
jsonnet_command = " ".join(
["OUTPUT=$(%s" % ctx.executable.jsonnet.short_path] +
["-J %s" % im for im in _add_prefix_to_imports(ctx.label, ctx.attr.imports)] +
["-J %s" % im for im in depinfo.imports.to_list()] + ["-J ."] +
["-J " + shell.quote(im) for im in _get_import_paths(ctx.label, [ctx.file.src], ctx.attr.imports)] +
["-J " + shell.quote(im) for im in depinfo.imports.to_list()] +
other_args +
["--ext-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_strs.items()] +
Expand Down

0 comments on commit 0a60c2f

Please sign in to comment.