Skip to content

Commit

Permalink
Add version specific bazelrc
Browse files Browse the repository at this point in the history
  • Loading branch information
restingbull committed Jul 5, 2024
1 parent 0334284 commit a78f812
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 47 deletions.
49 changes: 19 additions & 30 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@ buildifier:
# no lint warnings for the moment. They are basically a smoke alarm in hell.
# keep this argument in sync with .pre-commit-config.yaml
warnings: "-confusing-name,-constant-glob,-duplicated-name,-function-docstring,-function-docstring-args,-function-docstring-header,-module-docstring,-name-conventions,-no-effect,-constant-glob,-provider-params,-print,-rule-impl-return,-bzl-visibility,-unnamed-macro,-uninitialized,-unreachable"
matrix:
platform:
- ubuntu2004
- ubuntu1804
- macos
- windows
bzlmod:
- true
- false
tasks:
ubuntu1804:
name: "Tests without Bzlmod (Ubuntu 18.04)"
unittests:
name: "Unittests"
platform: ${{ platform }}
build_flags:
- "--enable_bzlmod=false"
- "--enable_bzlmod=${{ bzlmod }}"
test_flags:
- "--enable_bzlmod=false"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
ubuntu2004_bzlmod:
name: "Tests (Ubuntu 20.04)"
platform: ubuntu2004
- "--enable_bzlmod=${{ bzlmod }}"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
macos:
name: "Tests (Latest Macos)"
bazel_integration_tests:
platform: $${{ platform }}
test_flags:
- "--enable_bzlmod=true"
test_targets:
- "//:all_tests"
build_targets:
- "//:rules_kotlin_release"
- //examples:all
rbe_ubuntu1604:
build_flags:
- "--enable_bzlmod=false"
Expand All @@ -48,20 +51,6 @@ tasks:
# cannot be used with remote builds)
- "--strategy=KotlinCompile=remote"
- "--config=rbe"
bazel_integration_tests_ubuntu1804:
name: "Bazel Integration Tests (Ubuntu 18.04)"
platform: ubuntu1804
test_flags:
- "--enable_bzlmod=true"
test_targets:
- //examples:all
bazel_integration_tests_ubuntu2004:
name: "Bazel Integration Tests (Ubuntu 20.04)"
platform: ubuntu2004
test_flags:
- "--enable_bzlmod=true"
test_targets:
- //examples:all
stardoc:
name: Stardoc api documentation
platform: ubuntu1804
Expand Down
1 change: 0 additions & 1 deletion examples/android/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
common --lockfile_mode=off
common --incompatible_enable_android_toolchain_resolution
common --android_platforms=//:arm64-v8a
1 change: 0 additions & 1 deletion examples/anvil/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
common --lockfile_mode=off
common --incompatible_enable_android_toolchain_resolution
common --android_platforms=//:arm64-v8a
1 change: 0 additions & 1 deletion examples/deps/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
common --lockfile_mode=off
common --incompatible_enable_android_toolchain_resolution
common --android_platforms=//:arm64-v8a
71 changes: 57 additions & 14 deletions src/main/kotlin/io/bazel/kotlin/test/BazelIntegrationTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ object BazelIntegrationTestRunner {
}
}

val version = bazel.run(workspace, "--bazelrc=/dev/null", "version").parseVersion()

val bazelrc = resolveBazelrc(workspace, version)

listOf(true, false)
.filter { bzlmod ->
bzlmod && workspace.hasModule() || !bzlmod && workspace.hasWorkspace()
Expand All @@ -50,49 +54,67 @@ object BazelIntegrationTestRunner {
bazel.run(
workspace,
"info",
"--bazelrc=$bazelrc",
"--enable_bzlmod=$bzlmod",
"--override_repository=rules_kotlin=$unpack",
).onFailure { err ->
throw err
}
).onFailThrow()
bazel.run(
workspace,
"build",
"--bazelrc=$bazelrc",
"--enable_bzlmod=$bzlmod",
"--override_repository=rules_kotlin=$unpack",
"//...",
).onFailure { err ->
throw err
}
).onFailThrow()
bazel.run(
workspace,
"query",
"--bazelrc=$bazelrc",
"--enable_bzlmod=$bzlmod",
"--override_repository=rules_kotlin=$unpack",
"kind(\".*_test\", \"//...\")",
).onFailure {
err ->
throw err
}.onSuccess { process ->
).ok { process ->
if (process.inputStream.readAllBytes().isNotEmpty()) {
bazel.run(
workspace,
"test",
"--bazelrc=$bazelrc",
"--enable_bzlmod=$bzlmod",
"--override_repository=rules_kotlin=$unpack",
"//...",
).onFailure { err ->
throw err
}
).onFailThrow()
}
}

}
}

fun Path.hasModule() = resolve("MODULE").exists() || resolve("MODULE.bazel").exists()
fun Path.hasWorkspace() = resolve("WORKSPACE").exists() || resolve("WORKSPACE.bazel").exists()

data class Version(val major: Int, val minor: Int, val patch: Int)

fun Result<Process>.parseVersion(): Version {
ok { process ->
process.inputStream.bufferedReader(UTF_8).use { reader ->
generateSequence(reader::readLine)
.find { line -> line.startsWith("Build label: ") }
?.let { line ->
val parts = line.substring("Build label: ".length).split(".")
return Version(parts[0].toInt(), parts[1].toInt(), parts[2].toInt())
}
throw IllegalStateException("VBazel version not available")
}
}
}

private fun Result<Process>.onFailThrow() = onFailure {
throw it
}

private inline fun <R> Result<Process>.ok(action: (Process) -> R) = fold(
onSuccess = action,
onFailure = { err -> throw err },
)

fun Path.run(inDirectory: Path, vararg args: String): Result<Process> = ProcessBuilder()
.command(this.toString(), *args)
Expand All @@ -114,4 +136,25 @@ object BazelIntegrationTestRunner {
),
)
}

fun resolveBazelrc(workspace: Path, version: Version): Path {
workspace.resolve(".bazelrc.${version.major}-${version.minor}-${version.patch}")
.takeIf(Path::exists)?.let {
return it
}
workspace.resolve(".bazelrc.${version.major}-${version.minor}").takeIf(Path::exists)?.let {
return it
}
workspace.resolve(".bazelrc.${version.major}").takeIf(Path::exists)?.let {
return it
}
workspace.resolve(".bazelrc.${version.major}").takeIf(Path::exists)?.let {
return it
}
workspace.resolve(".bazelrc").takeIf(Path::exists)?.let {
return it
}
return workspace.resolve("/dev/null")
}

}

0 comments on commit a78f812

Please sign in to comment.