Skip to content

Commit 6d73d2d

Browse files
committed
feat: compile-time fallback include path
1 parent 806090e commit 6d73d2d

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/config/workspace.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ pub struct WorkspaceProtoConfigs {
2020
protoc_include_prefix: Vec<PathBuf>,
2121
cli_include_paths: Vec<PathBuf>,
2222
init_include_paths: Vec<PathBuf>,
23+
fallback_include_path: Option<PathBuf>,
2324
}
2425

2526
impl WorkspaceProtoConfigs {
26-
pub fn new(cli_include_paths: Vec<PathBuf>) -> Self {
27+
pub fn new(cli_include_paths: Vec<PathBuf>, fallback_include_path: Option<PathBuf>) -> Self {
2728
// Try to find protobuf library and get its include paths
2829
// Do not emit metadata on stdout as LSP programs can consider
2930
// it part of spec
@@ -39,6 +40,7 @@ impl WorkspaceProtoConfigs {
3940
workspaces: HashSet::new(),
4041
formatters: HashMap::new(),
4142
configs: HashMap::new(),
43+
fallback_include_path,
4244
protoc_include_prefix,
4345
cli_include_paths,
4446
init_include_paths: Vec::new(),
@@ -128,6 +130,7 @@ impl WorkspaceProtoConfigs {
128130

129131
ipath.push(w.to_path_buf());
130132
ipath.extend_from_slice(&self.protoc_include_prefix);
133+
ipath.extend_from_slice(self.fallback_include_path.as_slice());
131134
Some(ipath)
132135
}
133136

@@ -180,7 +183,7 @@ mod test {
180183
let f = tmpdir.path().join("protols.toml");
181184
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
182185

183-
let mut ws = WorkspaceProtoConfigs::new(vec![]);
186+
let mut ws = WorkspaceProtoConfigs::new(vec![], None);
184187
ws.add_workspace(&WorkspaceFolder {
185188
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
186189
name: "Test".to_string(),
@@ -214,7 +217,7 @@ mod test {
214217
let f = tmpdir.path().join("protols.toml");
215218
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
216219

217-
let mut ws = WorkspaceProtoConfigs::new(vec![]);
220+
let mut ws = WorkspaceProtoConfigs::new(vec![], None);
218221
ws.add_workspace(&WorkspaceFolder {
219222
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
220223
name: "Test".to_string(),
@@ -249,7 +252,7 @@ mod test {
249252
let f = tmpdir.path().join(file);
250253
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
251254

252-
let mut ws = WorkspaceProtoConfigs::new(vec![]);
255+
let mut ws = WorkspaceProtoConfigs::new(vec![], None);
253256
ws.add_workspace(&WorkspaceFolder {
254257
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
255258
name: "Test".to_string(),
@@ -272,7 +275,7 @@ mod test {
272275
PathBuf::from("/path/to/protos"),
273276
PathBuf::from("relative/path"),
274277
];
275-
let mut ws = WorkspaceProtoConfigs::new(cli_paths);
278+
let mut ws = WorkspaceProtoConfigs::new(cli_paths, None);
276279
ws.add_workspace(&WorkspaceFolder {
277280
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
278281
name: "Test".to_string(),
@@ -309,7 +312,7 @@ mod test {
309312
PathBuf::from("relative/init/path"),
310313
];
311314

312-
let mut ws = WorkspaceProtoConfigs::new(cli_paths);
315+
let mut ws = WorkspaceProtoConfigs::new(cli_paths, None);
313316
ws.set_init_include_paths(init_paths);
314317
ws.add_workspace(&WorkspaceFolder {
315318
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
@@ -329,4 +332,37 @@ mod test {
329332
// CLI paths should still be included
330333
assert!(include_paths.contains(&PathBuf::from("/cli/path")));
331334
}
335+
336+
#[test]
337+
fn test_fallback_include_path() {
338+
let tmpdir = tempdir().expect("failed to create temp directory");
339+
let f = tmpdir.path().join("protols.toml");
340+
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
341+
342+
// Set both CLI and initialization include paths
343+
let cli_paths = vec![PathBuf::from("/cli/path")];
344+
let init_paths = vec![
345+
PathBuf::from("/init/path1"),
346+
PathBuf::from("relative/init/path"),
347+
];
348+
349+
let mut ws = WorkspaceProtoConfigs::new(cli_paths, Some("fallback_path".into()));
350+
ws.set_init_include_paths(init_paths);
351+
ws.add_workspace(&WorkspaceFolder {
352+
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
353+
name: "Test".to_string(),
354+
});
355+
356+
let inworkspace = Url::from_file_path(tmpdir.path().join("foobar.proto")).unwrap();
357+
let include_paths = ws.get_include_paths(&inworkspace).unwrap();
358+
359+
// Fallback path should be included and on the last position
360+
assert_eq!(
361+
include_paths
362+
.iter()
363+
.rev()
364+
.position(|p| p == "fallback_path"),
365+
Some(0)
366+
);
367+
}
332368
}

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ async fn main() {
4848
.with_writer(file_appender.0)
4949
.init();
5050

51+
let fallback_include_path = option_env!("PROTOC_FALLBACK_INCLUDE_PATH").map(Into::into);
52+
5153
tracing::info!("server version: {}", env!("CARGO_PKG_VERSION"));
5254
let (server, _) = async_lsp::MainLoop::new_server(|client| {
5355
tracing::info!("Using CLI options: {:?}", cli);
56+
tracing::info!("Using fallback include path: {:?}", fallback_include_path);
5457
let router = ProtoLanguageServer::new_router(
5558
client.clone(),
5659
cli.include_paths
5760
.map(|ic| ic.into_iter().map(std::path::PathBuf::from).collect())
5861
.unwrap_or_default(),
62+
fallback_include_path,
5963
);
6064

6165
tokio::spawn({

src/server.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ pub struct ProtoLanguageServer {
3232
}
3333

3434
impl ProtoLanguageServer {
35-
pub fn new_router(client: ClientSocket, cli_include_paths: Vec<PathBuf>) -> Router<Self> {
35+
pub fn new_router(
36+
client: ClientSocket,
37+
cli_include_paths: Vec<PathBuf>,
38+
fallback_include_path: Option<PathBuf>,
39+
) -> Router<Self> {
3640
let mut router = Router::new(Self {
3741
client,
3842
counter: 0,
3943
state: ProtoLanguageState::new(),
40-
configs: WorkspaceProtoConfigs::new(cli_include_paths),
44+
configs: WorkspaceProtoConfigs::new(cli_include_paths, fallback_include_path),
4145
});
4246

4347
router.event::<TickEvent>(|st, _| {

0 commit comments

Comments
 (0)