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

[7.1.0] Generate a lockfile for the distribution archive on the fly #21338

Merged
merged 2 commits into from
Feb 14, 2024
Merged
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
22 changes: 22 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ filegroup(
srcs = glob(
["*"],
exclude = [
"MODULE.bazel.lock", # Use MODULE.bazel.lock.dist instead
"WORKSPACE.bzlmod", # Needs to be filtered.
"bazel-*", # convenience symlinks
"out", # IntelliJ with setup-intellij.sh
"output", # output of compile.sh
".*", # mainly .git* files
],
) + [
"//:MODULE.bazel.lock.dist",
"//:WORKSPACE.bzlmod.filtered",
"//examples:srcs",
"//scripts:srcs",
Expand Down Expand Up @@ -96,6 +98,25 @@ genrule(
]),
)

genrule(
name = "generate_dist_lockfile",
srcs = [
"MODULE.bazel",
"//third_party/googleapis:MODULE.bazel",
"//third_party/remoteapis:MODULE.bazel",
],
outs = ["MODULE.bazel.lock.dist"],
cmd = "touch BUILD && " +
"trap 'rm -rf tmp_bazel_root' EXIT && " +
# Instead of `bazel mod deps`, we run a simpler command like `bazel query :all` here
# so that we only trigger module resolution, not extension eval.
# Also use `--batch` so that Bazel doesn't keep a server process alive.
"$(location //src:bazel) --batch --output_user_root=$$PWD/tmp_bazel_root query --check_direct_dependencies=error --lockfile_mode=update :all && " +
"mv MODULE.bazel.lock $@",
tags = ["requires-network"],
tools = ["//src:bazel"],
)

pkg_tar(
name = "bootstrap-jars",
srcs = [
Expand Down Expand Up @@ -151,6 +172,7 @@ pkg_tar(
],
# TODO(aiuto): Replace with pkg_filegroup when that is available.
remap_paths = {
"MODULE.bazel.lock.dist": "MODULE.bazel.lock",
"WORKSPACE.bzlmod.filtered": "WORKSPACE.bzlmod",
# Rewrite paths coming from local repositories back into third_party.
"external/googleapis~override": "third_party/googleapis",
Expand Down
9 changes: 1 addition & 8 deletions scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,8 @@ if [ -z "${BAZEL_SKIP_JAVA_COMPILATION}" ]; then
workspace(name = 'bazel_tools')
EOF

# Set up the MODULE.bazel file for `bazel_tools` and update the hash in the lockfile.
# Set up the MODULE.bazel file for `bazel_tools`
link_file "${PWD}/src/MODULE.tools" "${BAZEL_TOOLS_REPO}/MODULE.bazel"
new_hash=$(shasum -a 256 "${BAZEL_TOOLS_REPO}/MODULE.bazel" | awk '{print $1}')
sed -i.bak "/\"bazel_tools\":/s/\"[a-f0-9]*\"/\"$new_hash\"/" MODULE.bazel.lock
# TODO: Temporary hack for lockfile version mismatch, remove these lines after updating to 7.1.0
sed -i.bak 's/"lockFileVersion": 3/"lockFileVersion": 4/' MODULE.bazel.lock
# Replace canonical repository names and parts thereof of the form rules_foo~1.2.3 with rules_foo~
sed -i.bak -E 's/([a-z]([a-z0-9._-]*[a-z0-9]){0,1})~[a-zA-Z0-9.]{1,}(-[0-9.-]{1,}){0,1}(\+[0-9.-]{1,}){0,1}/\1/g' MODULE.bazel.lock
rm MODULE.bazel.lock.bak

mkdir -p "${BAZEL_TOOLS_REPO}/src/conditions"
link_file "${PWD}/src/conditions/BUILD.tools" \
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ genrule(
executable = 1,
output_to_bindir = 1,
visibility = [
"//:__pkg__", # For distribution archive lockfile generation
"//scripts:__pkg__", # For bash completion generation
"//scripts/packages:__pkg__", # For installer generation
"//src/java:__subpackages__", # For command line reference generation
Expand Down
12 changes: 0 additions & 12 deletions src/test/shell/bazel/bazel_determinism_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ function test_determinism() {
# Set up the maven repository properly.
cp derived/maven/BUILD.vendor derived/maven/BUILD

# Update the hash of bazel_tools in lockfile to avoid rerunning module resolution.
new_hash=$(shasum -a 256 "src/MODULE.tools" | awk '{print $1}')
sed -i.bak "/\"bazel_tools\":/s/\"[a-f0-9]*\"/\"$new_hash\"/" MODULE.bazel.lock
# TODO: Temporary hack for lockfile version mismatch, remove these lines after updating to 7.1.0
sed -i.bak 's/"lockFileVersion": 3/"lockFileVersion": 4/' MODULE.bazel.lock
# Replace canonical repository names and parts thereof of the form rules_foo~1.2.3 with rules_foo~
sed -i.bak -E 's/([a-z]([a-z0-9._-]*[a-z0-9]){0,1})~[a-zA-Z0-9.]{1,}(-[0-9.-]{1,}){0,1}(\+[0-9.-]{1,}){0,1}/\1/g' MODULE.bazel.lock
rm MODULE.bazel.lock.bak

# Use @bazel_tools//tools/python:autodetecting_toolchain to avoid
# downloading python toolchain.

# Build Bazel once.
bazel \
--output_base="${TEST_TMPDIR}/out1" \
Expand Down
5 changes: 4 additions & 1 deletion third_party/googleapis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ license(
license_text = "LICENSE",
)

exports_files(["LICENSE"])
exports_files([
"LICENSE",
"MODULE.bazel",
])

filegroup(
name = "srcs",
Expand Down
5 changes: 4 additions & 1 deletion third_party/remoteapis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package(default_visibility = ["//visibility:public"])

licenses(["notice"])

exports_files(["LICENSE"])
exports_files([
"LICENSE",
"MODULE.bazel",
])

load("@io_bazel//third_party/grpc:build_defs.bzl", "java_grpc_library")
load("@io_bazel//tools/build_rules:utilities.bzl", "java_library_srcs")
Expand Down
Loading