Skip to content

Commit 923e912

Browse files
Fix panic when --respect-ignores --stdin-filepath on external path to cwd (#969)
* Fix panic when `--respect-ignores --stdin-filepath` on external path to cwd * Update changelog --------- Co-authored-by: JohnnyMorganz <[email protected]>
1 parent 36981d7 commit 923e912

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Fixed
1616

1717
- 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))
18+
- 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))
1819

1920
## [2.0.2] - 2024-12-07
2021

src/cli/main.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ fn path_is_stylua_ignored(path: &Path, search_parent_directories: bool) -> Resul
246246
)
247247
.context("failed to parse ignore file")?;
248248

249+
// matched_path_or_any_parents panics when path is not in cwd
250+
// can happen when `--respect-ignores --stdin-filepath {path}`
251+
if !path
252+
.canonicalize()
253+
.unwrap_or_default()
254+
.starts_with(ignore.path().canonicalize().unwrap_or_default())
255+
{
256+
return Ok(false);
257+
}
258+
249259
Ok(matches!(
250260
ignore.matched_path_or_any_parents(path, false),
251261
ignore::Match::Ignore(_)
@@ -624,12 +634,12 @@ mod tests {
624634
use assert_fs::prelude::*;
625635

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

630640
$(
631641
cwd.child($file_name).write_str($file_contents).unwrap();
632-
)+
642+
)*
633643

634644
cwd
635645
}};
@@ -724,6 +734,30 @@ mod tests {
724734
cwd.close().unwrap();
725735
}
726736

737+
#[test]
738+
fn explicitly_provided_files_not_in_cwd() {
739+
let cwd = construct_tree!({
740+
".styluaignore": "foo.lua",
741+
});
742+
743+
let another = construct_tree!({});
744+
745+
let mut cmd = create_stylua();
746+
cmd.current_dir(cwd.path())
747+
.args([
748+
"--respect-ignores",
749+
"--stdin-filepath",
750+
another.child("foo.lua").to_str().unwrap(),
751+
"-",
752+
])
753+
.write_stdin("local x = 1")
754+
.assert()
755+
.success()
756+
.stdout("local x = 1\n");
757+
758+
cwd.close().unwrap();
759+
}
760+
727761
#[test]
728762
fn test_respect_ignores() {
729763
let cwd = construct_tree!({

0 commit comments

Comments
 (0)