Skip to content

Commit

Permalink
Add support for BAZELISK_NOJDK to Go Bazelisk (#531)
Browse files Browse the repository at this point in the history
* TEST: test_BAZELISK_NOJDK

Passes for Python, fails for Go

* REFACTOR: introduce variable - flavor

* REFACTOR: introduce parameter - config

* Add BAZELISK_NOJDK support to Bazelisk Go
  • Loading branch information
sf-jbazuzi authored Jan 23, 2024
1 parent 88d3724 commit 0c6620a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
30 changes: 30 additions & 0 deletions bazelisk_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ function test_bazel_last_downstream_green() {
(echo "FAIL: 'bazelisk version' of an unreleased binary must not print a build label."; exit 1)
}

function test_BAZELISK_NOJDK() {
setup

# Running the nojdk Bazel without a valid JAVA is expected to fail
set +e
BAZELISK_HOME="$BAZELISK_HOME" \
USE_BAZEL_VERSION="7.0.0" \
BAZELISK_NOJDK="1" \
bazelisk --noautodetect_server_javabase version 2>&1 | tee log
set -e

grep "FATAL: Could not find embedded or explicit server javabase, and --noautodetect_server_javabase is set." log || \
(echo "FAIL: nojdk Bazel should fail when no JDK is supplied."; exit 1)

# Theoretically there could be a cache collision in the Bazelisk cache between nojdk and regular Bazel.
#
# Ensure that isn't happening by running the regular Bazel right after nojdk Bazel, just in case. If there is a collision, it will fail.
BAZELISK_HOME="$BAZELISK_HOME" \
USE_BAZEL_VERSION="7.0.0" \
JAVA_HOME="does/not/exist" \
bazelisk --noautodetect_server_javabase version 2>&1 | tee log

grep "Build label: 7.0.0" log || \
(echo "FAIL: Expected to find 'Build label: 7.0.0' in the output of 'bazelisk version'"; exit 1)
}

function test_bazel_last_rc() {
setup

Expand Down Expand Up @@ -491,6 +517,10 @@ echo "# test_bazel_last_downstream_green"
test_bazel_last_downstream_green
echo

echo "# test_BAZELISK_NOJDK"
test_BAZELISK_NOJDK
echo

if [[ $BAZELISK_VERSION == "GO" ]]; then
echo "# test_bazel_version_go"
test_bazel_version_go
Expand Down
2 changes: 1 addition & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func downloadBazel(bazelVersionString string, bazeliskHome string, repos *Reposi
// downloads/metadata/[fork-or-url]/bazel-[version-os-etc] is a text file containing a hex sha256 of the contents of the downloaded bazel file.
// downloads/sha256/[sha256]/bin/bazel[extension] contains the bazel with a particular sha256.
func downloadBazelIfNecessary(version string, bazeliskHome string, bazelForkOrURLDirName string, repos *Repositories, config config.Config, downloader DownloadFunc) (string, error) {
pathSegment, err := platforms.DetermineBazelFilename(version, false)
pathSegment, err := platforms.DetermineBazelFilename(version, false, config)
if err != nil {
return "", fmt.Errorf("could not determine path segment to use for Bazel binary: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (r *Repositories) DownloadFromBaseURL(baseURL, version, destDir, destFile s
return "", fmt.Errorf("%s is not set", BaseURLEnv)
}

srcFile, err := platforms.DetermineBazelFilename(version, true)
srcFile, err := platforms.DetermineBazelFilename(version, true, config)
if err != nil {
return "", err
}
Expand Down
1 change: 1 addition & 0 deletions platforms/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/bazelbuild/bazelisk/platforms",
visibility = ["//visibility:public"],
deps = [
"//config:go_default_library",
"//versions:go_default_library",
"@com_github_hashicorp_go_version//:go_default_library",
],
Expand Down
13 changes: 11 additions & 2 deletions platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"runtime"

"github.com/bazelbuild/bazelisk/config"
"github.com/bazelbuild/bazelisk/versions"
semver "github.com/hashicorp/go-version"
)
Expand Down Expand Up @@ -82,7 +83,15 @@ func DetermineOperatingSystem() (string, error) {
}

// DetermineBazelFilename returns the correct file name of a local Bazel binary.
func DetermineBazelFilename(version string, includeSuffix bool) (string, error) {
func DetermineBazelFilename(version string, includeSuffix bool, config config.Config) (string, error) {
flavor := "bazel"

bazeliskNojdk := config.Get("BAZELISK_NOJDK")

if len(bazeliskNojdk) != 0 && bazeliskNojdk != "0" {
flavor = "bazel_nojdk"
}

osName, err := DetermineOperatingSystem()
if err != nil {
return "", err
Expand All @@ -98,7 +107,7 @@ func DetermineBazelFilename(version string, includeSuffix bool) (string, error)
filenameSuffix = DetermineExecutableFilenameSuffix()
}

return fmt.Sprintf("bazel-%s-%s-%s%s", version, osName, machineName, filenameSuffix), nil
return fmt.Sprintf("%s-%s-%s-%s%s", flavor, version, osName, machineName, filenameSuffix), nil
}

// DarwinFallback Darwin arm64 was supported since 4.1.0, before 4.1.0, fall back to x86_64
Expand Down
6 changes: 3 additions & 3 deletions repositories/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ type GcsListResponse struct {

// DownloadRelease downloads the given Bazel release into the specified location and returns the absolute path.
func (gcs *GCSRepo) DownloadRelease(version, destDir, destFile string, config config.Config) (string, error) {
srcFile, err := platforms.DetermineBazelFilename(version, true)
srcFile, err := platforms.DetermineBazelFilename(version, true, config)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (gcs *GCSRepo) DownloadCandidate(version, destDir, destFile string, config
return "", fmt.Errorf("'%s' does not refer to a release candidate", version)
}

srcFile, err := platforms.DetermineBazelFilename(version, true)
srcFile, err := platforms.DetermineBazelFilename(version, true, config)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func (gcs *GCSRepo) GetRollingVersions(bazeliskHome string) ([]string, error) {

// DownloadRolling downloads the given Bazel version into the specified location and returns the absolute path.
func (gcs *GCSRepo) DownloadRolling(version, destDir, destFile string, config config.Config) (string, error) {
srcFile, err := platforms.DetermineBazelFilename(version, true)
srcFile, err := platforms.DetermineBazelFilename(version, true, config)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion repositories/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type gitHubRelease struct {

// DownloadVersion downloads a Bazel binary for the given version and fork to the specified location and returns the absolute path.
func (gh *GitHubRepo) DownloadVersion(fork, version, destDir, destFile string, config config.Config) (string, error) {
filename, err := platforms.DetermineBazelFilename(version, true)
filename, err := platforms.DetermineBazelFilename(version, true, config)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 0c6620a

Please sign in to comment.