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.21k stars 41 forks source link

Ability To Specify Script-Relative `include!()` Statement #6

Closed AldaronLau closed 3 years ago

AldaronLau commented 3 years ago

Doing something like

mod script_module {
    include!("./script-module.rs");
}

does not work.

I came up with the following solution:

mod script_module {
    include!(concat!(env!("PWD"), "/script-module.rs"));
}

But it is not completely cross platform, as PWD is not defined on Windows (I think unless you're in PowerShell or Git BASH).

All of the CARGO_* environment variables point inside the ./.rust-script folder, so there's no way to compose a path that points to the current directory.

I wonder if it's possible to override CARGO_MANIFEST_DIR to point to the directory in which the script is located, or make env!("RUST_SCRIPT_BASE_PATH") work (currently RUST_SCRIPT_BASE_PATH is only defined for the runtime of the script and not during rustc compilation).

fornwall commented 3 years ago

Hello and thanks for the issue!

In the just released 0.8.0(update with cargo install --force rust-script), the RUST_SCRIPT_BASE_PATH and other RUST_SCRIPT_* environment variables are also defined at compilation time, so env!("RUST_SCRIPT_BASE_PATH") will work.

Let me know if there are any issues related to that!