Skip to content

Commit d4f8f86

Browse files
authored
feat: 'impacted' task functional and tested (#2)
* implement * go back to simpler * no more root module * fix * fix * fix * fix * fix * fix
1 parent 89d1a47 commit d4f8f86

File tree

11 files changed

+361
-42
lines changed

11 files changed

+361
-42
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ jobs:
2222
- uses: actions/checkout@v5
2323
- uses: bazel-contrib/setup-bazel@0.15.0
2424
- name: Install Aspect CLI
25-
uses: jaxxstorm/action-install-gh-release@v2.1.0
26-
with:
27-
repo: aspect-build/aspect-cli
28-
tag: 2025.42.8
29-
asset-name: aspect-cli
30-
platform: unknown_linux
31-
arch: x86_64
32-
extension-matching: disable
33-
rename-to: aspect
34-
chmod: 0755
25+
run: |
26+
curl -LO https://github.com/aspect-build/aspect-cli/releases/download/v2025.46.20/aspect-cli-x86_64-unknown-linux-musl
27+
chmod +x aspect-cli-x86_64-unknown-linux-musl
28+
mv aspect-cli-x86_64-unknown-linux-musl /usr/local/bin/aspect
3529
- name: Test
3630
working-directory: example
3731
shell: sh

MODULE.aspect

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Public API re-export"
2+
use_task("bazel-diff.axl", "impacted")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# `aspect affected`
1+
# `aspect impacted`
22

3-
This task lets you run only the tests which are "affected" by changes to the sources.
3+
This task lets you run only the tests which are "impacted" by changes to the sources.
44

55
It uses the popular Tinder/bazel-diff utility.
66
As they document on https://github.com/Tinder/bazel-diff#getting-started:

bazel-diff.axl

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
1-
"""Calculate affected targets from the BASE commit
1+
"""Calculate impacted targets from the BASE commit
22

33
Based on
44
https://github.com/Tinder/bazel-diff/blob/master/bazel-diff-example.sh
55
"""
66
def exec(command):
77
return command.stdout("piped").spawn().wait_with_output().stdout.strip()
88

9+
# buildifier: disable=function-docstring
10+
def checkout_merge_base(std: std.Std):
11+
out = std.io.stdout
12+
out.write("Checking out merge base...\n")
13+
current_revision = exec(std.process.command("git").args(["rev-parse", "HEAD"]))
14+
merge_base = exec(std.process.command("git").args(["merge-base", "HEAD", "origin/main"]))
15+
checkout = std.process.command("git").args(["checkout", merge_base, "--quiet"]).spawn()
16+
if checkout.wait().success:
17+
return merge_base, current_revision, None
18+
else:
19+
return None, None, checkout.stderr
920

10-
def impl(ctx: task_context):
11-
out = ctx.std.io.stdout
12-
out.write("Building bazel-diff executable...\n")
13-
build = ctx.build(
14-
ctx.args.bazel_diff_cli or "//tools:bazel-diff",
15-
events = True,
16-
bazel_flags = ["--build_runfile_links"],
21+
# buildifier: disable=function-docstring
22+
def impl(ctx: TaskContext):
23+
ctx.std.io.stdout.write("Building %s executable...\n" % ctx.args.bazel_diff_cli)
24+
build = ctx.bazel.build(
25+
ctx.args.bazel_diff_cli,
26+
build_events = True,
27+
flags = ["--build_runfile_links"],
1728
)
1829
(bazel_diff_command, runfiles) = (None, None)
19-
for event in build.events():
20-
if event.type == "named_set_of_files":
30+
for event in build.build_events():
31+
if event.kind == "named_set_of_files":
2132
for file in event.payload.files:
22-
if len(file.path_prefix) == 0 or file.file.uri.endswith(".jar"):
33+
if len(file.path_prefix) == 0 or file.file.endswith(".jar"):
2334
continue
24-
runfiles = file.file.uri.removeprefix("file://") + ".runfiles"
25-
bazel_diff_command = file.file.uri.removeprefix("file://")
35+
runfiles = file.file.removeprefix("file://") + ".runfiles"
36+
bazel_diff_command = file.file.removeprefix("file://")
2637
if not bazel_diff_command:
2738
ctx.std.io.stderr.write("Error finding bazel-diff executable")
2839
return 1
@@ -37,18 +48,16 @@ def impl(ctx: task_context):
3748
.spawn() \
3849
.wait()
3950

40-
out.write("Checking out merge base...\n")
41-
current_revision = exec(ctx.std.process.command("git").args(["rev-parse", "HEAD"]))
42-
merge_base = exec(ctx.std.process.command("git").args(["merge-base", "HEAD", "origin/main"]))
43-
checkout = ctx.std.process.command("git").args(["checkout", merge_base, "--quiet"]).spawn().wait()
44-
if not checkout.success:
45-
ctx.std.io.err.write("Error checking out merge base: %s" % checkout.stderr)
51+
(merge_base, current_revision, error) = checkout_merge_base(ctx.std)
52+
if error:
53+
ctx.std.io.stderr.write("Error checking out merge base: %s" % error)
4654
return 1
4755

48-
out.write("Generating Hashes for Base Revision %s\n" % merge_base)
4956
starting_hashes_json=ctx.std.env.temp_dir() + "/starting_hashes.json"
5057
final_hashes_json=ctx.std.env.temp_dir() + "/final_hashes.json"
5158
impacted_targets_path=ctx.std.env.temp_dir() + "/impacted_targets.txt"
59+
60+
ctx.std.io.stdout.write("Generating Hashes for Base Revision %s...\n" % merge_base)
5261
generate_hashes = bazel_diff([
5362
"generate-hashes",
5463
"-w", ctx.std.env.current_dir(),
@@ -59,10 +68,10 @@ def impl(ctx: task_context):
5968
ctx.std.io.stderr.write("Error generating hashes: %s" % generate_hashes.code)
6069
return 1
6170

62-
out.write("Restoring current revision...\n")
63-
restore = ctx.std.process.command("git").args(["checkout", current_revision, "--quiet"]).spawn().wait()
64-
if not restore.success:
65-
ctx.std.io.err.write("Error checking out original commit: %s" % checkout.stderr)
71+
ctx.std.io.stdout.write("Restoring current revision...\n")
72+
restore = ctx.std.process.command("git").args(["checkout", current_revision, "--quiet"]).spawn()
73+
if not restore.wait().success:
74+
ctx.std.io.stderr.write("Error checking out original commit: %s" % restore.stderr)
6675
return 1
6776

6877
generate_hashes = bazel_diff([
@@ -86,11 +95,12 @@ def impl(ctx: task_context):
8695
return 1
8796

8897
ctx.std.io.stdout.write("Impacted Targets for Revision %s: %s" % (current_revision, ctx.std.fs.read_to_string(impacted_targets_path)))
98+
# TODO: offer to test the impacted targets by running `bazel test` on them
8999
return 0
90100

91-
affected = task(
101+
impacted = task(
92102
implementation = impl,
93103
args = {
94-
"bazel_diff_cli": args.string(),
104+
"bazel_diff_cli": args.string(default = "//tools:bazel-diff"),
95105
}
96106
)

example/.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Repository rules, such as rules_jvm_external: put Bazel's JDK on the path.
2+
# Avoids non-hermeticity from dependency on a JAVA_HOME pointing at a system JDK
3+
# see https://github.com/bazelbuild/rules_jvm_external/issues/445
4+
common --repo_env="JAVA_HOME=../bazel_tools/jdk"

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*

example/MODULE.aspect

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
axl_local_dep(
2+
name = "impacted",
3+
path = "..",
4+
auto_use_tasks = True,
5+
)

example/MODULE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bazel_dep(name = "rules_java", version = "8.16.1")
2+
3+
http_jar = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
4+
http_jar(
5+
name = "bazel-diff",
6+
urls = [
7+
"https://github.com/Tinder/bazel-diff/releases/download/10.1.0/bazel-diff_deploy.jar"
8+
],
9+
integrity = "sha256-1UKqi+n0hVABtd3hE8A7kmRJO9YAlESg+AWUx6zyar4=",
10+
)

0 commit comments

Comments
 (0)