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(Bazel): Support Bazel 7.2.0 #8909

Merged
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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ COPY --from=dotnetbuild /opt/dotnet /opt/dotnet
# BAZEL
FROM base as bazelbuild

ARG BAZEL_VERSION
ARG BAZELISK_VERSION

ENV BAZEL_HOME=/opt/bazel

RUN mkdir -p $BAZEL_HOME/bin \
&& if [ "$(arch)" = "aarch64" ]; then \
curl -L https://github.com/bazelbuild/bazel/releases/download/$BAZEL_VERSION/bazel-$BAZEL_VERSION-linux-arm64 -o $BAZEL_HOME/bin/bazel; \
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v$BAZELISK_VERSION/bazelisk-linux-arm64 -o $BAZEL_HOME/bin/bazel; \
else \
curl -L https://github.com/bazelbuild/bazel/releases/download/$BAZEL_VERSION/bazel-$BAZEL_VERSION-linux-x86_64 -o $BAZEL_HOME/bin/bazel; \
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v$BAZELISK_VERSION/bazelisk-linux-amd64 -o $BAZEL_HOME/bin/bazel; \
fi \
&& chmod a+x $BAZEL_HOME/bin/bazel

Expand Down
2 changes: 1 addition & 1 deletion docker/versions.dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG ANDROID_CMD_VERSION=11076708
ARG ASKALONO_VERSION=0.4.6
ARG BAZEL_VERSION=7.0.1
ARG BAZELISK_VERSION=1.20.0
ARG BOWER_VERSION=1.8.14
ARG BOYTERLC_VERSION=1.3.1
ARG COCOAPODS_VERSION=1.15.2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
---
project:
id: "Bazel::plugins/package-managers/bazel/src/funTest/assets/projects/synthetic/bazel-7.2/MODULE.bazel:"
definition_file_path: "<REPLACE_DEFINITION_FILE_PATH>"
declared_licenses: []
declared_licenses_processed: {}
vcs:
type: "Git"
url: "<REPLACE_URL_PROCESSED>"
revision: "<REPLACE_REVISION>"
path: "<REPLACE_PATH>"
vcs_processed:
type: "Git"
url: "<REPLACE_URL_PROCESSED>"
revision: "<REPLACE_REVISION>"
path: "<REPLACE_PATH>"
homepage_url: ""
scopes:
- name: "dev"
dependencies:
- id: "Bazel::googletest:1.14.0"
linkage: "STATIC"
dependencies:
- id: "Bazel::abseil-cpp:20230125.1"
linkage: "STATIC"
dependencies:
- id: "Bazel::bazel_skylib:1.6.1"
linkage: "STATIC"
dependencies:
- id: "Bazel::platforms:0.0.9"
linkage: "STATIC"
- id: "Bazel::platforms:0.0.9"
linkage: "STATIC"
- id: "Bazel::rules_cc:0.0.9"
linkage: "STATIC"
- id: "Bazel::platforms:0.0.9"
linkage: "STATIC"
dependencies:
- id: "Bazel::rules_license:0.0.7"
linkage: "STATIC"
- id: "Bazel::rules_cc:0.0.9"
linkage: "STATIC"
dependencies:
- id: "Bazel::platforms:0.0.9"
linkage: "STATIC"
- name: "main"
dependencies:
- id: "Bazel::glog:0.5.0"
linkage: "STATIC"
dependencies:
- id: "Bazel::gflags:2.2.2"
linkage: "STATIC"
packages:
- id: "Bazel::abseil-cpp:20230125.1"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/abseil/abseil-cpp"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/abseil/abseil-cpp/archive/refs/tags/20230125.1.tar.gz"
hash:
value: "81311c17599b3712069ded20cca09a62ab0bf2a89dfa16993786c8782b7ed145"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/abseil/abseil-cpp"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/abseil/abseil-cpp.git"
revision: ""
path: ""
- id: "Bazel::bazel_skylib:1.6.1"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/bazelbuild/bazel-skylib"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/bazelbuild/bazel-skylib/releases/download/1.6.1/bazel-skylib-1.6.1.tar.gz"
hash:
value: "9f38886a40548c6e96c106b752f242130ee11aaa068a56ba7e56f4511f33e4f2"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/bazelbuild/bazel-skylib"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/bazelbuild/bazel-skylib.git"
revision: ""
path: ""
- id: "Bazel::gflags:2.2.2"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://gflags.github.io/gflags/"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/gflags/gflags/archive/refs/tags/v2.2.2.tar.gz"
hash:
value: "34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/gflags/gflags"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/gflags/gflags.git"
revision: ""
path: ""
- id: "Bazel::glog:0.5.0"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/google/glog"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/google/glog/archive/refs/tags/v0.5.0.tar.gz"
hash:
value: "eede71f28371bf39aa69b45de23b329d37214016e2055269b3b5e7cfd40b59f5"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/google/glog"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/google/glog.git"
revision: ""
path: ""
- id: "Bazel::googletest:1.14.0"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://google.github.io/googletest/"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz"
hash:
value: "8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/google/googletest"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/google/googletest.git"
revision: ""
path: ""
- id: "Bazel::platforms:0.0.9"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/bazelbuild/platforms"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/bazelbuild/platforms/releases/download/0.0.9/platforms-0.0.9.tar.gz"
hash:
value: "5eda539c841265031c2f82d8ae7a3a6490bd62176e0c038fc469eabf91f6149b"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/bazelbuild/platforms"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/bazelbuild/platforms.git"
revision: ""
path: ""
- id: "Bazel::rules_cc:0.0.9"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/bazelbuild/rules_cc"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"
hash:
value: "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf"
algorithm: "SHA-256"
vcs:
type: "Git"
url: ""
revision: ""
path: ""
vcs_processed:
type: "Git"
url: ""
revision: ""
path: ""
- id: "Bazel::rules_license:0.0.7"
purl: "pkg:generic/[email protected]"
declared_licenses: []
declared_licenses_processed: {}
description: ""
homepage_url: "https://github.com/bazelbuild/rules_license"
binary_artifact:
url: ""
hash:
value: ""
algorithm: ""
source_artifact:
url: "https://github.com/bazelbuild/rules_license/releases/download/0.0.7/rules_license-0.0.7.tar.gz"
hash:
value: "4531deccb913639c30e5c7512a054d5d875698daeb75d8cf90f284375fe7c360"
algorithm: "SHA-256"
vcs:
type: "Git"
url: "https://github.com/bazelbuild/rules_license"
revision: ""
path: ""
vcs_processed:
type: "Git"
url: "https://github.com/bazelbuild/rules_license.git"
revision: ""
path: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bazel_dep(name = "glog", version = "0.5.0", repo_name = "com_github_google_glog")
bazel_dep(name = "googletest", version = "1.14.0", repo_name = "com_google_googletest", dev_dependency = True)
fviernau marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.0.1
fviernau marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ class BazelFunTest : StringSpec({

result.toYaml() should matchExpectedResult(expectedResultFile, definitionFile)
}

"Dependencies are detected correctly with Bazel 7.2.0" {
val definitionFile = getAssetFile("projects/synthetic/bazel-7.2/MODULE.bazel")
val expectedResultFile = getAssetFile("projects/synthetic/bazel-7.2-expected-output.yml")

val result = create("Bazel").resolveSingleProject(definitionFile)

result.toYaml() should matchExpectedResult(expectedResultFile, definitionFile)
}
})
56 changes: 45 additions & 11 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.ossreviewtoolkit.utils.common.withoutPrefix
import org.semver4j.RangesList
import org.semver4j.RangesListFactory

