Skip to content

Commit

Permalink
rust-project: add support for alternative Buck2 commands
Browse files Browse the repository at this point in the history
This is useful for users of wrappers such as Buckle, as well as developers hacking on Buck2 that
need to use a custom build.
  • Loading branch information
cbarrete committed Nov 2, 2024
1 parent a4df9cc commit 7206804
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
13 changes: 9 additions & 4 deletions integrations/rust-project/src/buck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ pub(crate) fn to_json_project(
aliases: FxHashMap<Target, AliasedTargetInfo>,
relative_paths: bool,
check_cycles: bool,
buck2_command: Option<String>,
) -> Result<JsonProject, anyhow::Error> {
let mode = select_mode(None);
let buck = Buck::new(mode);
let buck = Buck::new(buck2_command, mode);
let project_root = buck.resolve_project_root()?;

let ExpandedAndResolved {
Expand Down Expand Up @@ -445,12 +446,16 @@ fn merge_unit_test_targets(

#[derive(Debug, Default)]
pub(crate) struct Buck {
command: String,
mode: Option<String>,
}

impl Buck {
pub(crate) fn new(mode: Option<String>) -> Self {
Buck { mode }
pub(crate) fn new(command: Option<String>, mode: Option<String>) -> Self {
Buck {
command: command.unwrap_or_else(|| "buck2".into()),
mode,
}
}

/// Invoke `buck2` with the given subcommands.
Expand Down Expand Up @@ -480,7 +485,7 @@ impl Buck {
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let mut cmd = Command::new("buck2");
let mut cmd = Command::new(&self.command);

// rust-analyzer invokes the check-on-save command with `RUST_BACKTRACE=short`
// set. Unfortunately, buck2 doesn't handle that well and becomes extremely
Expand Down
9 changes: 7 additions & 2 deletions integrations/rust-project/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ pub(crate) struct Check {
}

impl Check {
pub(crate) fn new(mode: Option<String>, use_clippy: bool, saved_file: PathBuf) -> Self {
pub(crate) fn new(
buck2_command: Option<String>,
mode: Option<String>,
use_clippy: bool,
saved_file: PathBuf,
) -> Self {
let saved_file = canonicalize(&saved_file).unwrap_or(saved_file);

let mode = select_mode(mode.as_deref());
let buck = buck::Buck::new(mode);
let buck = buck::Buck::new(buck2_command, mode);
Self {
buck,
use_clippy,
Expand Down
15 changes: 12 additions & 3 deletions integrations/rust-project/src/cli/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) struct Develop {
pub(crate) buck: buck::Buck,
pub(crate) check_cycles: bool,
pub(crate) invoked_by_ra: bool,
pub(crate) buck2_command: Option<String>,
}

pub(crate) struct OutputCfg {
Expand All @@ -64,6 +65,7 @@ impl Develop {
relative_paths,
mode,
check_cycles,
buck2_command,
..
} = command
{
Expand All @@ -82,14 +84,15 @@ impl Develop {
};

let mode = select_mode(mode.as_deref());
let buck = buck::Buck::new(mode);
let buck = buck::Buck::new(buck2_command.clone(), mode);

let develop = Develop {
sysroot,
relative_paths,
buck,
check_cycles,
invoked_by_ra: false,
buck2_command,
};
let out = OutputCfg { out, pretty };

Expand All @@ -104,7 +107,10 @@ impl Develop {
}

if let crate::Command::DevelopJson {
sysroot_mode, args, ..
sysroot_mode,
args,
buck2_command,
..
} = command
{
let out = Output::Stdout;
Expand All @@ -123,14 +129,15 @@ impl Develop {
}
};

let buck = buck::Buck::new(mode);
let buck = buck::Buck::new(buck2_command.clone(), mode);

let develop = Develop {
sysroot,
relative_paths: false,
buck,
check_cycles: false,
invoked_by_ra: true,
buck2_command,
};
let out = OutputCfg { out, pretty: false };

Expand Down Expand Up @@ -233,6 +240,7 @@ impl Develop {
relative_paths,
buck,
check_cycles,
buck2_command,
..
} = self;

Expand Down Expand Up @@ -272,6 +280,7 @@ impl Develop {
aliased_libraries,
*relative_paths,
*check_cycles,
buck2_command.clone(),
)?;

Ok(rust_project)
Expand Down
17 changes: 16 additions & 1 deletion integrations/rust-project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ enum Command {
/// Optional argument specifying build mode.
#[clap(short = 'm', long)]
mode: Option<String>,

/// Command used to run Buck2. Defaults to `buck2`.
#[clap(long)]
buck2_command: Option<String>,
},
/// `DevelopJson` is a more limited, stripped down [`Command::Develop`].
///
Expand All @@ -131,6 +135,10 @@ enum Command {
client: Option<String>,

args: JsonArguments,

/// Command used to run Buck2. Defaults to `buck2`.
#[clap(long)]
buck2_command: Option<String>,
},
/// Build the saved file's owning target. This is meant to be used by IDEs to provide diagnostics on save.
Check {
Expand All @@ -147,6 +155,9 @@ enum Command {

/// The file saved by the user. `rust-project` will infer the owning target(s) of the saved file and build them.
saved_file: PathBuf,
/// Command used to run Buck2. Defaults to `buck2`.
#[clap(long)]
buck2_command: Option<String>,
},
}

Expand Down Expand Up @@ -282,12 +293,13 @@ fn main() -> Result<(), anyhow::Error> {
mode,
use_clippy,
saved_file,
buck2_command,
..
} => {
let subscriber = tracing_subscriber::registry().with(fmt.with_filter(filter));
tracing::subscriber::set_global_default(subscriber)?;

cli::Check::new(mode, use_clippy, saved_file.clone())
cli::Check::new(buck2_command, mode, use_clippy, saved_file.clone())
.run()
.inspect_err(|e| crate::scuba::log_check_error(&e, &saved_file, use_clippy))
}
Expand Down Expand Up @@ -371,6 +383,7 @@ fn json_args_pass() {
args,
sysroot_mode: SysrootMode::Rustc,
client: None,
buck2_command: None,
}),
version: false,
};
Expand All @@ -388,6 +401,7 @@ fn json_args_pass() {
args,
sysroot_mode: SysrootMode::Rustc,
client: None,
buck2_command: None,
}),
version: false,
};
Expand All @@ -405,6 +419,7 @@ fn json_args_pass() {
args,
sysroot_mode: SysrootMode::Rustc,
client: None,
buck2_command: None,
}),
version: false,
};
Expand Down

0 comments on commit 7206804

Please sign in to comment.