diff --git a/CHANGELOG.md b/CHANGELOG.md index 8865192b..64e04512 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- 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)) + ## [2.0.1] - 2024-11-18 ### Added diff --git a/src/cli/config.rs b/src/cli/config.rs index 1d0d9fa9..1a76541e 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -55,15 +55,22 @@ impl ConfigResolver<'_> { }) } + /// Returns the root used when searching for configuration + /// If `--search-parent-directories`, then there is no root, and we keep searching + /// Else, the root is the current working directory, and we do not search higher than the cwd + fn get_configuration_search_root(&self) -> Option { + match self.opt.search_parent_directories { + true => None, + false => Some(self.current_directory.to_path_buf()), + } + } + pub fn load_configuration(&mut self, path: &Path) -> Result { if let Some(configuration) = self.forced_configuration { return Ok(configuration); } - let root = match self.opt.search_parent_directories { - true => None, - false => Some(self.current_directory.to_path_buf()), - }; + let root = self.get_configuration_search_root(); let absolute_path = self.current_directory.join(path); let parent_path = &absolute_path @@ -91,19 +98,25 @@ impl ConfigResolver<'_> { return Ok(configuration); } + let root = self.get_configuration_search_root(); + let my_current_directory = self.current_directory.to_owned(); + match &self.opt.stdin_filepath { Some(filepath) => self.load_configuration(filepath), - None => { - #[cfg(feature = "editorconfig")] - if self.opt.no_editorconfig { + None => match self.find_config_file(&my_current_directory, root)? { + Some(config) => Ok(config), + None => { + #[cfg(feature = "editorconfig")] + if self.opt.no_editorconfig { + Ok(self.default_configuration) + } else { + editorconfig::parse(self.default_configuration, &PathBuf::from("*.lua")) + .context("could not parse editorconfig") + } + #[cfg(not(feature = "editorconfig"))] Ok(self.default_configuration) - } else { - editorconfig::parse(self.default_configuration, &PathBuf::from("*.lua")) - .context("could not parse editorconfig") } - #[cfg(not(feature = "editorconfig"))] - Ok(self.default_configuration) - } + }, } } diff --git a/src/cli/main.rs b/src/cli/main.rs index 10d7e973..bcaad741 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -810,6 +810,24 @@ mod tests { cwd.close().unwrap(); } + #[test] + fn test_cwd_configuration_respected_when_formatting_from_stdin() { + let cwd = construct_tree!({ + "stylua.toml": "quote_style = 'AutoPreferSingle'", + "foo.lua": "local x = \"hello\"", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .arg("-") + .write_stdin("local x = \"hello\"") + .assert() + .success() + .stdout("local x = 'hello'\n"); + + cwd.close().unwrap(); + } + #[test] fn test_cwd_configuration_respected_for_file_in_cwd() { let cwd = construct_tree!({