private const val BAZEL_FALLBACK_VERSION = "7.0.1"
private const val LOCKFILE_NAME = "MODULE.bazel.lock"

class Bazel(
Expand All @@ -84,7 +85,10 @@ class Bazel(
args = args,
workingDir = workingDir,
// Disable the optional wrapper script under `tools/bazel`, to ensure the --version option works.
environment = environment + mapOf("BAZELISK_SKIP_WRAPPER" to "true")
environment = environment + mapOf(
"BAZELISK_SKIP_WRAPPER" to "true",
"USE_BAZEL_FALLBACK_VERSION" to BAZEL_FALLBACK_VERSION
)
)

override fun transformVersion(output: String) = output.removePrefix("bazel ")
Expand All @@ -105,16 +109,7 @@ class Bazel(
// If no lockfile is present, getDependencyGraph() runs "bazel mod graph", which creates a MODULE.bazel.lock
// file as a side effect. That file contains the URL of the Bazel module registry that was used for dependency
// resolution.
val registryUrl = parseLockfile(lockfile).registryUrl()

val registry = registryUrl.withoutPrefix("file://")?.let {
val localRegistryURL = it.replace("%workspace%", projectDir.absolutePath)
logger.info {
"Using local Bazel module registry at '$localRegistryURL'."
}

LocalBazelModuleRegistryService(File(localRegistryURL))
} ?: RemoteBazelModuleRegistryService.create(registryUrl)
val registry = determineRegistry(parseLockfile(lockfile), projectDir)

val packages = getPackages(scopes, registry)

Expand All @@ -139,6 +134,45 @@ class Bazel(
)
}

/**
* This function determines the Bazel module registry to use based on the given [lockfile]: If this is a lockfile
* generated by Bazel version >= 7.2.0, a [CompositeBazelModuleRegistryService] based on the "registryFileHashes"
* will be returned. Else, either a [LocalBazelModuleRegistryService] or a [RemoteBazelModuleRegistryService] based
* on the "cmdRegistries" will be returned.
*/
private fun determineRegistry(lockfile: Lockfile, projectDir: File): BazelModuleRegistryService {
// Bazel version < 7.2.0.
if (lockfile.flags != null) {
val registryUrl = lockfile.registryUrl()

return registryUrl.withoutPrefix("file://")?.let {
val localRegistryURL = it.replace("%workspace%", projectDir.absolutePath)
nnobelis marked this conversation as resolved.
Show resolved Hide resolved
logger.info {
"Using local Bazel module registry at '$localRegistryURL'."
}

LocalBazelModuleRegistryService(File(localRegistryURL))
} ?: RemoteBazelModuleRegistryService.create(registryUrl)
}

// Bazel version >= 7.2.0.
if (lockfile.registryFileHashes != null) {
val registryFileHashes = lockfile.registryFileHashes.map { (url, _) ->
if (url.startsWith("file://")) {
url.replace("%workspace%", projectDir.absolutePath)
} else {
url
}
}.toSet()

return CompositeBazelModuleRegistryService.create(registryFileHashes)
}

val msg = "Bazel registry URL cannot be determined from the lockfile."
logger.error(msg)
error(msg)
}

private fun getPackages(scopes: Set<Scope>, registry: BazelModuleRegistryService): Set<Package> {
val ids = scopes.collectDependencies()
val moduleMetadataForId = ids.associateWith { getModuleMetadata(it, registry) }
Expand Down
Loading
Loading