Skip to content
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
27 changes: 27 additions & 0 deletions .aspect/axl.axl
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,32 @@ def test_large_bes(ctx: TaskContext, tc: int, temp_dir: str) -> int:

return tc

def test_bazel_info(ctx: TaskContext, tc: int) -> int:
info = ctx.bazel.info()

# Should return a dict
tc = test_case(tc, type(info) == "dict", "bazel.info() should return a dict")
tc = test_case(tc, len(info) > 0, "bazel.info() dict should be non-empty")

# Common keys should be present
for key in ["output_base", "execution_root", "workspace", "bazel-bin", "bazel-genfiles", "bazel-testlogs"]:
tc = test_case(tc, key in info, "bazel.info() should contain key: " + key)
tc = test_case(tc, type(info[key]) == "string", "bazel.info()[" + key + "] should be a string")
tc = test_case(tc, info[key] != "", "bazel.info()[" + key + "] should be non-empty")

# output_base should be an absolute path
tc = test_case(tc, info["output_base"].startswith("/"), "output_base should be an absolute path")

# workspace should match root_dir
root_dir = ctx.std.env.root_dir()
tc = test_case(tc, info["workspace"] == root_dir, "bazel.info()['workspace'] should match ctx.std.env.root_dir()")

# workdir override: pointing at the same workspace should give the same output_base
info2 = ctx.bazel.info(workdir = root_dir)
tc = test_case(tc, info2["output_base"] == info["output_base"], "bazel.info(workdir=root_dir) should return same output_base")

return tc

def test_http(ctx: TaskContext, tc: int, temp_dir: str) -> int:
# Test HTTP download with integrity/sha256 verification
# Using a well-known static file: LICENSE from this repo's GitHub
Expand Down Expand Up @@ -437,6 +463,7 @@ def impl(ctx: TaskContext) -> int:
tc = test_build_events(ctx, tc, temp_dir)
tc = test_large_bes(ctx, tc, temp_dir)
tc = test_http(ctx, tc, temp_dir)
tc = test_bazel_info(ctx, tc)

print(tc, "tests passed")
return 0
Expand Down
55 changes: 55 additions & 0 deletions crates/axl-runtime/src/engine/bazel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::HashMap;
use std::process::Stdio;

use allocative::Allocative;
use derive_more::Display;
use either::Either;
use starlark::collections::SmallMap;
use starlark::environment::Methods;
use starlark::environment::MethodsBuilder;
use starlark::environment::MethodsStatic;
Expand Down Expand Up @@ -210,6 +212,59 @@ pub(crate) fn bazel_methods(registry: &mut MethodsBuilder) {
fn query<'v>(#[allow(unused)] this: values::Value<'v>) -> starlark::Result<query::Query> {
Ok(query::Query::new())
}

/// Run `bazel info` and return all key/value pairs as a dict.
///
/// Blocks until the command completes. Raises an error if Bazel exits
/// with a non-zero code.
///
/// # Arguments
/// * `workdir`: workspace root to run `bazel info` in (default: inferred from ctx)
///
/// **Examples**
///
/// ```python
/// def _show_info_impl(ctx):
/// info = ctx.bazel.info()
/// print(info["output_base"])
/// print(info["execution_root"])
/// ```
fn info<'v>(
#[allow(unused)] this: values::Value<'v>,
#[starlark(require = named, default = NoneOr::None)] workdir: NoneOr<String>,
eval: &mut Evaluator<'v, '_, '_>,
) -> anyhow::Result<SmallMap<String, String>> {
let store = AxlStore::from_eval(eval)?;
let workdir = workdir
.into_option()
.map(std::path::PathBuf::from)
.unwrap_or_else(|| store.root_dir.clone());

let output = std::process::Command::new("bazel")
.arg("info")
.current_dir(&workdir)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.stdin(Stdio::null())
.output()
.map_err(|e| anyhow::anyhow!("failed to spawn bazel: {}", e))?;

if !output.status.success() {
anyhow::bail!(
"bazel info failed with exit code {:?}",
output.status.code()
);
}

let stdout = String::from_utf8_lossy(&output.stdout);
let mut map = SmallMap::new();
for line in stdout.lines() {
if let Some((key, value)) = line.split_once(": ") {
map.insert(key.trim().to_string(), value.trim().to_string());
}
}
Ok(map)
}
}

#[starlark_module]
Expand Down