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.27k stars 43 forks source link

Example dependencies don't seem to be working #144

Closed andar1an closed 3 weeks ago

andar1an commented 3 weeks ago

I am trying to get an example script working with dependencies from the example repo, but for either dependency inclusion method, it seems that dependencies are not being compiled and cargo toml and lock may not be generated? I need to path explicit flag still to see what is happening.

For example:

//! This is a regular crate doc comment, but it also contains a partial
//! Cargo manifest.  Note the use of a *fenced* code block, and the
//! `cargo` "language".
//!
//! ```cargo
//! [dependencies]
//! time = "0.1.25"
//! ```

fn main() {
    println!("{}", time::now().rfc822z());
}

will state use of undeclared crate or module "time".

I have tried with various quotes e.g. /// vs //! vs // vs no quotes with just fence, as well as use statements and other methods of including dependency, and making sure file is executable but can't seem to get this working.

Am I missing something?

Log:

[2024-11-02T22:18:48Z INFO  rust_script] Arguments: Args { script: Some("example.ers"), script_args: ["--clear-cache"], expr: false, loop_: false, count: false, base_path: None, pkg_path: None, gen_pkg_only: false, cargo_output: true, clear_cache: false, debug: false, dep: [], extern_: [], force: false, unstable_features: [], build_kind: Normal, toolchain_version: None, wrapper: None }
[2024-11-02T22:18:48Z INFO  rust_script] input: File("example", "/home/username/example.ers", "#!/usr/bin/env rust-script\n\n// https://rust-script.org/#scripts\n// https://github.com/fornwall/rust-script/tree/main/examples\n\n//! This is a regular crate doc comment, but it also contains a partial\n//! Cargo manifest.  Note the use of a *fenced* code block, and the\n//! `cargo` \"language\".\n//!\n//! ```cargo\n//! [dependencies]\n//! time = \"0.1/home/username")
[2024-11-02T22:18:48Z INFO  rust_script] prelude_items: []
[2024-11-02T22:18:48Z INFO  rust_script] id: "757b44cade3c09a2f7355d82"
[2024-11-02T22:18:48Z INFO  rust_script] pkg_path: "/home/username/.cache/rust-script/projects/757b44cade3c09a2f7355d82"
[2024-11-02T22:18:48Z INFO  rust_script] using_cache: true
[2024-11-02T22:18:48Z INFO  rust_script::manifest] part_mani: {}
[2024-11-02T22:18:48Z INFO  rust_script::manifest] source: None
[2024-11-02T22:18:48Z INFO  rust_script::manifest] manifest: [[bin]]
    name = "example_757b44cade3c09a2f7355d82"
    path = "/home/username/example.ers"

    [dependencies]

    [package]
    authors = ["Anonymous"]
    edition = "2021"
    name = "example_757b44cade3c09a2f7355d82"
    version = "0.1.0"

    [profile.release]
    strip = true

[2024-11-02T22:18:48Z INFO  rust_script] action: InputAction { cargo_output: true, force_compile: false, execute: true, pkg_path: "/home/username/.cache/rust-script/projects/757b44cade3c09a2f7355d82", script_path: "/home/username/example.ers", using_cache: true, toolchain_version: None, debug: false, manifest: "[[bin]]\nname = \"example_757b44cade3c09a2f7355d82\"\npath = \"//home/username/example.ers\"\n\n[dependencies]\n\n[package]\nauthors = [\"Anonymous\"]\nedition = \"2021\"\nname = \"example_757b44cade3c09a2f7355d82\"\nversion = \"0.1.0\"\n\n[profile.release]\nstrip = true\n", script: None, build_kind: Normal, bin_name: "example_757b44cade3c09a2f7355d82", original_script_path: Some("example.ers") }
[2024-11-02T22:18:48Z INFO  rust_script] creating pkg dir...
[2024-11-02T22:18:48Z INFO  rust_script] generating Cargo package...
[2024-11-02T22:18:48Z DEBUG rust_script] overwrite_file("/home/username/.cache/rust-script/projects/757b44cade3c09a2f7355d82/Cargo.toml", _)
[2024-11-02T22:18:48Z DEBUG rust_script] Equal content
[2024-11-02T22:18:48Z INFO  rust_script] disarming pkg dir cleanup...
[2024-11-02T22:18:48Z DEBUG rust_script] No old binary found
   Compiling example_757b44cade3c09a2f7355d82 v0.1.0 (/home/username/.cache/rust-script/projects/757b44cade3c09a2f7355d82)
error[E0433]: failed to resolve: use of undeclared crate or module `time`
  --> /home/username/example.ers:16:20
   |
16 |     println!("{}", time::now().rfc822z());
   |                    ^^^^ use of undeclared crate or module `time`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example_757b44cade3c09a2f7355d82` (bin "example_757b44cade3c09a2f7355d82") due to 1 previous error
[2024-11-02T22:18:48Z INFO  rust_script] cleaning cache with max_age: 604800000
[2024-11-02T22:18:48Z INFO  rust_script] cutoff:            1729981128361 ms
[2024-11-02T22:18:48Z INFO  rust_script] checking: "/home/username/.cache/rust-script/projects/757b44cade3c09a2f7355d82"
[2024-11-02T22:18:48Z INFO  rust_script] meta_mtime:        1730585737204 ms
[2024-11-02T22:18:48Z INFO  rust_script] done cleaning cache.
error: Could not execute cargo

No matter what I do, dependencies in generated toml is empty

andar1an commented 3 weeks ago

I had single line comments before the cargo doc comments to direct to links for rust-script.org.

Because rust-script expects the first single line comment to be a type of dependency, the following dependencies were not being used.

Moving the cargo fence to just after hash-bang corrected issue.