Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#340 Allow justfile without workdir #355

Closed
wants to merge 9 commits into from

Conversation

smonami
Copy link

@smonami smonami commented Sep 17, 2018

#340 Making --working-directory optional for --justfile and --edit.

Copy link
Owner

@casey casey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patch! I have a few suggestions for how to avoid some unwraps.

I think this needs a test that ensures that the correct working directory is chosen. Would you like to take a stab at writing one? You can check out the existing tests in the tests directory for ideas. I'm also happy to write it myself.

src/run.rs Outdated
@@ -1,6 +1,6 @@
use common::*;

use std::{convert, ffi};
use std::{convert, ffi };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this change isn't needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected extra space.

src/run.rs Outdated
@@ -86,8 +86,7 @@ pub fn run() {
.short("f")
.long("justfile")
.takes_value(true)
.help("Use <JUSTFILE> as justfile. --working-directory must also be set")
.requires("WORKING-DIRECTORY"))
.help("Use <JUSTFILE> as justfile. --working-directory can be set [default: supplied justfile location]"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that since the behavior of just in setting the working directory is the same as normal when using this flag, we can make the comment shorter. Just Use <JUSTFILE> as justfile. is fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/run.rs Outdated
@@ -187,7 +186,12 @@ pub fn run() {
.collect::<Vec<&str>>();

let justfile_option = matches.value_of("JUSTFILE");
let working_directory_option = matches.value_of("WORKING-DIRECTORY");
let mut working_directory_option = matches.value_of("WORKING-DIRECTORY");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change working_directory_option to a Option<&Path>, so that we're working with paths from here on out, and don't have to convert to a string. You can do .map(Path::new).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/run.rs Outdated
let working_directory_option = matches.value_of("WORKING-DIRECTORY");
let mut working_directory_option = matches.value_of("WORKING-DIRECTORY");

if justfile_option.is_some() && working_directory_option.is_none() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to an if let, so we can avoid the unwraps:

if let (Some(justfile), None) = (justfile_option, working_directory_option) {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/run.rs Outdated
let mut working_directory_option = matches.value_of("WORKING-DIRECTORY");

if justfile_option.is_some() && working_directory_option.is_none() {
let justfile_path = Path::new(justfile_option.unwrap()).parent();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If parent() returns none, we should exit with a message, like die!("Could not find parent directory of justfile at {}", justfile)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added none check. But there seems to be a known issue(rust-lang/rust#36861) with parent returning "" in certain cases. Please suggest if it can be handled in a better way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! Let's resolve the justfile to an absolute path, and then using the parent of that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should fix the issue with parent returning ""

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: I am very new to Rust :-), and although I understand reference concept, not able to find a 'Rust way' to deal with following situation and so having trouble with the code:

if let (Some(justfile), None) = (justfile_option, working_directory_option) {
     let justfile_path = Path::new(justfile);
    if Path::new(justfile).is_absolute() {
        working_directory_option = Path::new(justfile).parent().unwrap().to_str();
    } else {
      let justfile_path_canonical = justfile_path.canonicalize();
      if justfile_path.canonicalize().is_ok() {
// @casey following line errors out with borrowed value does not live long enough. Any suggestion?
        working_directory_option = justfile_path_canonical.ok().unwrap().to_str(); 
      } else {
        die!("Could not find parent directory of justfile at {}.", justfile);
      }
    }
    //println!("working_directory_option:{:?}", working_directory_option);
  }

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all, we were all new to Rust once :)

Can you push the code (even if it's broken) to the PR branch so I can try it out?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It's broken.

@casey
Copy link
Owner

casey commented Oct 25, 2018

I changed it to turn justfile_option and working_directory_option in a Path and Pathbuf respectively. Using path types simplifies the code because now conversions aren't required to and from &str. working_directory_option is a PathBuf so that it can be modified if need be, and this also solves the lifetime problem. This still needs a test, but I'm happy to write one if you'd rather not.

@casey casey mentioned this pull request Oct 25, 2018
@casey
Copy link
Owner

casey commented Nov 16, 2018

This will need a full integration test, i.e. a test that makes sure that the working directory is correct when --working-directory isn't passed.

@casey
Copy link
Owner

casey commented Apr 8, 2019

Just added a test and landed this in #392. Thank you for contributing!

@casey casey closed this Apr 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants