{"id":"6e4d49113e7faded","repo":"rust-lang/cargo","slug":"already-loaded-without-errors","errorCode":null,"errorMessage":"already loaded without errors","messagePattern":"already loaded without errors","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/bin/cargo/main.rs","lineNumber":34,"sourceCode":"\nfn main() {\n    let _guard = setup_logger();\n\n    let mut gctx = match GlobalContext::default() {\n        Ok(gctx) => gctx,\n        Err(e) => {\n            let mut shell = Shell::new();\n            cargo::exit_with_error(e.into(), &mut shell)\n        }\n    };\n\n    let nightly_features_allowed = matches!(&*features::channel(), \"nightly\" | \"dev\");\n    if nightly_features_allowed {\n        let _span = tracing::span!(tracing::Level::TRACE, \"completions\").entered();\n        let args = std::env::args_os();\n        let current_dir = std::env::current_dir().ok();\n        let completer = clap_complete::CompleteEnv::with_factory(|| {\n            let mut gctx = GlobalContext::default().expect(\"already loaded without errors\");\n            cli::cli(&mut gctx)\n        })\n        .var(\"CARGO_COMPLETE\");\n        if completer\n            .try_complete(args, current_dir.as_deref())\n            .unwrap_or_else(|e| {\n                let mut shell = Shell::new();\n                cargo::exit_with_error(e.into(), &mut shell)\n            })\n        {\n            return;\n        }\n    }\n\n    let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {\n        cargo::ops::fix_exec_rustc(&gctx, &lock_addr).map_err(|e| CliError::from(e))\n    } else {\n        let _token = cargo::util::job::setup();","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/bin/cargo/main.rs#L16-L52","documentation":"On the nightly-only shell-completion path in `cargo`'s `main`, `GlobalContext::default().expect(\"already loaded without errors\")` constructs the global config a second time inside the `clap_complete::CompleteEnv` factory. It already succeeded once at the top of `main`, so the second call is expected to succeed too. The panic indicates a transient or environment-driven failure on the reload — config files changed between the two calls, a HOME/CARGO_HOME race, or filesystem error.","triggerScenarios":"Triggering tab-completion (`CARGO_COMPLETE` env) on a nightly/dev Cargo while the config files (`~/.cargo/config.toml`, `$CARGO_HOME/config.toml`, or a credential file) are concurrently modified or become unreadable; HOME unset/unwritable mid-completion; permission flip on the config between the two `GlobalContext::default()` calls.","commonSituations":"Shell completion triggered while a package manager or editor rewrites `~/.cargo/config.toml`; running under a sandbox/CI that mutates `$HOME`; concurrent `cargo login`/`cargo logout` rewriting the credentials file.","solutions":["Re-trigger completion after the config file write finishes.","Ensure `HOME` and `CARGO_HOME` point to a stable, readable directory.","Check permissions on `~/.cargo/config.toml` and the credentials file; `chmod u+r` if needed.","If it reproduces, capture `RUST_LOG=cargo::util::context=trace` output and file a Cargo bug — the second load should arguably fall back rather than panic."],"exampleFix":"// before\nlet mut gctx = GlobalContext::default().expect(\"already loaded without errors\");\n// after (fail soft inside the non-critical completion path)\nlet mut gctx = match GlobalContext::default() {\n    Ok(g) => g,\n    Err(e) => {\n        tracing::warn!(\"completion config reload failed: {e}\");\n        return;\n    }\n};","handlingStrategy":"try-catch","validationCode":"// Validate that the cargo config is readable before relying on completion:\nuse std::process::Command;\nfn cargo_config_ok() -> bool {\n    Command::new(\"cargo\").arg(\"--version\").output().map(|o| o.status.success()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"// Wrap your cargo invocation; on panic/non-zero exit from completion, fall back to no completion\nlet out = std::process::Command::new(\"cargo\").args(args).status();\nmatch out {\n    Ok(s) if s.success() => {}\n    _ => { /* silently skip completion */ }\n}","preventionTips":["Keep `~/.cargo/config.toml` and the credentials file stable while shell completion runs.","Ensure `HOME`/`CARGO_HOME` point to a readable directory.","Don't run `cargo login`/`cargo logout` concurrently with shell completion."],"tags":["cargo","config","shell-completion","nightly","internal-invariant"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}