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

Rust: run sample .fsx via cargo-script (?) #3472

Closed
jkone27 opened this issue Jun 12, 2023 · 8 comments
Closed

Rust: run sample .fsx via cargo-script (?) #3472

jkone27 opened this issue Jun 12, 2023 · 8 comments

Comments

@jkone27
Copy link

jkone27 commented Jun 12, 2023

Description

trying to run a sample .fsx and have it executed by cargo script but i get some errors,
maybe we should have a way to keep/compile also comments, so that cargo script can still read comments in the output?

i have checked this: https://github.com/alfonsogarciacaro/fable-rust-sample/blob/main/Cargo.toml, not clear though how to do it with cargo script if possible..

Repro code

dotnet new tool-manifest
dotnet tool install fable --prerelease
touch test.fsx
dotnet fable test.fsx --lang rust
cargo install cargo-script
cargo script test.fs.rs // seems to fail for missing deps
cargo script -D cargo_fable test.fs.rs // fails as no loop or expr provided...

Expected and actual results

we are able to execute some F# code as Rust code

Related information

  • Fable version: dotnet fable --version : 4.1.4
  • Operating system: Mac OS
@ncave
Copy link
Collaborator

ncave commented Jun 13, 2023

@jkone27 Unfortunately comments are not available in the Fable AST.

We could add a Fable.Core.Rust helper to emit Rust comments in the future.

Perhaps using a cargo-script template can work, something like this:

//! ```cargo
//! [dependencies]
//! fable_library_rust = { path = "./fable_modules/fable-library-rust" }
//! ```
#![allow(unused_imports)]
#{prelude}
extern crate fable_library_rust;
#{script}

(although I wasn't able to make templates work, but perhaps I'm not doing something right).

Anyway, it's all about the dependency on fable_library_rust, so if you find a way to provide that dependency, it should work.

Personally I don't see much difference between using cargo-script vs. just putting your .fsx script in a /src folder and having a simple Cargo.toml (like the one you linked to above) to define the dependencies, therefore skipping the need for cargo-script to generate the package. But that's obviously up to you.

@jkone27
Copy link
Author

jkone27 commented Jun 14, 2023

cannot find that lib on crates.io, is it still there?

Updating crates.io index
error: no matching package named `fable_library_rust` found

@jkone27
Copy link
Author

jkone27 commented Jun 14, 2023

was trying to follow the sample here, but then had to modify toml to include a different bin,

[package]
name = "fable-rust-sample"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "testRust"
path = "testRust.rs"

[dependencies]
fable_library_rust = { path = "./fable_modules/fable-library-rust" }

and eventually ended up with this build error ..

.> cargo run --manifest-path /Users/admin/Repositories/scripts/test_fable_rust/Cargo.toml
Watching .
   Compiling core-foundation-sys v0.8.4
   Compiling cfg-if v1.0.0
   Compiling libc v0.2.146
   Compiling num-traits v0.2.15
   Compiling memchr v2.5.0
   Compiling regex-syntax v0.7.2
   Compiling arrayvec v0.7.3
   Compiling iana-time-zone v0.1.57
   Compiling time v0.1.45
   Compiling getrandom v0.2.10
   Compiling aho-corasick v1.0.2
   Compiling uuid v1.3.4
   Compiling num-integer v0.1.45
   Compiling rust_decimal v1.29.1
   Compiling chrono v0.4.26
   Compiling num-bigint v0.4.3
error: couldn't read /Users/admin/Repositories/scripts/test_fable_rust/target/debug/build/num-bigint-27dce587fe4d8a0f/out/radix_bases.rs: No such file or directory (os error 2)
   --> /Users/admin/.cargo/registry/src/github.com-1ecc6299db9ec823/num-bigint-0.4.3/src/biguint/convert.rs:759:9
    |
759 |         include! { concat!(env!("OUT_DIR"), "/radix_bases.rs") }
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `num-bigint` due to previous error

@jkone27
Copy link
Author

jkone27 commented Jun 14, 2023

is it related to this one? rust-num/num-bigint#218.

@ncave
Copy link
Collaborator

ncave commented Jun 14, 2023

@jkone27 Could be, not sure. I haven't encountered it cause I usually run stable.

It might have been fixed in the master branch, but the last num-bigint release is from 2021, so we might have to reference it from github.

I just tried rustc 1.72.0-nightly, seems to compile fine, are you running something else?

@jkone27
Copy link
Author

jkone27 commented Jun 15, 2023

i will update my rustc, i have : rustc 1.69.0 (84c898d65 2023-04-16)

@jkone27
Copy link
Author

jkone27 commented Jun 15, 2023

same error with latest stable from rustup (1.70)...

admin@LT-NLAMS-3108 ~ % rustc --version
rustc 1.70.0 (90c541806 2023-05-31)

ok sorry it worked after cargo clean and cargo run, but not when using fable clean and fable run commands...

i get error only when running via (error above with this script sequence) as in the fable rust test project

dotnet fable --lang rs -e rs --watch --runScript

this gives that error, but if i simply compile with fable to rust, and run instead

cargo clean
cargo run myscript.fsx

then it works fine!

@ncave
Copy link
Collaborator

ncave commented Jun 15, 2023

@jkone27 Yes, --watch and --runScript flags are mostly for targeting JavaScript where there is no subsequent compilation step after Fable is done. Same thing for clean, it only cleans Fable-produced files, not build artifacts of compilations that happen after that.

I'm glad it works, feel free to close this, or rename to a more specific issue if you have one.
Thanks for testing and providing feedback!

@jkone27 jkone27 closed this as completed Jun 16, 2023
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

No branches or pull requests

2 participants