Skip to content

Commit

Permalink
Add a print_rerun feature
Browse files Browse the repository at this point in the history
Summary:
Useful for some projects that want to reuse the what-changed logic. Rough model would be:

* Run `supertd targets cell//... --output=data.json` (or with `--dry-run` to get the flags and do it yourself).
* Run `supertd btd --base=data.json --print-rerun cell//...` (with `--cells` and `--config` as per the README probably).
* Look at the output - will either be `+` for targets that were modified, `-` for ones that were deleted, or `*` if there is no benefit to doing a rerun.

Reviewed By: Acesine

Differential Revision: D56398248

fbshipit-source-id: bfbfe50443e69971fb1c7a6387c9d4d4cfa47acc
  • Loading branch information
ndmitchell authored and facebook-github-bot committed Apr 23, 2024
1 parent a5cd9e2 commit 7311c89
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions btd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ pub struct Args {
#[arg(long)]
graph_size: bool,

/// Print out the patterns to rerun. Patterns will be prefixed with either `+` (added) or `-` (removed).
#[arg(long)]
print_rerun: bool,

/// Reports all graph errors on the diff revision.
#[arg(long)]
write_errors_to_file: Option<PathBuf>,
Expand Down Expand Up @@ -199,6 +203,10 @@ pub fn main(args: Args) -> anyhow::Result<()> {
None => universe.to_vec(),
Some(x) => x.modified.map(|x| x.as_pattern()),
};
if args.print_rerun {
print_rerun(&rerun);
return Ok(());
}
let new = if ask_buck.is_empty() {
Targets::new(Vec::new())
} else {
Expand Down Expand Up @@ -301,6 +309,21 @@ struct Rerun {
deleted: HashSet<Package>,
}

/// Print out the patterns to rerun, in diff style.
fn print_rerun(rerun: &Option<Rerun>) {
match rerun {
None => println!("* everything"),
Some(rerun) => {
for x in &rerun.deleted {
println!("- {}", x);
}
for x in &rerun.modified {
println!("+ {}", x);
}
}
}
}

/// Tells us which things might have changed, and therefore what
/// we should run buck2 targets on at the diff revision to
/// properly check if it really did change.
Expand Down
30 changes: 30 additions & 0 deletions btd/test/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def test_run(patch_name):
out_btd1 = Path(output_dir).joinpath("btd1.json")
out_btd2 = Path(output_dir).joinpath("btd2.json")
out_targets = Path(output_dir).joinpath("targets.txt")
out_rerun = Path(output_dir).joinpath("rerun.txt")
btd_args = [
"--check-dangling",
"--cells",
Expand Down Expand Up @@ -165,6 +166,16 @@ def test_run(patch_name):
output=out_btd2,
expect_fail=btd_fail,
)
run(
btd,
*btd_args,
"--universe",
"root//...",
"--buck",
buck,
"--print-rerun",
output=out_rerun,
)
if not btd_fail:
# We want to make sure our rerun logic was correct
assert read_file(out_btd1) == read_file(out_btd2)
Expand All @@ -177,6 +188,8 @@ def test_run(patch_name):
run(buck, "build", "@" + str(out_targets))
# Check custom properties
check_properties(patch_name, output)
rerun = read_file(out_rerun)
check_properties_rerun(patch_name, rerun)


def check_properties(patch, rdeps):
Expand Down Expand Up @@ -219,6 +232,23 @@ def check_properties(patch, rdeps):
raise AssertionError("No properties known for: " + patch)


def check_properties_rerun(patch, rerun):
if patch == "nothing":
assert rerun == ""
elif patch == "file":
assert rerun == ""
elif patch == "rename_inner":
assert rerun == "+ root//inner\n"
elif patch == "buckconfig":
assert rerun == "* everything\n"
elif patch == "cfg_modifiers":
assert rerun == "+ root//inner\n"
elif patch == "delete_inner":
assert rerun == "- root//inner\n"
else:
raise AssertionError("No properties known for: " + patch)


def expect_btd_failure(patch):
if patch == "delete_inner":
return "Target `root//inner:baz` was deleted but is referenced by `root//:bar`"
Expand Down

0 comments on commit 7311c89

Please sign in to comment.