{"record":{"id":"efee6b62769650b9","repo":"alibaba/open-code-review","slug":"load-scan-template-w","errorCode":null,"errorMessage":"load scan template: %w","messagePattern":"load scan template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/scan_cmd.go","lineNumber":133,"sourceCode":"\t}\n\tdefer func() {\n\t\tif cerr := closeOut(); cerr != nil {\n\t\t\tretErr = errors.Join(retErr, fmt.Errorf(\"close output file: %w\", cerr))\n\t\t}\n\t}()\n\n\tcc, err := loadCommonContext(opts.repoDir, opts.rulePath, \"\", opts.maxTools, opts.maxGitProcs, false)\n\tif err != nil {\n\t\treturn err\n\t}\n\tapplyCLIExcludes(cc, splitPaths(opts.excludes))\n\n\t// scan owns its own template (scan_template.json) independent from the\n\t// diff-review template loaded by loadCommonContext above. Apply --max-tools\n\t// as an \"only raise\" override to the scan template's per-file budget.\n\tscanTpl, err := template.LoadScanDefault()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"load scan template: %w\", err)\n\t}\n\tif err := scanTpl.Validate(); err != nil {\n\t\treturn fmt.Errorf(\"invalid scan template: %w\", err)\n\t}\n\tif opts.maxTools > scanTpl.MaxToolRequestTimes {\n\t\tscanTpl.MaxToolRequestTimes = opts.maxTools\n\t}\n\tif opts.batch != \"\" {\n\t\t// CLI override of BATCH_STRATEGY; validated downstream by parseBatchStrategy\n\t\t// (unknown values silently fall back to \"none\").\n\t\tscanTpl.BatchStrategy = opts.batch\n\t}\n\t// Token budget: --max-tokens-budget overrides the template value when set.\n\tbudget := scanTpl.MaxTokensBudget\n\tif opts.maxTokensBudget > 0 {\n\t\tbudget = int64(opts.maxTokensBudget)\n\t}\n","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/scan_cmd.go#L115-L151","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reinstall or rebuild ocr from a clean checkout so the embedded scan_template.json is intact (go build ./cmd/opencodereview).","Verify the binary is not truncated/corrupted (compare checksum with the official release).","Check that internal/config/template/scan_template.json parses as valid JSON (go test ./internal/config/template/...) and report/fix if it doesn't."],"exampleFix":"// before (broken embed in a forked checkout)\nscan_template.json: \"{ invalid json }\"\n// after\ngit checkout -- internal/config/template/scan_template.json && go build -o ocr ./cmd/opencodereview","handlingStrategy":"validation","validationCode":"// Embedded template: validate the binary/checkout before running scans\nif _, err := os.Stat(\"internal/config/template/scan_template.json\"); err != nil {\n    return fmt.Errorf(\"scan template missing from checkout: %w\", err)\n}\nvar tpl template.ScanTemplate\nif err := json.Unmarshal(defaultScanTemplate, &tpl); err != nil {\n    return fmt.Errorf(\"scan template not valid JSON: %w\", err)\n}","typeGuard":"func validScanTemplate(b []byte) bool {\n    var tpl template.ScanTemplate\n    return json.Unmarshal(b, &tpl) == nil\n}","tryCatchPattern":"if err := executeScan(opts); err != nil {\n    var inner error\n    if errors.Unwrap(err) != nil && strings.Contains(err.Error(), \"unmarshal default scan template\") {\n        inner = fmt.Errorf(\"binary artifact broken; reinstall ocr\")\n    }\n    report(inner)\n}","preventionTips":["Never hand-edit scan_template.json in release branches; validate JSON in CI.","Verify release binaries with checksums after download.","Add a smoke test that calls template.LoadScanDefault() in package tests."],"tags":["go","cli","template","config"],"backgroundTag":"embedded-template-load-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}