alibaba/open-code-review · error

file path %q must be relative, not absolute

Error message

file path %q must be relative, not absolute

What it means

readWorkspaceFileForDiff rejects the relative path passed for an untracked/workspace file because it is absolute. The diff pipeline always supplies repo-relative paths; an absolute path here would bypass the repo-confinement checks that follow, so it is treated as invalid input.

Source

Thrown at internal/diff/workspace_file.go:20

// Copyright 2026 alibaba/open-code-review Contributors

package diff

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/alibaba/open-code-review/internal/pathutil"
)

func readWorkspaceFileForDiff(repoDir, relPath string) ([]byte, error) {
	repoRoot, err := pathutil.CanonicalPath(repoDir)
	if err != nil {
		return nil, fmt.Errorf("resolve repository path %q: %w", repoDir, err)
	}
	if filepath.IsAbs(relPath) {
		return nil, fmt.Errorf("file path %q must be relative, not absolute", relPath)
	}

	fullPath := filepath.Join(repoRoot, relPath)
	if !pathutil.WithinBase(repoRoot, fullPath) {
		return nil, fmt.Errorf("file path %q is outside repository", relPath)
	}

	parent, err := filepath.EvalSymlinks(filepath.Dir(fullPath))
	if err != nil {
		return nil, fmt.Errorf("resolve parent path for %q: %w", relPath, err)
	}
	if !pathutil.WithinBase(repoRoot, parent) {
		return nil, fmt.Errorf("file path %q is outside repository", relPath)
	}

	info, err := os.Lstat(fullPath)
	if err != nil {
		return nil, fmt.Errorf("stat file %q: %w", relPath, err)

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Pass the path relative to the repository root (e.g. src/foo.go, not /home/user/repo/src/foo.go)
  2. Strip any repository-root prefix before calling the workspace diff reader
  3. Workspace files are resolved as filepath.Join(repoRoot, relPath); supply relPath accordingly
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/diff/workspace_file.go:20 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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