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 Sep 12, 2024
1 parent d249428 commit 7a2b350
Show file tree
Hide file tree
Showing 4 changed files with 43 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 @@ -50,9 +50,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 @@ -436,12 +437,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 @@ -471,7 +476,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
17 changes: 14 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
{
let out = if stdout {
Expand All @@ -81,14 +83,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 @@ -102,7 +105,12 @@ impl Develop {
return (develop, input, out);
}

if let crate::Command::DevelopJson { sysroot_mode, args } = command {
if let crate::Command::DevelopJson {
sysroot_mode,
args,
buck2_command,
} = command
{
let out = Output::Stdout;
let mode = select_mode(None);

Expand All @@ -119,14 +127,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 @@ -226,6 +235,7 @@ impl Develop {
relative_paths,
buck,
check_cycles,
buck2_command,
..
} = self;

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

Ok(rust_project)
Expand Down
14 changes: 13 additions & 1 deletion integrations/rust-project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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 @@ -123,6 +127,10 @@ enum Command {
sysroot_mode: SysrootMode,

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 @@ -133,6 +141,9 @@ enum Command {
use_clippy: bool,
/// 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 @@ -266,11 +277,12 @@ 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).run()
cli::Check::new(buck2_command, mode, use_clippy, saved_file).run()
}
}
}
Expand Down

0 comments on commit 7a2b350

Please sign in to comment.