{"record":{"id":"6dcb2b5b098d49f5","repo":"sgl-project/sglang","slug":"failed-to-build-crate-python-module-with-cargo","errorCode":null,"errorMessage":"failed to build {crate.python_module} with Cargo","messagePattern":"failed to build (.+?) with Cargo","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/rust_extensions/loader.py","lineNumber":350,"sourceCode":"    command = [\n        \"cargo\",\n        \"build\",\n        \"--release\",\n        \"--locked\",\n        \"--package\",\n        crate.package,\n    ]\n    if crate.features:\n        command.extend((\"--features\", \",\".join(crate.features)))\n\n    environment = os.environ.copy()\n    environment[\"CARGO_TARGET_DIR\"] = os.fspath(target_dir)\n    environment[\"PYO3_PYTHON\"] = sys.executable\n    logger.info(\"Building %s with `%s`\", crate.python_module, \" \".join(command))\n    try:\n        subprocess.run(command, cwd=crate.workspace, env=environment, check=True)\n    except (OSError, subprocess.CalledProcessError) as exc:\n        raise RuntimeError(f\"failed to build {crate.python_module} with Cargo\") from exc\n\n    release_dir = target_dir / \"release\"\n    if target := environment.get(\"CARGO_BUILD_TARGET\"):\n        release_dir = target_dir / target / \"release\"\n    artifact = release_dir / _cargo_library_filename(crate.library)\n    if not artifact.is_file():\n        raise FileNotFoundError(\n            f\"Cargo completed but did not produce the expected artifact {artifact}\"\n        )\n    return artifact\n\n\ndef _cargo_library_filename(library: str) -> str:\n    if sys.platform == \"win32\":\n        return f\"{library}.dll\"\n    if sys.platform == \"darwin\":\n        return f\"lib{library}.dylib\"\n    return f\"lib{library}.so\"","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/rust_extensions/loader.py#L332-L368","documentation":"`cargo build` failed or could not be spawned while building the crate; the original OSError/CalledProcessError is chained. The Cargo stderr appears in the traceback's __cause__.","triggerScenarios":"load_rust_extension triggering a source build where cargo exits non-zero (compile error, dependency fetch failure) or cannot execute (missing binary).","commonSituations":"Rust compile errors in the crate; offline environment preventing dependency downloads; cargo absent from PATH; incompatible rustc version vs pyo3.","solutions":["Read the chained CalledProcessError/underlying cargo output for the compile error and fix it","Run `cargo build --locked` manually in the workspace to reproduce","Ensure network access for crates.io (or a vendored/offline mirror) and a compatible Rust version","Use the prebuilt wheel to avoid compiling"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"subprocess.run([\"cargo\", \"build\", \"--locked\"], cwd=workspace, check=True)  # preflight the build","typeGuard":null,"tryCatchPattern":"try:\n    load_rust_extension(...)\nexcept RuntimeError as e:\n    cause = e.__cause__\n    log_cargo_output(cause)\n    raise","preventionTips":["Build once in Docker/CI with network access; cache artifacts","Pin a Rust toolchain version compatible with pyo3"],"tags":["rust","cargo","build-failure","compile"],"backgroundTag":"native-build-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}