Skip to content

Commit

Permalink
Lexiclean search directory so .. does not check the current directo…
Browse files Browse the repository at this point in the history
…ry (#2236)

If the search directory was `..`, for example in the invocation
`just ../foo`, we would wind up checking the justfile in the current
directory since we did `INVOCATION_DIRECTORY/..`.ancestors(), which
would first return `INVOCATION_DIRECTORY`.

Instead, lexiclean the result of joining th invocation directory with
the search directory, so `..` is removed, and `ancestors()` doesn't
return the invocation directory.
  • Loading branch information
casey authored Jul 8, 2024
1 parent f1020b4 commit 5648142
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const DEFAULT_JUSTFILE_NAME: &str = JUSTFILE_NAMES[0];
pub(crate) const JUSTFILE_NAMES: [&str; 2] = ["justfile", ".justfile"];
const PROJECT_ROOT_CHILDREN: &[&str] = &[".bzr", ".git", ".hg", ".svn", "_darcs"];

#[derive(Debug)]
pub(crate) struct Search {
pub(crate) justfile: PathBuf,
pub(crate) working_directory: PathBuf,
Expand Down
7 changes: 4 additions & 3 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ impl Subcommand {
) {
let starting_path = match &config.search_config {
SearchConfig::FromInvocationDirectory => config.invocation_directory.clone(),
SearchConfig::FromSearchDirectory { search_directory } => {
env::current_dir().unwrap().join(search_directory)
}
SearchConfig::FromSearchDirectory { search_directory } => config
.invocation_directory
.join(search_directory)
.lexiclean(),
_ => unreachable!(),
};

Expand Down
16 changes: 16 additions & 0 deletions tests/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ fn single_upwards() {
search_test(path, &["../"]);
}

#[test]
fn double_upwards() {
let tmp = temptree! {
justfile: "default:\n\techo ok",
foo: {
bar: {
justfile: "default:\n\techo foo",
},
},
};

let path = tmp.path().join("foo/bar");

search_test(path, &["../default"]);
}

#[test]
fn find_dot_justfile() {
Test::new()
Expand Down

0 comments on commit 5648142

Please sign in to comment.