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

Cargo does not handle +toolchain directives. #14363

Closed
guest271314 opened this issue Aug 7, 2024 · 4 comments
Closed

Cargo does not handle +toolchain directives. #14363

guest271314 opened this issue Aug 7, 2024 · 4 comments
Labels
C-question Category: question

Comments

@guest271314
Copy link

Problem

The documentation https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#script says:

script
Tracking Issue: #12207
Cargo can directly run .rs files as:

$ cargo +nightly -Zscript file.rs

where file.rs can be as simple as:

fn main() {}

Steps

I just fetched the rust-nightly-x86_64-unknown-linux-gnu.tar.xz archive, extracted the cargo folder to the same directory where I have rustc and rust-std-x86_64-unknown-linux-gnu folders extracted from the same archive.

I have this for testing

script.rs

use std::env;
// https://stackoverflow.com/a/75981598
fn main() {
  let args: Vec<String> = env::args().collect();
  if args.len() > 1 {
      println!("Hello {}!", &args[1]);
  } else {
      println!("Hello world!");
  }
}

This is result

$ cargo/bin/cargo +nightly -Zscript ~/bin/script.rs
error: no such command: `+nightly`

	Cargo does not handle `+toolchain` directives.
	Did you mean to invoke `cargo` through `rustup` instead?

Possible Solution(s)

No idea. This is only my second time trying to use Rust.

Notes

It's basically impossible to download the entire Rust toolchain on a temporary file system running a live Linux session.

So I fetched the Nightly archive, immediately got rid of the 600+ MB documentation folder, and everything else except rustc and rust-std-x86_64-unknown-linux-gnu folders.

I'll re-fetch the nightly archive and extract cargo folder.
Eventually I got this to run, using -L to I guess link to rustlib/x86_64-unknown-linux-gnu/lib

#!/bin/sh
//bin/bash -ec '[ "$0" -nt "${0%.*}" ] && /home/user/bin/rustc/bin/rustc -L /home/user/bin/rust-std-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib "$0" -o "${0%.*}"; "${0%.*}" "$@"' "$0" "$@"; exit $?

use std::env;
// https://stackoverflow.com/a/75981598
fn main() {
  let args: Vec<String> = env::args().collect();
  if args.len() > 1 {
      println!("Hello {}!", &args[1]);
  } else {
      println!("Hello world!");
  }
}

What I'm really working on doing is using Rust .rs file as a Native Messaging host script.

I converted a working C version to Rust using https://c2rust.com/.

Now I have to figure out how to link to libc in the resulting file https://gist.github.com/rust-play/92af6fefec0ea8bcc76a5b18be40005e

//https://stackoverflow.com/q/41322300
//https://c2rust.com/
//https://github.com/rust-lang/rust/issues/1772
#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
#![register_tool(c2rust)]
#![feature(register_tool)]
extern "C" {
    static mut stdin: *mut _IO_FILE;
    static mut stdout: *mut _IO_FILE;
    fn fflush(__stream: *mut FILE) -> libc::c_int;
// ...

Just to see what would happen if I was trying to compile the C to Rust code

./rust.sh "test"
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
  --> ./rust.sh:10:39

// ...

118 |         free(message as *mut libc::c_void);
    |                              ^^^^ use of undeclared crate or module `libc`

error: aborting due to 58 previous errors

For more information about this error, try `rustc --explain E0433`.

This is my second time trying to use Rust to build something. My first time using Rust as a script. Thanks.

Version

cargo 1.82.0-nightly (fa6465836 2024-08-02)
release: 1.82.0-nightly
commit-hash: fa646583675d7c140482bd906145c71b7fb4fc2b
commit-date: 2024-08-02
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/3.3.1)
ssl: OpenSSL 3.3.1 4 Jun 2024
os: Ubuntu 22.4.0 (jammy) [64-bit]
@guest271314 guest271314 added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Aug 7, 2024
@weihanglo
Copy link
Member

This is expected, and as I said, you need to run cargo -Zscript without +nightly. See https://doc.rust-lang.org/nightly/cargo/commands/cargo.html#common-options.

If Cargo has been installed with rustup, and the first argument to cargo begins with +, it will be interpreted as a rustup toolchain name (such as +stable or +nightly). See the rustup documentation for more information about how toolchain overrides work.

@guest271314
Copy link
Author

$ cargo/bin/cargo -Zscript ~/bin/script.rs
warning: `package.edition` is unspecified, defaulting to `2021`
error: could not execute process `rustc -vV` (never executed)

Caused by:
  Permission denied (os error 13)

@guest271314
Copy link
Author

It looks like cargo depends on rustc. However I have not installed rustc. I just have the rustc, cargo, and rust-std-x86_64-unknown-linux-gnu folders in a local directory. No rustup.

@weihanglo
Copy link
Member

Yes. You have to install the rust toolchain correctly via rustup or some other installation method listed here: https://rust-lang.github.io/rustup/installation/index.html. Standalone installation method can be found here: https://forge.rust-lang.org/infra/other-installation-methods.html.

The toolchain installation is out of scope of Cargo and I have limited time to support such kind of tickets. https://users.rust-lang.org/ is one place for user support.

Close since this is not a bug in Cargo. Thank you.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2024
@weihanglo weihanglo added C-question Category: question and removed C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-question Category: question
Projects
None yet
Development

No branches or pull requests

2 participants