ahmetb/kubectx · error

file %d lookup: %w

Error message

file %d lookup: %w

What it means

contextNodeWithFileIndex wraps an error from contexts.Pipe(yaml.Lookup("[name=" + name + "]")) as "file %d lookup: %w". The YAML field lookup for the context name fails structurally in file i — not 'not found', but an error evaluating the lookup expression over the contexts node.

Source

Thrown at internal/kubeconfig/contexts.go:52

	}
	return contexts, nil
}

// contextNodeWithFileIndex searches for a context by name across all files.
// Returns the context node and the index of the file that contains it.
// Files without a valid "contexts" sequence are skipped, but if errors occur
// during lookup they are included in the final error message.
func (k *Kubeconfig) contextNodeWithFileIndex(name string) (*yaml.RNode, int, error) {
	var fileErrors []error
	for i := range k.files {
		contexts, err := contextsNodeOf(&k.files[i])
		if err != nil {
			fileErrors = append(fileErrors, fmt.Errorf("file %d: %w", i, err))
			continue
		}
		context, err := contexts.Pipe(yaml.Lookup("[name=" + name + "]"))
		if err != nil {
			fileErrors = append(fileErrors, fmt.Errorf("file %d lookup: %w", i, err))
			continue
		}
		if context != nil {
			return context, i, nil
		}
	}
	if len(fileErrors) > 0 {
		return nil, -1, fmt.Errorf("context with name %q not found (errors in files: %w)",
			name, errors.Join(fileErrors...))
	}
	return nil, -1, fmt.Errorf("context with name %q not found", name)
}

func (k *Kubeconfig) contextNode(name string) (*yaml.RNode, error) {
	node, _, err := k.contextNodeWithFileIndex(name)
	return node, err
}

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Open the kubeconfig file at the reported index and inspect each entry under 'contexts' for a proper 'name' field.
  2. Fix or remove malformed context entries so each is a mapping with name/cluster/user.
  3. Validate with kubectl config view --kubeconfig <file> until it parses cleanly.
  4. Re-merge multi-file KUBECONFIG inputs with a tool that produces valid entries.

Example fix

// before (invalid entry breaks lookup)
contexts:
  - just-a-string
// after
contexts:
  - name: my-ctx
    context:
      cluster: my-cluster
      user: my-user
Defensive patterns

Strategy: validation

Validate before calling

// Go: verify context entries are well-formed maps before lookup operations
type contextEntry struct {
	Name    string `yaml:"name"`
	Context struct {
		Cluster string `yaml:"cluster"`
		User    string `yaml:"user"`
	} `yaml:"context"`
}
// each item in 'contexts' must unmarshal into this shape before Pipe(Lookup) can succeed

Try / catch

if err := kc.ModifyContextName(old, new); err != nil {
	if strings.Contains(err.Error(), "lookup") {
		fmt.Fprintln(os.Stderr, "A contexts entry is malformed; ensure every context is a mapping with a 'name' field.")
	}
	return err
}

Prevention

When it happens

Trigger: During contextNodeWithFileIndex iteration over k.files, Pipe(yaml.Lookup(...)) returns an error for file i, usually when the contexts node items are not the expected mapping shape (e.g. a context entry is not a map, or the list contains scalars), making the name-field lookup invalid.

Common situations: A contexts list containing malformed entries (missing name field, wrong types) after manual editing or a bad merge; a file where 'contexts' is a map instead of a list.

Related errors


AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02). Data as JSON: /api/errors/d739062662724d89. Report an issue: GitHub.