rust-lang/rust-analyzer · error

we never provide completions for excluded files

Error message

we never provide completions for excluded files

What it means

handle_completion_resolve looks up the FileId for the position stored in the completion item's resolve data. rust-analyzer only advertises completions for files it has indexed (not excluded by client filters), so from_proto::file_id returning None violates that invariant and the expect panics. It is a "should never happen" assertion guarding the resolve-data contract.

Source

Thrown at crates/rust-analyzer/src/handlers/request.rs:1224

    mut original_completion: CompletionItem,
) -> anyhow::Result<CompletionItem> {
    let _p = tracing::info_span!("handle_completion_resolve").entered();

    if !all_edits_are_disjoint(&original_completion, &[]) {
        return Err(invalid_params_error(
            "Received a completion with overlapping edits, this is not LSP-compliant".to_owned(),
        )
        .into());
    }

    let Some(data) = original_completion.data.take() else {
        return Ok(original_completion);
    };

    let resolve_data: lsp_ext::CompletionResolveData = serde_json::from_value(data)?;

    let file_id = from_proto::file_id(&snap, &resolve_data.position.text_document.uri)?
        .expect("we never provide completions for excluded files");
    let line_index = snap.file_line_index(file_id)?;
    // FIXME: We should fix up the position when retrying the cancelled request instead
    let Ok(offset) = from_proto::offset(&line_index, resolve_data.position.position) else {
        return Ok(original_completion);
    };
    let source_root = snap.analysis.source_root_id(file_id)?;

    let mut forced_resolve_completions_config =
        snap.config.completion(Some(source_root), snap.minicore());
    forced_resolve_completions_config.fields_to_resolve = CompletionFieldsToResolve::empty();

    let position = FilePosition { file_id, offset };
    let Some(completions) = snap.analysis.completions(
        &forced_resolve_completions_config,
        position,
        resolve_data.trigger_character,
    )?
    else {

View on GitHub (pinned to e8f7e90aa3)

Solutions

  1. Re-trigger completion inside the currently open, non-excluded file so resolve data references a live FileId.
  2. Check client/workspace settings that exclude files (files.exclude, watcher globs) for the document you are completing in.
  3. Reopen the file with textDocument/didOpen so rust-analyzer registers it, then resolve the completion again.
  4. Update rust-analyzer/client; report if resolve data references excluded files in a stock setup.
Defensive patterns

Strategy: validation

Validate before calling

// client: only resolve completion items obtained from a live, non-excluded rust-analyzer document
const uri = item.data?.position?.textDocument?.uri;
const doc = workspace.textDocuments.find(d => d.uri.toString() === uri);
if (!doc || isExcluded(doc.uri)) { return item; } // skip resolve

Type guard

function hasValidResolveData(item: unknown): item is { data: { position: { textDocument: { uri: string } } } } {
  return typeof item === 'object' && item !== null &&
    'data' in item && (item as any)?.data?.position?.textDocument?.uri !== undefined;
}

Try / catch

try {
  return await client.sendRequest('completionItem/resolve', item);
} catch (e) {
  console.warn('completion resolve failed; returning original item', e);
  return item; // graceful fallback to the unresolved item
}

Prevention

When it happens

Trigger: A completion item whose data.position.textDocument.uri points to a file that is excluded/unregistered (e.g. excluded via textDocument/didOpen filters, file watcher exclusions, or a non-file URI scheme) is resolved by the client, or the URI fails conversion to an internal FileId.

Common situations: Clients that cache and replay completion items after the file was closed or removed from the workspace, virtual/untitled documents, files matched by negative glob patterns in client config, and stale items from a previous session.

Related errors


AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03). Data as JSON: /api/errors/e97bab34eb89407b. Report an issue: GitHub.