Skip to content

Commit

Permalink
fix: infer current workspace name from execution_root (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
withered-magic committed May 9, 2024
1 parent 8b11ada commit e2e87ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/starpls/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(crate) fn run_check(paths: Vec<String>, output_base: Option<String>) -> anyh
bazel_client,
interner.clone(),
info.workspace.clone(),
info.workspace_name,
external_output_base.clone(),
fetch_repo_sender,
bzlmod_enabled,
Expand Down
19 changes: 18 additions & 1 deletion crates/starpls/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub(crate) struct DefaultFileLoader {
bazel_client: Arc<dyn BazelClient>,
interner: Arc<PathInterner>,
workspace: PathBuf,
workspace_name: Option<String>,
external_output_base: PathBuf,
cached_load_results: DashMap<String, PathBuf>,
fetch_repo_sender: Sender<Task>,
Expand All @@ -200,6 +201,7 @@ impl DefaultFileLoader {
bazel_client: Arc<dyn BazelClient>,
interner: Arc<PathInterner>,
workspace: PathBuf,
workspace_name: Option<String>,
external_output_base: PathBuf,
fetch_repo_sender: Sender<Task>,
bzlmod_enabled: bool,
Expand All @@ -208,6 +210,7 @@ impl DefaultFileLoader {
bazel_client,
interner,
workspace,
workspace_name,
external_output_base,
cached_load_results: Default::default(),
fetch_repo_sender,
Expand Down Expand Up @@ -280,7 +283,12 @@ impl DefaultFileLoader {
if !label.repo().is_empty() {
canonical_repo_res = Some(label.repo().to_string());
}
(self.external_output_base.join(label.repo()), PathBuf::new())

if self.workspace_name.as_ref().map(|name| name.as_str()) == Some(label.repo()) {
(self.workspace.clone(), PathBuf::new())
} else {
(self.external_output_base.join(label.repo()), PathBuf::new())
}
}
RepoKind::Current => {
// Find the Bazel workspace root.
Expand Down Expand Up @@ -553,6 +561,11 @@ impl FileLoader for DefaultFileLoader {
replace_trailing_slash: false,
})
})
.chain(self.workspace_name.as_ref().map(|name| LoadItemCandidate {
kind: LoadItemCandidateKind::Directory,
path: name.clone(),
replace_trailing_slash: false,
}))
.collect(),
),
_ => None,
Expand All @@ -571,6 +584,10 @@ impl FileLoader for DefaultFileLoader {
} else {
self.external_output_base.join(canonical_repo)
}
} else if self.workspace_name.as_ref().map(|name| name.as_str())
== Some(label.repo())
{
self.workspace.clone()
} else {
self.external_output_base.join(label.repo())
};
Expand Down
2 changes: 2 additions & 0 deletions crates/starpls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Server {
};

eprintln!("server: workspace root: {:?}", info.workspace);
eprintln!("server: workspace name: {:?}", info.workspace_name);

// Determine the output base for the purpose of resolving external repositories.
let external_output_base = info.output_base.join("external");
Expand Down Expand Up @@ -164,6 +165,7 @@ impl Server {
bazel_client.clone(),
path_interner.clone(),
info.workspace.clone(),
info.workspace_name,
external_output_base.clone(),
task_pool_sender.clone(),
bzlmod_enabled,
Expand Down
15 changes: 15 additions & 0 deletions crates/starpls_bazel/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ use anyhow::{anyhow, bail};
use parking_lot::RwLock;
use serde_json::Deserializer;

const DEFAULT_WORKSPACE_NAMES: &[&str] = &["__main__", "_main"];

#[derive(Default)]
pub struct BazelInfo {
pub output_base: PathBuf,
pub release: String,
pub starlark_semantics: String,
pub workspace: PathBuf,
pub workspace_name: Option<String>,
}

pub trait BazelClient: Send + Sync + 'static {
Expand Down Expand Up @@ -73,6 +76,7 @@ impl BazelClient for BazelCLI {
fn info(&self) -> anyhow::Result<BazelInfo> {
let output = self.run_command(&[
"info",
"execution_root",
"output_base",
"release",
"starlark-semantics",
Expand All @@ -84,12 +88,22 @@ impl BazelClient for BazelCLI {
let mut release = None;
let mut starlark_semantics = None;
let mut workspace = None;
let mut workspace_name = None;
for line in output.lines() {
let (key, value) = match line.split_once(": ") {
Some(pair) => pair,
None => continue,
};
match key {
"execution_root" => {
// Taken from https://github.com/cameron-martin/bazel-lsp/blob/92644f21aca7cfbba332c67ac1aa9cf43765e021/src/workspace.rs#L24.
workspace_name = PathBuf::from(value).file_name().and_then(|file_name| {
match file_name.to_string_lossy().to_string() {
name if DEFAULT_WORKSPACE_NAMES.contains(&name.as_str()) => None,
name => Some(name),
}
});
}
"output_base" => output_base = Some(value),
"release" => release = Some(value),
"starlark-semantics" => starlark_semantics = Some(value),
Expand All @@ -111,6 +125,7 @@ impl BazelClient for BazelCLI {
workspace: workspace
.ok_or_else(|| anyhow!("failed to determine workspace from `bazel info`"))?
.into(),
workspace_name,
})
}

Expand Down

0 comments on commit e2e87ee

Please sign in to comment.