@@ -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
2526impl 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}
0 commit comments