Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ libc = "0.2.177"
libgit2-sys = "0.18.2"
libloading = "0.8.9"
memchr = "2.7.6"
memfd = "0.6.5"
miow = "0.6.1"
opener = "0.8.3"
openssl = "0.10.74"
Expand Down Expand Up @@ -261,6 +262,9 @@ gix-transport = { version = "0.49.1", features = ["http-client-insecure-credenti
same-file.workspace = true
snapbox.workspace = true

[target.'cfg(target_os = "linux")'.dev-dependencies]
memfd.workspace = true

[build-dependencies]
flate2.workspace = true
tar.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,8 @@ impl GlobalContext {
.to_string(),
),
("{workspace-path-hash}", {
let real_path = std::fs::canonicalize(workspace_manifest_path)?;
let real_path = std::fs::canonicalize(workspace_manifest_path)
.unwrap_or_else(|_err| workspace_manifest_path.to_owned());
let hash = crate::util::hex::short_hash(&real_path);
format!("{}{}{}", &hash[0..2], std::path::MAIN_SEPARATOR, &hash[2..])
}),
Expand Down
38 changes: 38 additions & 0 deletions tests/testsuite/script/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::Write;

use crate::prelude::*;
use cargo_test_support::basic_manifest;
Expand Down Expand Up @@ -2174,3 +2175,40 @@ args: []
"#]])
.run();
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
#[cfg(target_os = "linux")]
fn memfd_script() {
use std::os::fd::AsRawFd;

let fd = memfd::MemfdOptions::new()
.close_on_exec(false)
.create("otkeep-script")
.unwrap();
Comment on lines +2184 to +2187
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we instead use libc::memfd_create so we don't need to pull in another dependency (it is lightweight I agree). Something like

let ret  = unsafe { libc::memfd_create(0) };
if ret < 0 {
    panic!("failed to created memfd: {}", std::io::Error::last_os_error());
}
let raw_fd = ret;
let file = std::fs::File::from_raw_fd(raw_fd);
...

Or do we have any more plans on memfd support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just didn't want to be responsible for unsafe code for APIs I'm unfamiliar with. If you think it is worth it, I'll change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or https://docs.rs/rustix/1.1.2/rustix/fs/fn.memfd_create.html since we already depend on rustix anyway?

Regardless, feel free to proceed with whatever you feel comfortable :)

let mut file = fd.into_file();
file.write_all(ECHO_SCRIPT.as_bytes()).unwrap();
file.flush().unwrap();

let raw_fd = file.as_raw_fd();

let p = cargo_test_support::project()
.file("echo.rs", ECHO_SCRIPT)
.build();

p.cargo(&format!("-Zscript -v /proc/self/fd/{raw_fd}"))
.masquerade_as_nightly_cargo(&["script"])
.with_stdout_data(str![[r#"
current_exe: [ROOT]/home/.cargo/build/[HASH]/target/debug/package
arg0: /proc/self/fd/[..]
args: []

"#]])
.with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to `2024`
[COMPILING] package v0.0.0 (/proc/self/fd/[..])
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `[ROOT]/home/.cargo/build/[HASH]/target/debug/package`

"#]])
.run();
}