-
Notifications
You must be signed in to change notification settings - Fork 43
feat: implement run task #1115
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
Open
thesayyn
wants to merge
1
commit into
main
Choose a base branch
from
run_verb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−7
Open
feat: implement run task #1115
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
crates/aspect-cli/src/builtins/aspect/lib/runnable_test.axl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Tests for `lib/runnable.axl`'s label canonicalizer. | ||
|
|
||
| Covers `canonical_label` across: | ||
| - Absolute labels: `//pkg:name`, `//pkg` (shorthand), `//:name`, `@repo//pkg:name`, `@repo//pkg` (shorthand). | ||
| - Relative labels: `:name`, bare `name`, `pkg:name` — both at the workspace root and inside a nested package. | ||
|
|
||
| `_current_package` derives the package from cwd vs workspace root; that path | ||
| needs a real cwd/root pair and is exercised by the live `aspect run` smoke | ||
| in CI rather than mocked here. | ||
|
|
||
| Run with: | ||
| aspect dev test-runnable | ||
| """ | ||
|
|
||
| load("./runnable.axl", "canonical_label") | ||
|
|
||
| def _eq(label, got, want): | ||
| if got != want: | ||
| fail("%s: got %r, want %r" % (label, got, want)) | ||
|
|
||
| def _test_absolute_unchanged_when_target_present(): | ||
| _eq("//foo/bar:baz", canonical_label("//foo/bar:baz"), "//foo/bar:baz") | ||
| _eq("//:foo", canonical_label("//:foo"), "//:foo") | ||
| _eq("//foo/bar:baz with pkg context", canonical_label("//foo/bar:baz", "ignored"), "//foo/bar:baz") | ||
|
|
||
| def _test_absolute_shorthand_expands_basename(): | ||
| _eq("//foo/bar -> //foo/bar:bar", canonical_label("//foo/bar"), "//foo/bar:bar") | ||
| _eq("//foo -> //foo:foo", canonical_label("//foo"), "//foo:foo") | ||
|
|
||
| def _test_external_repo_labels(): | ||
| _eq("@repo//foo:bar", canonical_label("@repo//foo:bar"), "@repo//foo:bar") | ||
| _eq("@repo//foo -> @repo//foo:foo", canonical_label("@repo//foo"), "@repo//foo:foo") | ||
| _eq("@@_main//foo:bar (bzlmod canonical)", canonical_label("@@_main//foo:bar"), "@@_main//foo:bar") | ||
|
|
||
| def _test_relative_colon_name_at_root(): | ||
| _eq(":foo at root -> //:foo", canonical_label(":foo", ""), "//:foo") | ||
|
|
||
| def _test_relative_colon_name_in_nested_package(): | ||
| _eq(":foo in examples/deliverable", canonical_label(":foo", "examples/deliverable"), "//examples/deliverable:foo") | ||
|
|
||
| def _test_bare_name_at_root(): | ||
| _eq("foo at root -> //:foo", canonical_label("foo", ""), "//:foo") | ||
|
|
||
| def _test_bare_name_in_nested_package(): | ||
| _eq("foo in examples -> //examples:foo", canonical_label("foo", "examples"), "//examples:foo") | ||
|
|
||
| def _test_relative_pkg_colon_name_at_root(): | ||
| _eq("sub:foo at root -> //sub:foo", canonical_label("sub:foo", ""), "//sub:foo") | ||
|
|
||
| def _test_relative_pkg_colon_name_in_nested_package(): | ||
| _eq("sub:foo in foo -> //foo/sub:foo", canonical_label("sub:foo", "foo"), "//foo/sub:foo") | ||
|
|
||
| def _test_default_current_package_is_root(): | ||
| """Callers that omit `current_package` (e.g. `_process_bes` on BES events) | ||
| get root-package resolution, which is correct because BES always emits | ||
| absolute labels — so the default never actually engages on a relative | ||
| input in practice.""" | ||
| _eq(":foo with default pkg", canonical_label(":foo"), "//:foo") | ||
| _eq("foo with default pkg", canonical_label("foo"), "//:foo") | ||
|
|
||
| def _test_impl(ctx): | ||
| _test_absolute_unchanged_when_target_present() | ||
| _test_absolute_shorthand_expands_basename() | ||
| _test_external_repo_labels() | ||
| _test_relative_colon_name_at_root() | ||
| _test_relative_colon_name_in_nested_package() | ||
| _test_bare_name_at_root() | ||
| _test_bare_name_in_nested_package() | ||
| _test_relative_pkg_colon_name_at_root() | ||
| _test_relative_pkg_colon_name_in_nested_package() | ||
| _test_default_current_package_is_root() | ||
| print("runnable.axl: OK (10 sections)") | ||
| return 0 | ||
|
|
||
| runnable_tests = task( | ||
| name = "test-runnable", | ||
| group = ["dev"], | ||
| implementation = _test_impl, | ||
| args = {}, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| """A default 'run' task that builds a target with `bazel build` and runs the resulting binary.""" | ||
|
|
||
| load("@std//time.axl", "sleep_iter") | ||
| load("./bazel.axl", "BazelTrait") | ||
| load("./lib/bazel_results.axl", "BES_DRAIN_TICK_MS", "init_data", "process_event") | ||
| load("./lib/environment.axl", "error") | ||
| load("./lib/health_check.axl", "HealthCheckTrait") | ||
| load("./lib/lifecycle.axl", "Phase", "ProgressStyles", "TaskLifecycleTrait", "preflight_phase", "setup_phase", "task_update") | ||
| load("./lib/runnable.axl", "runnable") | ||
| load("./lib/tips.axl", "tips") | ||
|
|
||
| def _impl(ctx: TaskContext) -> int: | ||
| bazel_trait = ctx.traits[BazelTrait] | ||
| hc_trait = ctx.traits[HealthCheckTrait] | ||
| lifecycle = ctx.traits[TaskLifecycleTrait] | ||
|
|
||
| target = ctx.args.target[0] | ||
| forward_args = list(ctx.args.run_args) | ||
|
|
||
| data = init_data() | ||
| setup_phase(ctx, lifecycle) | ||
| for handler in lifecycle.task_started: | ||
| handler(ctx, target) | ||
|
|
||
| preflight_phase(ctx, lifecycle, hc_trait) | ||
|
|
||
| for hook in hc_trait.post_health_check: | ||
| result = hook(ctx) | ||
| if result != None: | ||
| fail(result) | ||
|
|
||
| flags = [] | ||
| flags.extend(bazel_trait.extra_flags) | ||
| for hook in bazel_trait.task_flags: | ||
| flags.extend(hook(ctx)) | ||
| if bazel_trait.flags: | ||
| flags = bazel_trait.flags(flags) | ||
|
|
||
| startup_flags = list(bazel_trait.extra_startup_flags) | ||
| if bazel_trait.startup_flags: | ||
| startup_flags = bazel_trait.startup_flags(startup_flags) | ||
|
|
||
| rc = ctx.bazel.parse_rc( | ||
| root = ctx.std.env.root_dir(), | ||
| flags = flags, | ||
| startup_flags = startup_flags, | ||
| ) | ||
|
|
||
| # `run` inherits the `build` section in Bazel's rc hierarchy, so this picks | ||
| # up `common`, `build`, and `run` flags. Forwarded as-is to ctx.bazel.build. | ||
| rc_startup_flags, flags = rc.expand_all(command = "run") | ||
| ctx.bazel.startup_flags.extend(startup_flags + rc_startup_flags + ["--ignore_all_rc_files"]) | ||
|
|
||
| # Required for the spawn to succeed: keep BES artifact references | ||
| # local, materialize the binary on disk, and lay down the runfiles | ||
| # tree. Appended LAST so they win over any --config=X expansion | ||
| # introduced by the rc. | ||
| flags.append("--experimental_build_event_upload_strategy=local") | ||
| flags.append("--remote_download_outputs=toplevel") | ||
| flags.append("--build_runfile_links") | ||
|
|
||
| r = runnable(ctx, [target]) | ||
| events = bazel.build_events.iterator() | ||
| build_events = [events] + list(bazel_trait.build_event_sinks) | ||
|
|
||
| for hook in bazel_trait.build_start: | ||
| hook(ctx) | ||
|
|
||
| task_update( | ||
| ctx, | ||
| lifecycle, | ||
| "running", | ||
| ProgressStyles( | ||
| body = "Building %s..." % target, | ||
| stream = "Building %s" % target, | ||
| ), | ||
| kind = "bazel_results", | ||
| data = data, | ||
| phase = Phase(name = "build", description = "Build run target", emoji = "🔨"), | ||
| ) | ||
|
|
||
| build = ctx.bazel.build(target, build_events = build_events, flags = flags) | ||
|
|
||
| for _tick in sleep_iter(BES_DRAIN_TICK_MS): | ||
| for _ in range(10000): | ||
| event = events.try_pop() | ||
| if event == None: | ||
| break | ||
| r.event(event) | ||
| for handler in bazel_trait.build_event: | ||
| handler(ctx, event) | ||
| process_event(data, event) | ||
| if events.done: | ||
| break | ||
|
|
||
| build_status = build.wait() | ||
| for sink in bazel_trait.build_event_sinks: | ||
| sink.wait() | ||
| for hook in bazel_trait.build_end: | ||
| hook(ctx, build_status.code) | ||
|
|
||
| if not build_status.success: | ||
| return build_status.code or 1 | ||
|
|
||
| entrypoint = r.determine_entrypoint(target) | ||
| if not entrypoint: | ||
| error(ctx.std, "Failed to determine the run entrypoint for %s." % target) | ||
| return 1 | ||
| if not r.is_runnable(entrypoint): | ||
| error(ctx.std, "%s is not runnable (no runfiles tree at %s.runfiles)." % (target, entrypoint)) | ||
| return 1 | ||
|
|
||
| task_update( | ||
| ctx, | ||
| lifecycle, | ||
| "running", | ||
| ProgressStyles( | ||
| body = "Running %s..." % target, | ||
| stream = "Running %s" % target, | ||
| ), | ||
| phase = Phase(name = "run", description = "Run target", emoji = "🚀"), | ||
| ) | ||
|
|
||
| return r.spawn(entrypoint, forward_args).wait().code | ||
|
|
||
| run = task( | ||
| summary = "Build a target with bazel and run the resulting binary.", | ||
| args = { | ||
| "target": args.positional( | ||
| minimum = 1, | ||
| maximum = 1, | ||
| description = "Bazel target to build and run.", | ||
| ), | ||
| "run_args": args.trailing_var_args( | ||
| description = "Arguments forwarded to the target binary. Separate from `aspect run`'s own flags with `--`, e.g. `aspect run //foo:bar -- --binary-arg value`.", | ||
| ), | ||
| }, | ||
| traits = [ | ||
| BazelTrait, | ||
| HealthCheckTrait, | ||
| TaskLifecycleTrait, | ||
| ] + tips.TRAITS, | ||
| implementation = _impl, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aspect runshells out withctx.bazel.build(...), but this code expands.bazelrcascommand = "run", which injects run-only flags into a build invocation. Bazel’s command-line reference lists flags like--script_pathunder Run Options (not build), so users withrunentries in.bazelrccan hitunrecognized optionfailures before execution. This should expand build-compatible flags (or filter run-only ones) before callingbuild.Useful? React with 👍 / 👎.