Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Luau: fixed parentheses incorrectly removed in `(expr :: assertion) < foo` when multilining the expression, leading to a syntax error ([#940](https://github.com/JohnnyMorganz/StyLua/issues/940))
- Fixed panic when attempting to format a file outside of the current working directory when `--respect-ignores` is enabled ([#969](https://github.com/JohnnyMorganz/StyLua/pull/969))

## [2.0.2] - 2024-12-07

Expand Down
38 changes: 36 additions & 2 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ fn path_is_stylua_ignored(path: &Path, search_parent_directories: bool) -> Resul
)
.context("failed to parse ignore file")?;

// matched_path_or_any_parents panics when path is not in cwd
// can happen when `--respect-ignores --stdin-filepath {path}`
if !path
.canonicalize()
.unwrap_or_default()
.starts_with(ignore.path().canonicalize().unwrap_or_default())
{
return Ok(false);
}

Ok(matches!(
ignore.matched_path_or_any_parents(path, false),
ignore::Match::Ignore(_)
Expand Down Expand Up @@ -624,12 +634,12 @@ mod tests {
use assert_fs::prelude::*;

macro_rules! construct_tree {
({ $($file_name:literal:$file_contents:literal,)+ }) => {{
({ $($file_name:literal:$file_contents:literal,)* }) => {{
let cwd = assert_fs::TempDir::new().unwrap();

$(
cwd.child($file_name).write_str($file_contents).unwrap();
)+
)*

cwd
}};
Expand Down Expand Up @@ -724,6 +734,30 @@ mod tests {
cwd.close().unwrap();
}

#[test]
fn explicitly_provided_files_not_in_cwd() {
let cwd = construct_tree!({
".styluaignore": "foo.lua",
});

let another = construct_tree!({});

let mut cmd = create_stylua();
cmd.current_dir(cwd.path())
.args([
"--respect-ignores",
"--stdin-filepath",
another.child("foo.lua").to_str().unwrap(),
"-",
])
.write_stdin("local x = 1")
.assert()
.success()
.stdout("local x = 1\n");

cwd.close().unwrap();
}

#[test]
fn test_respect_ignores() {
let cwd = construct_tree!({
Expand Down