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

Add support for offline compilation #38

Open bevsxyz opened 2 years ago

bevsxyz commented 2 years ago

I wish to use rust-script with my i3blocks to obtain system temperature. My script works. But it required internet for compilation, and hence I could not run it during startup, and my bar ignored it completely. Can we make use of the cache and offer an offline compilation mode?

cargo-script issue Modes suggestion

bevsxyz commented 2 years ago

Another solution would be only to compile when the source files have changed. This would allow the reuse of the cached binaries. Here the ideal way to go about it would be to save the binary as a hidden dotfile in the source directory of the script. If there is no binary or the script modified time is later than the binary created time, then compile the binary.

fornwall commented 2 years ago

@bevsxyz Thanks for creating the issue!

I'm a bit uncertain about exactly what --offline does? I see https://doc.rust-lang.org/cargo/faq.html stating:

The --offline flag was added in Rust 1.36.0. This flag tells Cargo to not access the network, and try to proceed with available cached data if possible

But I'm uncertain about what that exactly does - why doesn't cargo proceed available cached data if possible by default? Is it to check for updates (but does it do that even in the existence of a Cargo.lock file)?

In general I don't see that rust-script requires internet access - after the initial build, I can turn off internet access and it still runs. Do you have more details about when it doesn't work, but when the cargo --offline flag would make it work?

As seen above I don't really understand the current situation :), but perhaps we can move towards "offline by default when possible" - not only to work better without internet access, but also to reduce the overhead of running script (avoid slow network access when running a script) and reproducibility.

bevsxyz commented 2 years ago

@fornwall sorry about the delayed reply. I am not sure either of --offline. But what I was thinking of was that keep a hidden binary in the directory of the script and only attempt to compile if the timestamp of this binary is outdated to that of the script. My issue was that for some reason i3bar stopped showing the script output when I restarted my system. The Perl script I have works like a charm. There are some speedups with rust-script and I'd like to use it for more stuff in the future.

Perhaps this way of relying on time-stamp we may not have to call cargo every time and avoid that overhead.

Blisto91 commented 2 years ago

Here are some of the original offline mode issues/comments if that helps with clarification. https://github.com/rust-lang/cargo/issues/4686 https://github.com/rust-lang/cargo/issues/5655 https://github.com/rust-lang/cargo/issues/5655#issuecomment-488347426 https://github.com/rust-lang/cargo/pull/6934

The stabilization proposal give a pretty good tldr i think if it still holds true

The offline mode behavior is fairly straightforward. An overview of when running in offline mode:

  • The resolver will avoid updating the index, and if the required packages are already downloaded, then it will proceed as normal. If the required packages are not available, then it will give an error with a hint to run without offline mode.

  • If the index has never been downloaded, it will give an error suggesting running without offline.

  • Commands that must use the network, like cargo publish, provide a straightforward error message that it cannot be used in offline mode.

Blisto91 commented 2 years ago

@fornwall The commit where you added the --offline flag to cargo makes it so scripts that require dependencies outside of std will fail unless they have already been downloaded prior. So if i'm starting fresh the example in the readme with rand will fail because i both don't have the index downloaded nor the rand dependency. I have to compile a equivalent rust project outside of rust-script before it will succeed. (This is with internet connected)

It should only apply the offline flag if we don't have an internet connection. Tho i dunno how easy that would be or is there is a better method. Or if the offline flag even does what we want from this feature.

fornwall commented 2 years ago

The commit where you added the --offline flag to cargo makes it so scripts that require dependencies outside of std will fail unless they have already been downloaded prior.

@Blisto91 Thanks, reverted now!