Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bazel workspace lsp support #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions bazel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "bazel"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
84 changes: 84 additions & 0 deletions bazel/src/bazel_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::fmt::Debug;
use std::path::PathBuf;
use std::process::Command;

#[derive(Debug, Clone, PartialEq)]
pub struct BazelInfo {
pub workspace_root: PathBuf,
pub output_base: PathBuf,
pub execroot: PathBuf,
}

fn parse_bazel_info(info: &str) -> Option<BazelInfo> {
let mut workspace_root: Option<PathBuf> = None;
let mut output_base: Option<PathBuf> = None;
let mut execroot: Option<PathBuf> = None;
info.split('\n').for_each(|l| {
let split: Vec<&str> = l.splitn(2, ':').collect();
if split.len() != 2 {
return;
}
let first = split.get(0);
let next = split.get(1);
match (first, next) {
(Some(key), Some(value)) => match key {
&"execution_root" => execroot = Some(value.trim().into()),
&"output_base" => output_base = Some(value.trim().into()),
&"workspace" => workspace_root = Some(value.trim().into()),
_ => {}
},
_ => {}
}
});
match (execroot, output_base, workspace_root) {
(Some(execroot), Some(output_base), Some(workspace_root)) => Some(BazelInfo {
workspace_root,
execroot,
output_base,
}),
_ => {
eprintln!(
"Couldn't find workspace_root, execroot or output_base in output:\n`{}`",
info
);
None
}
}
}

pub fn get_bazel_info(workspace_dir: Option<&str>) -> Option<BazelInfo> {
let mut raw_command = Command::new("bazel");
let mut command = raw_command.arg("info");
command = match workspace_dir {
Some(d) => command.current_dir(d),
None => command,
};

let output = command.output().ok()?;

if !output.status.success() {
return None;
}

let s = std::str::from_utf8(output.stdout.as_slice()).ok()?;

parse_bazel_info(s)
}

#[cfg(test)]
mod tests {
use super::parse_bazel_info;
use super::BazelInfo;

#[test]
fn parses_info() {
assert_eq!(
parse_bazel_info(include_str!("info.txt")),
Some(BazelInfo {
workspace_root: "/home/user/dev/bazel/bazel".into(),
execroot: "/home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel".into(),
output_base: "/home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf".into()
})
)
}
}
23 changes: 23 additions & 0 deletions bazel/src/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
bazel-bin: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel/bazel-out/k8-fastbuild/bin
bazel-genfiles: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel/bazel-out/k8-fastbuild/bin
bazel-testlogs: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel/bazel-out/k8-fastbuild/testlogs
character-encoding: file.encoding = ISO-8859-1, defaultCharset = ISO-8859-1
command_log: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/command.log
committed-heap-size: 418MB
execution_root: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel
gc-count: 11
gc-time: 42ms
install_base: /home/user/.cache/bazel/_bazel_user/install/41b71f1bb3ce13f20cfeeb31a9357113
java-home: /home/user/.cache/bazel/_bazel_user/install/41b71f1bb3ce13f20cfeeb31a9357113/embedded_tools/jdk
java-runtime: OpenJDK Runtime Environment (build 11.0.6+10-LTS) by Azul Systems, Inc.
java-vm: OpenJDK 64-Bit Server VM (build 11.0.6+10-LTS, mixed mode) by Azul Systems, Inc.
max-heap-size: 4175MB
output_base: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf
output_path: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/execroot/io_bazel/bazel-out
package_path: %workspace%
release: release 5.2.0
repository_cache: /home/user/.cache/bazel/_bazel_user/cache/repos/v1
server_log: /home/user/.cache/bazel/_bazel_user/726bdc44ca84ffc53f631c27e313c4cf/java.log.home.user.log.java.20220821-211440.206313
server_pid: 206313
used-heap-size: 266MB
workspace: /home/user/dev/bazel/bazel
Loading