Skip to content
Open
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
4 changes: 4 additions & 0 deletions crates/starpls/src/commands/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub(crate) struct ServerCommand {
#[clap(long = "analysis_debounce_interval", default_value_t = 250)]
pub(crate) analysis_debounce_interval: u64,

/// Whether to use buildozer to query for targets.
#[clap(long = "use_buildozer", default_value_t = false)]
pub(crate) use_buildozer: bool,

#[command(flatten)]
pub(crate) inference_options: InferenceOptions,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/starpls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl Server {

// Determine Bazel configuration.
let mut has_bazel_init_err = false;
let bazel_client = Arc::new(BazelCLI::new(&bazel_path));
let bazel_client =
Arc::new(BazelCLI::new(&bazel_path).with_buildozer(config.args.use_buildozer));
let bazel_cx = match BazelContext::new(&*bazel_client) {
Ok(cx) => cx,
Err(err) => {
Expand Down
50 changes: 43 additions & 7 deletions crates/starpls_bazel/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ pub trait BazelClient: Send + Sync + 'static {
pub struct BazelCLI {
executable: PathBuf,
repo_mappings: RwLock<HashMap<String, HashMap<String, String>>>,
use_buildozer: bool,
}

impl BazelCLI {
pub fn new(executable: impl AsRef<Path>) -> Self {
Self {
executable: executable.as_ref().to_path_buf(),
..Default::default()
repo_mappings: Default::default(),
use_buildozer: false,
}
}

pub fn with_buildozer(mut self, use_buildozer: bool) -> Self {
self.use_buildozer = use_buildozer;
self
}

fn run_command<I, S>(&self, args: I) -> anyhow::Result<Vec<u8>>
where
I: IntoIterator<Item = S>,
Expand All @@ -64,6 +71,22 @@ impl BazelCLI {
}
Ok(output.stdout)
}

fn run_buildozer_command<I, S>(&self, args: I) -> anyhow::Result<Vec<u8>>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
let output = Command::new("buildozer").args(args).output()?;
if !output.status.success() {
bail!(
"failed to run buildozer command with exit status {}, stderr={:?}",
output.status,
str::from_utf8(&output.stderr)?,
);
}
Ok(output.stdout)
}
}

impl BazelClient for BazelCLI {
Expand Down Expand Up @@ -174,12 +197,24 @@ impl BazelClient for BazelCLI {
}

fn query_all_workspace_targets(&self) -> anyhow::Result<Vec<String>> {
let output = self.run_command(["query", "kind('.* rule', ...)"])?;
let targets = str::from_utf8(&output)?
.lines()
.map(|line| line.to_string())
.collect();
Ok(targets)
if self.use_buildozer {
// Use buildozer to query targets
let output = self.run_buildozer_command(["print label", "//...:*"])?;
let targets = str::from_utf8(&output)?
.lines()
.filter(|line| !line.is_empty())
.map(|line| line.to_string())
.collect();
Ok(targets)
} else {
// Use bazel query
let output = self.run_command(["query", "kind('.* rule', ...)"])?;
let targets = str::from_utf8(&output)?
.lines()
.map(|line| line.to_string())
.collect();
Ok(targets)
}
}

fn fetch_repo(&self, repo: &str) -> anyhow::Result<()> {
Expand All @@ -202,6 +237,7 @@ impl Default for BazelCLI {
Self {
executable: "bazel".into(),
repo_mappings: Default::default(),
use_buildozer: false,
}
}
}
Loading