diff --git a/CHANGELOG.md b/CHANGELOG.md index c606d4d1..71df5113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/cli/main.rs b/src/cli/main.rs index 40450be9..16eead5c 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -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(_) @@ -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 }}; @@ -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!({