Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MODULE.bazel support to copy.sh plus silent lockless repos #88

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ignoring e2e MODULE.bazel.lock for the time being
# see https://github.com/bazelbuild/bazel/issues/20369
e2e/smoke/MODULE.bazel.lock
bazel-*
.bazelrc.user
.idea/
Expand Down
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
12 changes: 7 additions & 5 deletions 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 Expand Up @@ -151,10 +153,10 @@ def _deb_resolve_impl(rctx):
rctx.file(
"copy.sh",
_COPY_SH_TMPL.format(
# TODO: don't assume the canonical -> apparent repo mapping character, as it might change
# https://bazelbuild.slack.com/archives/C014RARENH0/p1719237766005439
# https://github.com/bazelbuild/bazel/issues/22865
name = rctx.name.split("~")[-1],
# NOTE: the split("~") is needed when we run bazel from another
# directory, e.g. when running e2e tests we change dir to e2e/smoke
# and then rctx.name is 'rules_distroless~~apt~bullseye'
name = rctx.name.split("~")[-1].replace("_resolve", ""),
label = locklabel,
workspace_relative_path = (("%s/" % locklabel.package) if locklabel.package else "") + locklabel.name,
),
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
Loading