Skip to content

Commit

Permalink
feat: Preserve output base between restarts
Browse files Browse the repository at this point in the history
This now uses a hash of the workspace directory as the output base when using a separate output base for queries, instead of a temp directory. This avoid recreating this expensive-to-create directory when restarting the language server.

Fixes #13.
  • Loading branch information
cameron-martin committed Feb 1, 2024
1 parent 60eb6a8 commit fc701fb
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 14 deletions.
5 changes: 3 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")
rust_binary(
name = "bazel-lsp",
srcs = glob(["src/**/*.rs"]),
compile_data = ["//src/builtin:builtin.pb"],
deps = [
"//src/builtin:builtin_proto_rust",
"@crates//:anyhow",
"@crates//:clap",
"@crates//:either",
"@crates//:hex",
"@crates//:lsp-types",
"@crates//:ring",
"@crates//:starlark",
"@crates//:starlark_lsp",
"@crates//:tempfile",
"@crates//:thiserror",
],
compile_data = ["//src/builtin:builtin.pb"],
)

rust_test(
Expand Down
35 changes: 34 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ edition = "2021"
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["derive"] }
either = "1.9.0"
hex = "0.4.3"
itertools = "0.12.0"
lsp-types = "0.94.1"
prost = "0.12.3"
prost-types = "0.12.3"
protoc-gen-prost = "0.2.3"
protoc-gen-tonic = "0.3.0"
ring = "0.17.7"
starlark = { git = "https://github.com/facebookexperimental/starlark-rust.git", branch = "main" }
starlark_lsp = { git = "https://github.com/facebookexperimental/starlark-rust.git", branch = "main" }
tempfile = "3.9.0"
thiserror = "1.0.56"
tonic = "0.10.2"
86 changes: 83 additions & 3 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl BazelClient for BazelCli {
fn query(&self, workspace: &BazelWorkspace, query: &str) -> anyhow::Result<String> {
let mut command = &mut Command::new(&self.bazel);
if let Some(output_base) = &workspace.query_output_base {
eprintln!("Running bazel query in {}", output_base.path().display());
command = command.arg("--output_base").arg(output_base.path());
command = command.arg("--output_base").arg(output_base);
}
command = command.arg("query").arg(query);
command = command.current_dir(&workspace.root);
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct Args {
/// The directory to put the query output bases in.
///
/// This is ignored if `--no-distinct-output-base` is enabled.
/// By default this is the OS's temp directory.
/// By default this is the directory `bazel-lsp` in the OS's
/// temp directory.
#[arg(long)]
query_output_base: Option<PathBuf>,
}
Expand All @@ -41,7 +42,7 @@ fn main() -> anyhow::Result<()> {
let query_output_base = if args.no_distinct_output_base {
None
} else {
Some(args.query_output_base.unwrap_or_else(|| env::temp_dir()))
Some(args.query_output_base.unwrap_or_else(|| env::temp_dir().join("bazel-lsp")))
};

let ctx = BazelContext::new(
Expand Down
10 changes: 7 additions & 3 deletions src/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::{
borrow::Cow,
io,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
};

use tempfile::TempDir;
use ring::digest;

use crate::client::BazelInfo;

pub struct BazelWorkspace {
pub root: PathBuf,
/// The output base to use for querying. This allows queries to not
/// be blocked by concurrent builds.
pub query_output_base: Option<TempDir>,
pub query_output_base: Option<PathBuf>,
pub workspace_name: Option<String>,
pub external_output_base: PathBuf,
}
Expand All @@ -34,7 +35,10 @@ impl BazelWorkspace {
}),
external_output_base: PathBuf::from(info.output_base).join("external"),
query_output_base: if let Some(output_base) = query_output_base {
Some(TempDir::with_prefix_in("bazel-lsp-", output_base)?)
let hash =
digest::digest(&digest::SHA256, output_base.as_ref().as_os_str().as_bytes());
let hash_hex = hex::encode(&hash);
Some(output_base.as_ref().join(hash_hex))
} else {
None
},
Expand Down

0 comments on commit fc701fb

Please sign in to comment.