Skip to content

Commit

Permalink
feat: add MODULE.bazel support to copy.sh plus silent lockless repos (#…
Browse files Browse the repository at this point in the history
…88)

* feat: add support for MODULE.bazel to the lock copy.sh script

Add support for MODULE.bazel to the lock script and avoid printing an
unnecessary (and annoying 😅) error when building in a "modern repo".

* feat: avoid DEBUG messages for lockless repos

Add a `nolock` attribute to avoid getting annoying DEBUG messages for
repos that we explicitly want to run without a lock.
  • Loading branch information
jjmaestro authored Oct 28, 2024
1 parent 7853bcd commit 7ba8729
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
3 changes: 1 addition & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ load("@bullseye//:packages.bzl", "bullseye_packages")

bullseye_packages()

# bazel run @apt_security//:lock
deb_index(
name = "apt_security",
# lock = "//examples/debian_snapshot_security:security.lock.json",
manifest = "//examples/debian_snapshot_security:security.yaml",
nolock = True,
)

load("@apt_security//:packages.bzl", "apt_security_packages")
Expand Down
9 changes: 7 additions & 2 deletions apt/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def _distroless_extension(module_ctx):
install.resolve_transitive,
)

# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % install.name)
if not install.nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % install.name)
else:
lockf = lockfile.from_json(module_ctx, module_ctx.read(install.lock))

Expand Down Expand Up @@ -65,6 +66,10 @@ def _distroless_extension(module_ctx):
install = tag_class(attrs = {
"name": attr.string(doc = "Name of the generated repository"),
"lock": attr.label(doc = """The lock file to use for the index."""),
"nolock": attr.bool(
doc = """If you explicitly want to run without a lock, set it to True to avoid the DEBUG messages.""",
default = False,
),
"manifest": attr.label(doc = """The file used to generate the lock file"""),
"resolve_transitive": attr.bool(
doc = """Whether dependencies of dependencies should be resolved and added to the lockfile.""",
Expand Down
14 changes: 11 additions & 3 deletions apt/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def deb_index(
name,
manifest,
lock = None,
nolock = False,
package_template = None,
resolve_transitive = True):
"""A convience repository macro for apt rules.
Expand All @@ -20,9 +21,13 @@ def deb_index(
deb_index(
name = "bullseye",
# For the initial setup, the lockfile attribute can be omitted and generated by running
# For the initial setup, the lockfile attribute can be omitted and
# generated by running
# bazel run @bullseye//:lock
# This will generate the lock.json file next to the manifest file by replacing `.yaml` with `.lock.json`
# This will generate the lock.json file next to the manifest file by
# replacing `.yaml` with `.lock.json`.
# If you explicitly want to run without a lock, set nolock to True to
# avoid the DEBUG messages
lock = "//examples/apt:bullseye.lock.json",
manifest = "//examples/apt:bullseye.yaml",
)
Expand Down Expand Up @@ -57,6 +62,8 @@ def deb_index(
While we strongly encourage users to check in the generated lockfile, it's not always possible because Debian repositories are rolling by default. Therefore, a lockfile generated today might not work later if the upstream repository removes or publishes a new version of a package.
If you explicitly want to run without a lock, set the `nolock` attribute to True to avoid the DEBUG messages.
#### Snapshot repositories
##### Debian
Expand All @@ -73,6 +80,7 @@ def deb_index(
name: name of the repository
manifest: label to a `manifest.yaml`
lock: label to a `lock.json`
nolock: bool, set to True if you explicitly want to run without a lock and avoid the DEBUG messages.
package_template: (EXPERIMENTAL!) a template file for generated BUILD files.
Available template replacement keys are: `{target_name}`, `{deps}`, `{urls}`, `{name}`, `{arch}`, `{sha256}`, `{repo_name}`
resolve_transitive: whether dependencies of dependencies should be resolved and added to the lockfile.
Expand All @@ -83,7 +91,7 @@ def deb_index(
resolve_transitive = resolve_transitive,
)

if not lock:
if not lock and not nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % name)

Expand Down
4 changes: 3 additions & 1 deletion apt/private/resolve.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ if [ -e $BUILD_WORKSPACE_DIRECTORY/WORKSPACE ]; then
wksp_file="WORKSPACE"
elif [ -e $BUILD_WORKSPACE_DIRECTORY/WORKSPACE.bazel ]; then
wksp_file="WORKSPACE.bazel"
elif [ -e $BUILD_WORKSPACE_DIRECTORY/MODULE.bazel ]; then
wksp_file="MODULE.bazel"
else
echo>&2 "Error: neither WORKSPACE nor WORKSPACE.bazel file was found"
echo>&2 "Error: no MODULE.bazel or WORKSPACE file was found"
exit 1
fi
Expand Down
13 changes: 10 additions & 3 deletions docs/apt.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ apt.install(
)
apt.install(
name = "bullseye_nolock",
# lock = ":bullseye.lock.json",
manifest = ":bullseye.yaml",
nolock = True,
)

# bazel run @bullseye//:lock
Expand Down

0 comments on commit 7ba8729

Please sign in to comment.