alibaba/open-code-review · error

load scan template: %w

Error message

load scan template: %w

What it means

`ocr scan` loads its embedded scan_template.json via template.LoadScanDefault() before starting a full-file scan. The template is compiled into the binary, so a failure here means the JSON could not be unmarshaled (fmt.Errorf("unmarshal default scan template: %w", err)). The CLI wraps this in "load scan template: %w" and aborts before any files are scanned.

Source

Thrown at cmd/opencodereview/scan_cmd.go:133

	}
	defer func() {
		if cerr := closeOut(); cerr != nil {
			retErr = errors.Join(retErr, fmt.Errorf("close output file: %w", cerr))
		}
	}()

	cc, err := loadCommonContext(opts.repoDir, opts.rulePath, "", opts.maxTools, opts.maxGitProcs, false)
	if err != nil {
		return err
	}
	applyCLIExcludes(cc, splitPaths(opts.excludes))

	// scan owns its own template (scan_template.json) independent from the
	// diff-review template loaded by loadCommonContext above. Apply --max-tools
	// as an "only raise" override to the scan template's per-file budget.
	scanTpl, err := template.LoadScanDefault()
	if err != nil {
		return fmt.Errorf("load scan template: %w", err)
	}
	if err := scanTpl.Validate(); err != nil {
		return fmt.Errorf("invalid scan template: %w", err)
	}
	if opts.maxTools > scanTpl.MaxToolRequestTimes {
		scanTpl.MaxToolRequestTimes = opts.maxTools
	}
	if opts.batch != "" {
		// CLI override of BATCH_STRATEGY; validated downstream by parseBatchStrategy
		// (unknown values silently fall back to "none").
		scanTpl.BatchStrategy = opts.batch
	}
	// Token budget: --max-tokens-budget overrides the template value when set.
	budget := scanTpl.MaxTokensBudget
	if opts.maxTokensBudget > 0 {
		budget = int64(opts.maxTokensBudget)
	}

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Reinstall or rebuild ocr from a clean checkout so the embedded scan_template.json is intact (go build ./cmd/opencodereview).
  2. Verify the binary is not truncated/corrupted (compare checksum with the official release).
  3. Check that internal/config/template/scan_template.json parses as valid JSON (go test ./internal/config/template/...) and report/fix if it doesn't.

Example fix

// before (broken embed in a forked checkout)
scan_template.json: "{ invalid json }"
// after
git checkout -- internal/config/template/scan_template.json && go build -o ocr ./cmd/opencodereview
Defensive patterns

Strategy: validation

Validate before calling

// Embedded template: validate the binary/checkout before running scans
if _, err := os.Stat("internal/config/template/scan_template.json"); err != nil {
    return fmt.Errorf("scan template missing from checkout: %w", err)
}
var tpl template.ScanTemplate
if err := json.Unmarshal(defaultScanTemplate, &tpl); err != nil {
    return fmt.Errorf("scan template not valid JSON: %w", err)
}

Type guard

func validScanTemplate(b []byte) bool {
    var tpl template.ScanTemplate
    return json.Unmarshal(b, &tpl) == nil
}

Try / catch

if err := executeScan(opts); err != nil {
    var inner error
    if errors.Unwrap(err) != nil && strings.Contains(err.Error(), "unmarshal default scan template") {
        inner = fmt.Errorf("binary artifact broken; reinstall ocr")
    }
    report(inner)
}

Prevention

When it happens

Trigger: Running `ocr scan` when internal/config/template's embedded scan_template.json cannot be parsed by json.Unmarshal — essentially only a build/embed regression in the binary itself, since the template ships inside the executable.

Common situations: A broken or self-built binary (wrong build tags stripping embed files, an edited scan_template.json with invalid JSON committed before release); running a corrupted or partially copied ocr executable.

Related errors


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