Skip to content

Commit

Permalink
Add MODULE.bazel to rules_jsonnet (#179)
Browse files Browse the repository at this point in the history
* Make fewer assumptions about Bazel's pathname structure

When using bzlmod, there is no guarantee that the underlying name of the
external is "examples". This may currently cause these tests to fail due
to non-matching regular expressions.

* Give rules_jsonnet a MODULE.bazel

* Document how rules_jsonnet may be used with bzlmod

* Stop referencing WORKSPACE file in tests

* Let the examples repository use MODULE.bazel

Instead of using local_repository() inside of the root WORKSPACE, give
examples/ its own MODULE.bazel that uses local_repository() to access
the copy of rules_jsonnet at the repository's root.

This allows us to move the bazelrc and workspace_status command we have
at the root directory into the examples/ directory as well.

* Update CI to match reality

It looks like it's not easy to make the WORKSPACE based build work
properly, as jsonnet_go depends on copies of Gazelle and rules_go that
are far too old. At least make sure that the MODULE.bazel based builds
are tested.
  • Loading branch information
EdSchouten authored Apr 3, 2024
1 parent 2ff602d commit 8f4598f
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 59 deletions.
32 changes: 19 additions & 13 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
---
platforms:
ubuntu1804:
matrix:
platform:
- ubuntu2204
- macos

tasks:
rules_jsonnet:
name: rules_jsonnet
platform: ${{ platform }}
build_targets:
- "//..."
- "@examples//..."
- //...

examples:
name: examples
platform: ${{ platform }}
working_directory: examples
test_flags:
- --define
- jsonnet_port=${{ jsonnet_port }}
test_targets:
- "//..."
- "@examples//..."
macos:
build_targets:
- "//..."
- "@examples//..."
test_targets:
- "//..."
- "@examples//..."
- //...
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples
3 changes: 0 additions & 3 deletions .bazelrc

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/bazel-*
bazel-*
# These files are not determinstic/stable, so don't check them in for now.
MODULE.bazel.lock
8 changes: 8 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module(
name = "rules_jsonnet",
version = "0.5.0",
repo_name = "io_bazel_rules_jsonnet",
)

bazel_dep(name = "jsonnet", version = "0.20.0")
bazel_dep(name = "jsonnet_go", version = "0.20.0", repo_name = "google_jsonnet_go")
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ These are build rules for working with [Jsonnet][jsonnet] files with Bazel.

## Setup

To use the Jsonnet rules, add the following to your `WORKSPACE` file to add the
external repositories for Jsonnet:
To use the Jsonnet rules, add the following to your `MODULE.bazel` file:

```python
bazel_dep(name = "rules_jsonnet", version = "0.5.0")
```

If you are using an older version of Bazel that does not support Bzlmod,
add the following to your `WORKSPACE` file to add the external
repositories for Jsonnet:

```python
http_archive(
name = "io_bazel_rules_jsonnet",
sha256 = "d20270872ba8d4c108edecc9581e2bb7f320afab71f8caa2f6394b5202e8a2c3",
strip_prefix = "rules_jsonnet-0.4.0",
urls = ["https://github.com/bazelbuild/rules_jsonnet/archive/0.4.0.tar.gz"],
sha256 = "c51ba0dba41d667fa5c64e56e252ba54be093e5ae764af6470dabca901f373eb",
strip_prefix = "rules_jsonnet-0.5.0",
urls = ["https://github.com/bazelbuild/rules_jsonnet/archive/0.5.0.tar.gz"],
)
load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_repositories")

Expand Down
6 changes: 0 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ workspace(name = "io_bazel_rules_jsonnet")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

local_repository(
name = "examples",
path = "examples",
)

local_repository(
name = "docs",
path = "docs",
)

# TODO: Move this to examples/WORKSPACE when recursive repositories are enabled.
load("//jsonnet:jsonnet.bzl", "jsonnet_repositories")

jsonnet_repositories()
Expand Down
File renamed without changes.
16 changes: 3 additions & 13 deletions examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ package(default_visibility = ["//visibility:public"])
# for jsonnet rules. The BUILD rules should not contain any jsonnet_to_json
# rules as this is redundant with jsonnet_to_json_test rules.

load(
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
"jsonnet_to_json_test",
)
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library", "jsonnet_to_json_test")

jsonnet_library(
name = "workflow",
Expand Down Expand Up @@ -143,10 +139,7 @@ jsonnet_to_json_test(

filegroup(
name = "test_str_files",
srcs = [
"WORKSPACE",
"file.txt",
],
srcs = ["file.txt"],
)

filegroup(
Expand All @@ -166,10 +159,7 @@ jsonnet_to_json_test(
"codefile2",
],
ext_code_files = [":test_code_files"],
ext_str_file_vars = [
"0-workspace",
"1-test",
],
ext_str_file_vars = ["1-test"],
ext_str_files = [":test_str_files"],
golden = "extvar_filegroup_golden.json",
)
Expand Down
10 changes: 10 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module(
name = "examples",
version = "0.0.0",
)

bazel_dep(name = "rules_jsonnet", version = "0.5.0")
local_path_override(
module_name = "rules_jsonnet",
path = "..",
)
1 change: 0 additions & 1 deletion examples/WORKSPACE

This file was deleted.

2 changes: 0 additions & 2 deletions examples/extvar_filegroup.jsonnet
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
local test = std.extVar('1-test');
local workspace = std.extVar('0-workspace');
local codefile = std.extVar('codefile');
local codefile2 = std.extVar('codefile2');
{
file1: test,
file2: codefile.weather,
file3: codefile2.weather,
file4: workspace,
}
3 changes: 1 addition & 2 deletions examples/extvar_filegroup_golden.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"file1": "this is great\n",
"file2": "Finally spring!",
"file3": "Finally summer!",
"file4": "workspace(name = \"examples\")\n"
"file3": "Finally summer!"
}
5 changes: 1 addition & 4 deletions examples/imports/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
load(
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
)
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")

jsonnet_library(
name = "a",
Expand Down
5 changes: 1 addition & 4 deletions examples/imports/tier2/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
load(
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
)
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")

jsonnet_library(
name = "c",
Expand Down
5 changes: 1 addition & 4 deletions examples/imports/tier2/tier3/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
load(
"@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
)
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")

jsonnet_library(
name = "b",
Expand Down
2 changes: 1 addition & 1 deletion examples/invalid.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
^RUNTIME ERROR: Foo\.
../examples/invalid\.jsonnet:15:1-13 ($|\$
.*invalid\.jsonnet:15:1-13 ($|\$
During evaluation )
File renamed without changes.

0 comments on commit 8f4598f

Please sign in to comment.