Skip to content

Commit bd6d0e1

Browse files
committed
rust-project: add support for alternative Buck2 commands
This is useful for users of wrappers such as Buckle, as well as developers hacking on Buck2 that need to use a custom build.
1 parent 2b3b02d commit bd6d0e1

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

integrations/rust-project/src/buck.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ pub(crate) fn to_json_project(
5151
aliases: FxHashMap<Target, AliasedTargetInfo>,
5252
relative_paths: bool,
5353
check_cycles: bool,
54+
buck2_command: Option<String>,
5455
) -> Result<JsonProject, anyhow::Error> {
5556
let mode = select_mode(None);
56-
let buck = Buck::new(mode);
57+
let buck = Buck::new(buck2_command, mode);
5758
let project_root = buck.resolve_project_root()?;
5859

5960
let ExpandedAndResolved {
@@ -437,12 +438,16 @@ fn merge_unit_test_targets(
437438

438439
#[derive(Debug, Default)]
439440
pub(crate) struct Buck {
441+
command: String,
440442
mode: Option<String>,
441443
}
442444

443445
impl Buck {
444-
pub(crate) fn new(mode: Option<String>) -> Self {
445-
Buck { mode }
446+
pub(crate) fn new(command: Option<String>, mode: Option<String>) -> Self {
447+
Buck {
448+
command: command.unwrap_or_else(|| "buck2".into()),
449+
mode,
450+
}
446451
}
447452

448453
/// Invoke `buck2` with the given subcommands.
@@ -472,7 +477,7 @@ impl Buck {
472477
I: IntoIterator<Item = S>,
473478
S: AsRef<OsStr>,
474479
{
475-
let mut cmd = Command::new("buck2");
480+
let mut cmd = Command::new(&self.command);
476481

477482
// rust-analyzer invokes the check-on-save command with `RUST_BACKTRACE=short`
478483
// set. Unfortunately, buck2 doesn't handle that well and becomes extremely

integrations/rust-project/src/cli/check.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ pub(crate) struct Check {
2323
}
2424

2525
impl Check {
26-
pub(crate) fn new(mode: Option<String>, use_clippy: bool, saved_file: PathBuf) -> Self {
26+
pub(crate) fn new(
27+
buck2_command: Option<String>,
28+
mode: Option<String>,
29+
use_clippy: bool,
30+
saved_file: PathBuf,
31+
) -> Self {
2732
let saved_file = safe_canonicalize(&saved_file);
2833

2934
let mode = select_mode(mode.as_deref());
30-
let buck = buck::Buck::new(mode);
35+
let buck = buck::Buck::new(buck2_command, mode);
3136
Self {
3237
buck,
3338
use_clippy,

integrations/rust-project/src/cli/develop.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) struct Develop {
3838
pub(crate) buck: buck::Buck,
3939
pub(crate) check_cycles: bool,
4040
pub(crate) invoked_by_ra: bool,
41+
pub(crate) buck2_command: Option<String>,
4142
}
4243

4344
pub(crate) struct OutputCfg {
@@ -64,6 +65,7 @@ impl Develop {
6465
relative_paths,
6566
mode,
6667
check_cycles,
68+
buck2_command,
6769
..
6870
} = command
6971
{
@@ -82,14 +84,15 @@ impl Develop {
8284
};
8385

8486
let mode = select_mode(mode.as_deref());
85-
let buck = buck::Buck::new(mode);
87+
let buck = buck::Buck::new(buck2_command.clone(), mode);
8688

8789
let develop = Develop {
8890
sysroot,
8991
relative_paths,
9092
buck,
9193
check_cycles,
9294
invoked_by_ra: false,
95+
buck2_command,
9396
};
9497
let out = OutputCfg { out, pretty };
9598

@@ -104,7 +107,10 @@ impl Develop {
104107
}
105108

106109
if let crate::Command::DevelopJson {
107-
sysroot_mode, args, ..
110+
sysroot_mode,
111+
args,
112+
buck2_command,
113+
..
108114
} = command
109115
{
110116
let out = Output::Stdout;
@@ -123,14 +129,15 @@ impl Develop {
123129
}
124130
};
125131

126-
let buck = buck::Buck::new(mode);
132+
let buck = buck::Buck::new(buck2_command.clone(), mode);
127133

128134
let develop = Develop {
129135
sysroot,
130136
relative_paths: false,
131137
buck,
132138
check_cycles: false,
133139
invoked_by_ra: true,
140+
buck2_command,
134141
};
135142
let out = OutputCfg { out, pretty: false };
136143

@@ -230,6 +237,7 @@ impl Develop {
230237
relative_paths,
231238
buck,
232239
check_cycles,
240+
buck2_command,
233241
..
234242
} = self;
235243

@@ -269,6 +277,7 @@ impl Develop {
269277
aliased_libraries,
270278
*relative_paths,
271279
*check_cycles,
280+
buck2_command.clone(),
272281
)?;
273282

274283
Ok(rust_project)

integrations/rust-project/src/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ enum Command {
111111
/// Optional argument specifying build mode.
112112
#[clap(short = 'm', long)]
113113
mode: Option<String>,
114+
115+
/// Command used to run Buck2. Defaults to `buck2`.
116+
#[clap(long)]
117+
buck2_command: Option<String>,
114118
},
115119
/// `DevelopJson` is a more limited, stripped down [`Command::Develop`].
116120
///
@@ -131,6 +135,10 @@ enum Command {
131135
client: Option<String>,
132136

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

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

@@ -287,12 +298,13 @@ fn main() -> Result<(), anyhow::Error> {
287298
mode,
288299
use_clippy,
289300
saved_file,
301+
buck2_command,
290302
..
291303
} => {
292304
let subscriber = tracing_subscriber::registry().with(fmt.with_filter(filter));
293305
tracing::subscriber::set_global_default(subscriber)?;
294306

295-
cli::Check::new(mode, use_clippy, saved_file.clone())
307+
cli::Check::new(buck2_command, mode, use_clippy, saved_file.clone())
296308
.run()
297309
.inspect_err(|e| crate::scuba::log_check_error(&e, &saved_file, use_clippy))
298310
}
@@ -376,6 +388,7 @@ fn json_args_pass() {
376388
args,
377389
sysroot_mode: SysrootMode::Rustc,
378390
client: None,
391+
buck2_command: None,
379392
}),
380393
version: false,
381394
};
@@ -393,6 +406,7 @@ fn json_args_pass() {
393406
args,
394407
sysroot_mode: SysrootMode::Rustc,
395408
client: None,
409+
buck2_command: None,
396410
}),
397411
version: false,
398412
};
@@ -410,6 +424,7 @@ fn json_args_pass() {
410424
args,
411425
sysroot_mode: SysrootMode::Rustc,
412426
client: None,
427+
buck2_command: None,
413428
}),
414429
version: false,
415430
};

0 commit comments

Comments
 (0)