fornwall / rust-script

Run Rust files and expressions as scripts without any setup or compilation step.
https://rust-script.org
Apache License 2.0
1.2k stars 41 forks source link

Justfile integration #109

Closed mkmik closed 1 year ago

mkmik commented 1 year ago

I have a script that works with rust-script:

#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! dummy = { path = "./dummy" }
//! ```
fn main() {
   dummy::run();
}

It depends on a local crate in the same directory where the script file lives.

I'd like to run it via a runner like just. However when run from the runner, the script location is not in the same directory where the the runner recipe lives and thus the dependencies relative paths no longer work.

demo:
    #!/usr/bin/env rust-script
    //! ```cargo
    //! [dependencies]
    //! dummy = { path = "./dummy" }
    //! ```
    fn main() {
       dummy::run();
    }
$ just demo
error: failed to get `dummy` as a dependency of package `demo v0.1.0 (/Users/mkm/Library/Caches/rust-script/projects/f40865aa4eabcf6c25c2788e)`

Caused by:
  failed to load source for dependency `dummy`

Caused by:
  Unable to update /var/folders/zp/pk0v2q4x39z43k91vst1_jsc0000gn/T/justzxHC7g/dummy

Caused by:
  failed to read `/var/folders/zp/pk0v2q4x39z43k91vst1_jsc0000gn/T/justzxHC7g/dummy/Cargo.toml`

Caused by:
  No such file or directory (os error 2)
error: Could not execute cargo
error: Recipe `demo` failed with exit code 1

The runner runs the script with $PWD set to the place I expect it to be, but rust-script (correctly) uses the location of the rust script to find deps.

Is there a trick I can use in order to fool rust-script about its location (fiddling with argv[0] ?) Or could we add a flag to tell rust-script to use $PWD as base dir for resolving dependencies?

fornwall commented 1 year ago

@mkmik Makes sense to do something here!

What do you think about adding something like the below option:

  -b, --base-path [<base-path>]
          Base path for resolving dependencies

This could be used as in:

demo:
    #!/usr/bin/env rust-script --base-path {{justfile_directory()}}
    /// ```cargo
    /// [dependencies]
    /// demo = { path = "./demo" }
    /// ```
    use demo::add;
    println!("{}", add(1, 2));
mkmik commented 1 year ago

yes that sounds reasonable!

fornwall commented 1 year ago

@mkmik Great! This option (-b, --base-path) is now available in the just released 0.30.0 version - does it work for you?

mkmik commented 1 year ago

wow that was fast.

it does work!

$ cat justfile
demo:
    #!/usr/bin/env rust-script --base-path {{justfile_directory()}}
    //! ```cargo
    //! [dependencies]
    //! dummy = { path = "./dummy" }
    //! ```
    fn main() {
       dummy::run();
    }
$ cat ../../dummy/src/lib.rs 
pub fn run() {
  println!("it works");
}
$ just demo 
it works
mkmik commented 1 year ago

BTW, for posterity, this works too:

demo:
    #!/usr/bin/env rust-script --base-path {{justfile_directory()}}
    // cargo-deps: dummy = { path = "./dummy" }
    fn main() {
       dummy::run();
    }

and this too:

demo:
    #!/usr/bin/env rust-script --base-path {{justfile_directory()}} -d dummy={path="./dummy"}
    fn main() {
       dummy::run();
    }