Skip to content

Commit

Permalink
Move cd out of Config::from_matches (#513)
Browse files Browse the repository at this point in the history
Move `env::set_current_dir` outside of `Config::from_matches()` and into
`run()`. It's a bit cleaner this way, and sets us up to make the just
process not change its own directory at all, instead explicitly changing
the working directory of its child processes.
  • Loading branch information
casey authored Nov 7, 2019
1 parent 2ff33de commit 33ba66d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) struct Config<'a> {
pub(crate) justfile: Option<&'a Path>,
pub(crate) working_directory: Option<&'a Path>,
pub(crate) invocation_directory: Result<PathBuf, String>,
pub(crate) search_directory: Option<&'a Path>,
}

mod cmd {
Expand Down Expand Up @@ -248,6 +249,8 @@ impl<'a> Config<'a> {
overrides.insert(name, value);
}

let mut search_directory = None;

let arguments = raw_arguments
.into_iter()
.skip_while(is_override)
Expand All @@ -261,9 +264,7 @@ impl<'a> Config<'a> {

let (dir, recipe) = argument.split_at(i + 1);

if let Err(error) = env::set_current_dir(dir) {
die!("Error changing directory: {}", error);
}
search_directory = Some(Path::new(dir));

if recipe.is_empty() {
return None;
Expand Down Expand Up @@ -300,6 +301,7 @@ impl<'a> Config<'a> {
shell: matches.value_of(arg::SHELL).unwrap(),
justfile: matches.value_of(arg::JUSTFILE).map(Path::new),
working_directory: matches.value_of(arg::WORKING_DIRECTORY).map(Path::new),
search_directory,
invocation_directory,
subcommand,
verbosity,
Expand All @@ -326,6 +328,7 @@ impl<'a> Default for Config<'a> {
working_directory: None,
invocation_directory: env::current_dir()
.map_err(|e| format!("Error getting current directory: {}", e)),
search_directory: None,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ pub fn run() -> Result<(), i32> {

let justfile = config.justfile;

if let Some(directory) = config.search_directory {
if let Err(error) = env::set_current_dir(&directory) {
die!(
"Error changing directory to {}: {}",
directory.display(),
error
);
}
}

let mut working_directory = config.working_directory.map(PathBuf::from);

if let (Some(justfile), None) = (justfile, working_directory.as_ref()) {
Expand Down

0 comments on commit 33ba66d

Please sign in to comment.