rust-lang/cargo · error

was not filled at beginning of the function

Error message

was not filled at beginning of the function

What it means

This panic is at the end of GlobalContext::load_credentials(). The function guards re-entry by checking self.credential_values.filled() at the top (line 1785) and returning Ok(()) early if already loaded. At line 1822-1824 it calls self.credential_values.set(credential_values) and expects success. The OnceCell .set() fails only if already filled — so if something filled credential_values between the top check and this call, the expect panics.

Source

Thrown at src/context/mod.rs:1824

        let mut credential_values = HashMap::default();
        if let CV::Table(map, _) = value {
            let base_map = self.values()?;
            for (k, v) in map {
                let entry = match base_map.get(&k) {
                    Some(base_entry) => {
                        let mut entry = base_entry.clone();
                        entry.merge(v, true)?;
                        entry
                    }
                    None => v,
                };
                credential_values.insert(k, entry);
            }
        }
        self.credential_values
            .set(credential_values)
            .expect("was not filled at beginning of the function");
        Ok(())
    }

    /// Looks for a path for `tool` in an environment variable or the given config, and returns
    /// `None` if it's not present.
    fn maybe_get_tool(
        &self,
        tool: &str,
        from_config: &Option<ConfigRelativePath>,
    ) -> Option<PathBuf> {
        let var = tool.to_uppercase();

        match self.get_env_os(&var).as_ref().and_then(|s| s.to_str()) {
            Some(tool_path) => {
                let maybe_relative = tool_path.contains('/') || tool_path.contains('\\');
                let path = if maybe_relative {
                    self.cwd.join(tool_path)
                } else {

View on GitHub (pinned to 0e07a15537)

Solutions

  1. If embedding cargo as a library, call load_credentials() once at startup before spawning threads.
  2. Synchronize access to GlobalContext credential loading with a mutex or ensure single-threaded init.
  3. Verify the ~/.cargo/credentials file is not corrupted or concurrently modified.
Defensive patterns

Strategy: validation

Validate before calling

// Call load_credentials once at startup before any concurrent access
ctx.load_credentials()?;
// Subsequent calls are no-ops due to the filled() guard

Prevention

When it happens

Trigger: A re-entrant or concurrent call to load_credentials() that fills the OnceCell between the filled() check on line 1785 and the set() on line 1824. Since load_credentials takes &self (not &mut self), concurrent calls are possible if the OnceCell is not otherwise synchronized.

Common situations: Embedding cargo as a library and calling load_credentials() from multiple threads simultaneously; a cargo internal code path that triggers credential loading recursively; corrupted credentials file causing a partial load that fills the cell before the full load completes.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/69d97a4fbca5d11c.json. Report an issue: GitHub.