Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use configuration from cwd when formatting from stdin #931

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use configuration from current working directory when formatting
from stdin and no stdin filepath provided
JohnnyMorganz committed Nov 30, 2024
commit 22180e88ef0d0fe869ddfd2e6d5c1e4aeaa91681
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
39 changes: 26 additions & 13 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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)
}
},
}
}

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