{"record":{"id":"f3c8fe0d43f30585","repo":"flipped-aurora/gin-vue-admin","slug":"parse-file-failed-w","errorCode":null,"errorMessage":"parse file failed: %w","messagePattern":"parse file failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/extract_func.go","lineNumber":25,"sourceCode":"    \"go/token\"\n    \"os\"\n)\n\n// ExtractFuncSourceByPosition 根据文件路径与行号，提取包含该行的整个方法源码\n// 返回：方法名、完整源码、起止行号\nfunc ExtractFuncSourceByPosition(filePath string, line int) (name string, source string, startLine int, endLine int, err error) {\n    // 读取源文件\n    src, readErr := os.ReadFile(filePath)\n    if readErr != nil {\n        err = fmt.Errorf(\"read file failed: %w\", readErr)\n        return\n    }\n\n    // 解析 AST\n    fset := token.NewFileSet()\n    file, parseErr := parser.ParseFile(fset, filePath, src, parser.ParseComments)\n    if parseErr != nil {\n        err = fmt.Errorf(\"parse file failed: %w\", parseErr)\n        return\n    }\n\n    // 在 AST 中定位包含指定行号的函数声明\n    var target *ast.FuncDecl\n    ast.Inspect(file, func(n ast.Node) bool {\n        fd, ok := n.(*ast.FuncDecl)\n        if !ok {\n            return true\n        }\n        s := fset.Position(fd.Pos()).Line\n        e := fset.Position(fd.End()).Line\n        if line >= s && line <= e {\n            target = fd\n            startLine = s\n            endLine = e\n            return false\n        }","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/extract_func.go#L7-L43","documentation":"ExtractFuncSourceByPosition reads a Go source file and parses it with go/parser to locate a function containing a given line. This error wraps any parse failure returned by parser.ParseFile. It means the target file is not syntactically valid Go, so no AST can be built and no function extraction is possible.","triggerScenarios":"Calling ExtractFuncSourceByPosition(filePath, line) where the file at filePath contains Go syntax errors (unbalanced braces, stray characters, truncated file) or is not Go source at all (e.g. wrong extension pointing at a config/JSON file).","commonSituations":"Extracting a function from a file mid-edit that was just saved in a broken state; pointing at a generated file that failed generation halfway; passing a non-Go file path; running the tool against a file written by another codegen step that emitted invalid syntax.","solutions":["Run 'gofmt -e <file>' or 'go build' on the file to see the underlying parse error reported in the wrapped %w cause and fix that syntax error first","Verify filePath points to a real Go .go source file, not a config/log/generated artifact","If the file is code-generated, re-run or fix the generator that produced the broken file","Retry ExtractFuncSourceByPosition after the file parses cleanly"],"exampleFix":"// before: passing a template placeholder that is not valid Go\nname, src, _, _, err := ast.ExtractFuncSourceByPosition(\"server/initialize/router.tpl\", 10)\n// after: parse a valid .go file\nname, src, _, _, err := ast.ExtractFuncSourceByPosition(\"server/initialize/router.go\", 10)","handlingStrategy":"try-catch","validationCode":"if _, err := os.Stat(filePath); err != nil { return err }\nif body, err := os.ReadFile(filePath); err != nil {\n    return err\n} else if _, parseErr := parser.ParseFile(token.NewFileSet(), filePath, body, parser.ParseComments); parseErr != nil {\n    return fmt.Errorf(\"file not valid Go: %w\", parseErr)\n}","typeGuard":null,"tryCatchPattern":"name, src, _, _, err := ast.ExtractFuncSourceByPosition(filePath, line)\nif err != nil {\n    var pe *scanner.ErrorList\n    if errors.As(err, &pe) {\n        // handle parse failure: log position, skip file\n        return fmt.Errorf(\"skip %s: %w\", filePath, err)\n    }\n    return err\n}","preventionTips":["Validate the file parses with gofmt -e before running extraction pipelines","Only pass .go files to ExtractFuncSourceByPosition","Skip and log files that fail to parse instead of aborting the whole codegen run"],"tags":["go","ast","parse-error","codegen"],"backgroundTag":"go-source-parse-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}