Skip to content

Commit 899fd0e

Browse files
Use workspace folder as search root for configuration in LSP mode (#1036)
* Handle config search root in LSP * Update changelog
1 parent 398a4ef commit 899fd0e

File tree

3 files changed

+474
-175
lines changed

3 files changed

+474
-175
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
- Fixed comments lost from expression after parentheses are removed when we are attempting to "hang" the expression. ([#1033](https://github.com/JohnnyMorganz/StyLua/issues/1033))
2323
- Fixed `document_range_formatting_provider` capability missing from `ServerCapabilities` in language server mode
24+
- Fixed current working directory incorrectly used as config search root in language server mode -- now, the root of the opened workspace is used instead ([#1032](https://github.com/JohnnyMorganz/StyLua/issues/1032))
2425

2526
## [2.2.0] - 2025-09-14
2627

src/cli/config.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,28 @@ impl ConfigResolver<'_> {
5858
/// Returns the root used when searching for configuration
5959
/// If `--search-parent-directories`, then there is no root, and we keep searching
6060
/// Else, the root is the current working directory, and we do not search higher than the cwd
61-
fn get_configuration_search_root(&self) -> Option<PathBuf> {
61+
fn get_configuration_search_root(
62+
&self,
63+
search_root_override: Option<PathBuf>,
64+
) -> Option<PathBuf> {
6265
match self.opt.search_parent_directories {
6366
true => None,
64-
false => Some(self.current_directory.to_path_buf()),
67+
false => {
68+
Some(search_root_override.unwrap_or_else(|| self.current_directory.to_path_buf()))
69+
}
6570
}
6671
}
6772

68-
pub fn load_configuration(&mut self, path: &Path) -> Result<Config> {
73+
pub(crate) fn load_configuration_with_search_root(
74+
&mut self,
75+
path: &Path,
76+
search_root_override: Option<PathBuf>,
77+
) -> Result<Config> {
6978
if let Some(configuration) = self.forced_configuration {
7079
return Ok(configuration);
7180
}
7281

73-
let root = self.get_configuration_search_root();
82+
let root = self.get_configuration_search_root(search_root_override);
7483

7584
let absolute_path = self.current_directory.join(path);
7685
let parent_path = &absolute_path
@@ -93,12 +102,16 @@ impl ConfigResolver<'_> {
93102
}
94103
}
95104

105+
pub fn load_configuration(&mut self, path: &Path) -> Result<Config> {
106+
self.load_configuration_with_search_root(path, None)
107+
}
108+
96109
pub fn load_configuration_for_stdin(&mut self) -> Result<Config> {
97110
if let Some(configuration) = self.forced_configuration {
98111
return Ok(configuration);
99112
}
100113

101-
let root = self.get_configuration_search_root();
114+
let root = self.get_configuration_search_root(None);
102115
let my_current_directory = self.current_directory.to_owned();
103116

104117
match &self.opt.stdin_filepath {

0 commit comments

Comments
 (0)