Skip to content

cargo -Zscript doesn't work for memfd created files anymore #16110

@crumblingstatue

Description

@crumblingstatue

When the script file is created through memfd_create, cargo panics when trying to run the script.

Repro:

use std::os::fd::FromRawFd;
use std::io::Write;
use std::os::unix::process::CommandExt;

unsafe extern "C" {
    fn memfd_create(name: *const std::ffi::c_char, flags: std::ffi::c_uint) -> std::ffi::c_int;
}

fn main() {
    let fd = unsafe { memfd_create(c"otkeep-script".as_ptr(), 0) };
    if fd == -1 {
        panic!("memfd_create failed when trying to create script file");
    }
    let mut f = unsafe { std::fs::File::from_raw_fd(fd) };
    f.write_all(SCRIPT.as_bytes()).unwrap();
    f.flush().unwrap();
    dbg!(std::process::Command::new(format!("/proc/self/fd/{fd}")).exec());
}

const SCRIPT: &str = r#"#!/usr/bin/env -S cargo +nightly -Zscript

fn main() {
    println!("Hello World");
}
"#;

Cargo output (as of 1.92.0-nightly (81c3f77a4 2025-10-10)):

thread 'main' (26272) panicked at src/tools/cargo/src/cargo/core/workspace.rs:454:18:
template is correct: No such file or directory (os error 2)

My use case is that I'm running cargo scripts through my script runner called OtKeep, which stores scripts in a database and uses memfd_create to create temporary files when executing the scripts.

This used to work, but doesn't anymore. The regression seems to be caused by #16073.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugS-needs-team-inputStatus: Needs input from team on whether/how to proceed.Z-scriptNightly: cargo script

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions