chenhg5/cc-connect · error

project %q located in parsed config but not raw file

Error message

project %q located in parsed config but not raw file

What it means

This is an internal consistency check: the project was found in the parsed TOML (projectIdx >= 0) but buildRawProjectSpans did not produce a raw-text span at that index. Since the surgical editor needs both views to agree, it fails rather than corrupting the file.

Source

Thrown at config/config.go:1240

	if err := toml.Unmarshal(data, cfg); err != nil {
		return fmt.Errorf("parse config: %w", err)
	}

	projectIdx := -1
	for i := range cfg.Projects {
		if cfg.Projects[i].Name == projectName {
			projectIdx = i
			break
		}
	}
	if projectIdx < 0 {
		return fmt.Errorf("project %q not found in config", projectName)
	}

	lines, hadTrailing := splitConfigLines(raw)
	spans := buildRawProjectSpans(lines)
	if projectIdx >= len(spans) {
		return fmt.Errorf("project %q located in parsed config but not raw file", projectName)
	}
	projSpan := spans[projectIdx]

	for j, prov := range cfg.Projects[projectIdx].Agent.Providers {
		if prov.Name == providerName {
			if j < len(projSpan.agentProviders) {
				ps := projSpan.agentProviders[j]
				lines = upsertTomlStringKey(lines, ps.start+1, ps.end, "model", model)
				return writeRawConfig(joinConfigLines(lines, hadTrailing))
			}
			break
		}
	}

	for _, ref := range cfg.Projects[projectIdx].Agent.ProviderRefs {
		if ref == providerName {
			return patchGlobalProviderField(lines, hadTrailing, cfg, providerName, "model", model)
		}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Report/inspect as a bug: dump the config and the wrapped error together
  2. Simplify the [[projects]] section formatting (one standard block per project) and retry
  3. Re-read the file — if it changed concurrently, retry the save once the writer settles
  4. Upgrade to a version where the raw-span parser matches the TOML parser
Defensive patterns

Strategy: fallback

Validate before calling

lines, _ := splitConfigLines(raw)
if projectIdx >= len(buildRawProjectSpans(lines)) {
	return errors.New("raw/parsed mismatch; reformat [[projects]]")
}

Try / catch

if err := config.SaveProviderModel(p, prov, model); err != nil {
	if strings.Contains(err.Error(), "but not raw file") {
		// fall back to full rewrite or ask user to reformat config
	}
	return err
}

Prevention

When it happens

Trigger: projectIdx >= len(spans) after buildRawProjectSpans(lines) — a mismatch between the parsed [[projects]] tables and the raw-line scanning, e.g. unusual table nesting, generated/duplicated headers, or a parser bug.

Common situations: Configs with non-standard [[projects]] placement or comments/formatting that confuse the raw span builder; concurrent edits rewrote the file between read and span build; a version skew where the raw-span parser lags the TOML schema.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/f63df5e4b43644c2. Report an issue: GitHub.