Skip to content

Commit f581279

Browse files
Use configuration from cwd when formatting from stdin (#931)
Use configuration from current working directory when formatting from stdin and no stdin filepath provided
1 parent d67c77b commit f581279

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed regression where configuration present in current working directory not used when formatting from stdin and no `--stdin-filepath` is provided ([#928](https://github.com/JohnnyMorganz/StyLua/issues/928))
13+
1014
## [2.0.1] - 2024-11-18
1115

1216
### Added

src/cli/config.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@ impl ConfigResolver<'_> {
5555
})
5656
}
5757

58+
/// Returns the root used when searching for configuration
59+
/// If `--search-parent-directories`, then there is no root, and we keep searching
60+
/// 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> {
62+
match self.opt.search_parent_directories {
63+
true => None,
64+
false => Some(self.current_directory.to_path_buf()),
65+
}
66+
}
67+
5868
pub fn load_configuration(&mut self, path: &Path) -> Result<Config> {
5969
if let Some(configuration) = self.forced_configuration {
6070
return Ok(configuration);
6171
}
6272

63-
let root = match self.opt.search_parent_directories {
64-
true => None,
65-
false => Some(self.current_directory.to_path_buf()),
66-
};
73+
let root = self.get_configuration_search_root();
6774

6875
let absolute_path = self.current_directory.join(path);
6976
let parent_path = &absolute_path
@@ -91,19 +98,25 @@ impl ConfigResolver<'_> {
9198
return Ok(configuration);
9299
}
93100

101+
let root = self.get_configuration_search_root();
102+
let my_current_directory = self.current_directory.to_owned();
103+
94104
match &self.opt.stdin_filepath {
95105
Some(filepath) => self.load_configuration(filepath),
96-
None => {
97-
#[cfg(feature = "editorconfig")]
98-
if self.opt.no_editorconfig {
106+
None => match self.find_config_file(&my_current_directory, root)? {
107+
Some(config) => Ok(config),
108+
None => {
109+
#[cfg(feature = "editorconfig")]
110+
if self.opt.no_editorconfig {
111+
Ok(self.default_configuration)
112+
} else {
113+
editorconfig::parse(self.default_configuration, &PathBuf::from("*.lua"))
114+
.context("could not parse editorconfig")
115+
}
116+
#[cfg(not(feature = "editorconfig"))]
99117
Ok(self.default_configuration)
100-
} else {
101-
editorconfig::parse(self.default_configuration, &PathBuf::from("*.lua"))
102-
.context("could not parse editorconfig")
103118
}
104-
#[cfg(not(feature = "editorconfig"))]
105-
Ok(self.default_configuration)
106-
}
119+
},
107120
}
108121
}
109122

src/cli/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,24 @@ mod tests {
810810
cwd.close().unwrap();
811811
}
812812

813+
#[test]
814+
fn test_cwd_configuration_respected_when_formatting_from_stdin() {
815+
let cwd = construct_tree!({
816+
"stylua.toml": "quote_style = 'AutoPreferSingle'",
817+
"foo.lua": "local x = \"hello\"",
818+
});
819+
820+
let mut cmd = create_stylua();
821+
cmd.current_dir(cwd.path())
822+
.arg("-")
823+
.write_stdin("local x = \"hello\"")
824+
.assert()
825+
.success()
826+
.stdout("local x = 'hello'\n");
827+
828+
cwd.close().unwrap();
829+
}
830+
813831
#[test]
814832
fn test_cwd_configuration_respected_for_file_in_cwd() {
815833
let cwd = construct_tree!({

0 commit comments

Comments
 (0)