alibaba/open-code-review · error

file %q not found: %w

Error message

file %q not found: %w

What it means

Wrapper error from FileReadProvider.Execute when FileReader.ReadLines fails for any reason (I/O error, path outside the repository, missing file, permission error). Note the wrapper unconditionally labels the cause 'not found', even when the underlying error is something else (e.g. 'outside repository' or a permissions failure), so always inspect the wrapped %w cause.

Source

Thrown at internal/tool/file_read.go:51

	}
	if !hasEnd || endLine <= 0 {
		endLine = 0
	}

	maxLines := fileReadMaxLines
	if endLine > 0 {
		requested := int(endLine) - int(startLine) + 1
		if requested <= 0 {
			return "", fmt.Errorf("invalid line range: start_line %d is greater than end_line %d", int(startLine), int(endLine))
		}
		if requested < maxLines {
			maxLines = requested
		}
	}

	lines, totalLines, err := p.FileReader.ReadLines(ctx, filePath, int(startLine), maxLines)
	if err != nil {
		return "", fmt.Errorf("file %q not found: %w", filePath, err)
	}

	if totalLines > 0 && int(startLine)-1 >= totalLines {
		return "", fmt.Errorf("file %q has only %d lines, requested range %d-%d", filePath, totalLines, int(startLine), int(endLine))
	}

	effectiveEnd := totalLines
	if endLine > 0 && int(endLine) < effectiveEnd {
		effectiveEnd = int(endLine)
	}
	fullRange := effectiveEnd - (int(startLine) - 1)
	truncated := fullRange > fileReadMaxLines

	displayEnd := int(startLine) - 1 + len(lines)

	var sb strings.Builder
	sb.WriteString(fmt.Sprintf("File: %s (Total lines: %d)\n", filePath, totalLines))
	sb.WriteString(fmt.Sprintf("IS_TRUNCATED: %t\n", truncated))

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Check the exact path exists relative to the repository root (ls) and fix typos.
  2. Inspect the wrapped cause after 'not found:' — if it says 'outside repository', use a repo-relative path instead of an absolute one.
  3. Verify file permissions, and for range/commit modes confirm the file exists at the given ref (git show <ref>:<path>).

Example fix

// before: absolute path rejected/wrapped as not found
{"file_path": "/etc/hosts"}
// after: repository-relative path
{"file_path": "internal/tool/file_read.go"}
Defensive patterns

Strategy: validation

Validate before calling

rel, err := filepath.Rel(repoRoot, filePath)
if err != nil || strings.HasPrefix(rel, "..") { return fmt.Errorf("path %q is not inside the repo", filePath) }
if _, err := os.Stat(filepath.Join(repoRoot, rel)); err != nil { return fmt.Errorf("file %q does not exist", rel) }

Type guard

func insideRepo(repoRoot, p string) bool { rel, err := filepath.Rel(repoRoot, p); return err == nil && !strings.HasPrefix(rel, "..") }

Try / catch

out, err := provider.Execute(ctx, args)
if err != nil && strings.Contains(err.Error(), "not found") {
    // inspect wrapped cause; fall back to git-show read at HEAD
    args["via_ref"] = "HEAD"
    out, err = provider.Execute(ctx, args)
}

Prevention

When it happens

Trigger: Executing file_read with a file_path that does not exist, is a directory, is unreadable, lies outside the repo root, or whose symlink target cannot be resolved — i.e. any error propagated from ReadLines/readFromDisk/readLinesFromDisk.

Common situations: Agent hallucinating a file path; typo in the path; file deleted or renamed between listing and reading; trying to read an absolute path or ../ outside the repository; reading a file that only exists at a git ref in range/commit mode while the disk copy is absent.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/d77db7e8cbc2b37b. Report an issue: GitHub.