chenhg5/cc-connect · error

directory reference cannot carry a location

Error message

directory reference cannot carry a location

What it means

`buildReferenceViewRequest` rejects a reference that resolves to a directory but also specifies a line/window location (`ref.locationFormat != referenceLocationNone`), since a directory listing has no line-addressable content. The error prevents rendering a file-style view (head + context lines) for something that can only be listed.

Source

Thrown at core/reference_show.go:41

	referenceViewRange    referenceViewMode = "range"
	referenceViewDir      referenceViewMode = "dir"
)

type referenceViewRequest struct {
	Ref        *localReference
	Mode       referenceViewMode
	Window     int
	MaxLines   int
	MaxEntries int
}

func buildReferenceViewRequest(rawRef, workspaceDir string) (*referenceViewRequest, error) {
	ref, err := parseUserLocalReference(rawRef, workspaceDir)
	if err != nil {
		return nil, err
	}
	if ref.kind == referenceKindDir && ref.locationFormat != referenceLocationNone {
		return nil, fmt.Errorf("directory reference cannot carry a location")
	}
	req := &referenceViewRequest{
		Ref:        ref,
		Window:     defaultShowContextLines,
		MaxLines:   defaultShowMaxRange,
		MaxEntries: defaultShowMaxEntries,
	}
	switch {
	case ref.kind == referenceKindDir:
		req.Mode = referenceViewDir
	case ref.locationFormat == referenceLocationColonRange:
		req.Mode = referenceViewRange
	case ref.locationFormat != referenceLocationNone:
		req.Mode = referenceViewContext
	default:
		req.Mode = referenceViewFileHead
		req.MaxLines = defaultShowHeadLines
	}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Drop the location suffix when referencing a directory: use `/show src/` instead of `/show src/:10`.
  2. Check with os.Stat whether the target is a directory before appending a line suffix.
  3. If the intent is a specific file inside the directory, name the file: `/show src/main.go:10`.
  4. Fix any automation that unconditionally appends `:<line>` to reference strings.

Example fix

// before
req, err := buildReferenceViewRequest("src/:10", workspaceDir)

// after
if info, err := os.Stat(filepath.Join(workspaceDir, "src")); err == nil && info.IsDir() {
    req, err = buildReferenceViewRequest("src/", workspaceDir) // no :line
} else {
    req, err = buildReferenceViewRequest("src/:10", workspaceDir)
}
Defensive patterns

Strategy: validation

Validate before calling

func stripLocationIfDir(raw, workspaceDir string) string {
    base := raw
    if i := strings.LastIndex(base, ":"); i > 0 { base = base[:i] }
    if info, err := os.Stat(filepath.Join(workspaceDir, base)); err == nil && info.IsDir() {
        return base
    }
    return raw
}

Try / catch

req, err := buildReferenceViewRequest(rawRef, ws)
if err != nil && strings.Contains(err.Error(), "directory reference") {
    reply("directories cannot take :line — try /show " + strings.TrimSuffix(rawRef, ":"+linePart))
    return
}

Prevention

When it happens

Trigger: Calling buildReferenceViewRequest (via cmdShow or the anonymous message handler) with a directory path carrying a location suffix — e.g. `src/:10`, `src/utils:42-50`, or a markdown link to a directory with a line suffix.

Common situations: User types `/show src/` then drags/selects text adding `:12`; a card template appends the current line number to a directory reference; scripts that programmatically append `:N` to any path, including directories.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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