Skip to content

Commit

Permalink
Use configuration from cwd when formatting from stdin (#931)
Browse files Browse the repository at this point in the history
Use configuration from current working directory when formatting
from stdin and no stdin filepath provided
  • Loading branch information
JohnnyMorganz authored Nov 30, 2024
1 parent d67c77b commit f581279
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 26 additions & 13 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
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<Config> {
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
Expand Down Expand Up @@ -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)
}
},
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!({
Expand Down

0 comments on commit f581279

Please sign in to comment.