{"record":{"id":"2a347b8709b912f5","repo":"rust-lang/rust","slug":"failed-to-spawn-as","errorCode":null,"errorMessage":"Failed to spawn `as`.","messagePattern":"Failed to spawn `as`\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_codegen_cranelift/src/global_asm.rs","lineNumber":233,"sourceCode":") -> Result<(), String> {\n    assert!(!global_asm.is_empty());\n\n    // Remove all LLVM style comments\n    let mut global_asm = global_asm\n        .lines()\n        .map(|line| if let Some(index) = line.find(\"//\") { &line[0..index] } else { line })\n        .collect::<Vec<_>>()\n        .join(\"\\n\");\n    global_asm.push('\\n');\n\n    // Assemble `global_asm`\n    if option_env!(\"CG_CLIF_FORCE_GNU_AS\").is_some() {\n        let mut child = Command::new(&config.assembler)\n            .arg(\"-o\")\n            .arg(&global_asm_object_file)\n            .stdin(Stdio::piped())\n            .spawn()\n            .expect(\"Failed to spawn `as`.\");\n        child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();\n        let status = child.wait().expect(\"Failed to wait for `as`.\");\n        if !status.success() {\n            return Err(format!(\"Failed to assemble `{}`\", global_asm));\n        }\n    } else {\n        // Escape { and }\n        let global_asm = global_asm.replace('{', \"{{\").replace('}', \"}}\");\n\n        let mut child = Command::new(std::env::current_exe().unwrap())\n            // Avoid a warning about the jobserver fd not being passed\n            .env_remove(\"CARGO_MAKEFLAGS\")\n            .arg(\"--target\")\n            .arg(&config.target)\n            .arg(\"--crate-type\")\n            .arg(\"staticlib\")\n            .arg(\"--emit\")\n            .arg(\"obj\")","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_codegen_cranelift/src/global_asm.rs#L215-L251","documentation":"This fires in Cranelift codegen's `compile_global_asm` when the environment variable `CG_CLIF_FORCE_GNU_AS` is set. The code spawns the system assembler (`as`, path from `config.assembler`) via `Command::new(&config.assembler).spawn().expect(...)`. If the assembler binary cannot be found in PATH, lacks execute permission, or the OS refuses to spawn it (resource limits, fork failure), the panic fires.","triggerScenarios":"Compiling a crate that uses `global_asm!` with the Cranelift backend while `CG_CLIF_FORCE_GNU_AS` is set, and the system assembler (`as`) is not installed or not in PATH. Common on minimal Docker containers, Alpine Linux (uses `as` from binutils), or cross-compilation setups without the target's assembler.","commonSituations":"Minimal Docker images (e.g., `debian:slim`, Alpine) without `binutils` installed. Cross-compilation where the target's `as` isn't in PATH. Systems where `as` exists but is not executable or the process table is full. Setting `CG_CLIF_FORCE_GNU_AS` to bypass the default LLVM-via-rustc asm path.","solutions":["Install binutils: `apt-get install binutils` (Debian/Ubuntu) or `apk add binutils` (Alpine).","Remove the `CG_CLIF_FORCE_GNU_AS` environment variable — without it, cg_clif uses rustc+LLVM to assemble global asm instead of spawning `as` directly.","Verify the assembler is in PATH: `which as`.","If cross-compiling, install the cross-assembler (e.g., `gcc-multilib` or the target's binutils)."],"exampleFix":"# before (CG_CLIF_FORCE_GNU_AS set but `as` missing → panic)\nCG_CLIF_FORCE_GNU_AS=1 cargo build\n\n# after (install binutils, or remove the env var)\n# Option A: install binutils\napt-get install -y binutils\n# Option B: unset the flag\nunset CG_CLIF_FORCE_GNU_AS\ncargo build","handlingStrategy":"validation","validationCode":"// Before building with global_asm! + CG_CLIF_FORCE_GNU_AS, verify\n// the assembler exists:\n// which as || { echo 'as not found; install binutils'; exit 1; }\n// Rust-side check in build.rs:\n// fn main() {\n//     if option_env!(\"CG_CLIF_FORCE_GNU_AS\").is_some() {\n//         let out = std::process::Command::new(\"which\").arg(\"as\").output();\n//         if !out.map(|o| o.status.success()).unwrap_or(false) {\n//             panic!(\"CG_CLIF_FORCE_GNU_AS set but `as` not found. Install binutils.\");\n//         }\n//     }\n// }","typeGuard":null,"tryCatchPattern":"// In a build script, handle assembler spawn failure gracefully:\nuse std::process::Command;\nlet result = Command::new(&assembler).arg(\"-o\").arg(&outfile).spawn();\nmatch result {\n    Ok(mut child) => { child.wait().expect(\"wait failed\"); }\n    Err(e) => {\n        eprintln!(\"Failed to spawn assembler: {}. Install binutils or unset CG_CLIF_FORCE_GNU_AS.\", e);\n        std::process::exit(1);\n    }\n}","preventionTips":["Always have binutils installed when using CG_CLIF_FORCE_GNU_AS.","Verify 'which as' succeeds before building.","In Dockerfiles, add binutils to the install list.","Don't set CG_CLIF_FORCE_GNU_AS unless you specifically need GNU as over the LLVM path."],"tags":["rustc","cranelift","codegen","global-asm","assembler","environment","binutils","cg-clif"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}