flipped-aurora/gin-vue-admin · error
parse file failed: %w
Error message
parse file failed: %w
What it means
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.
Source
Thrown at server/utils/ast/extract_func.go:25
"go/token"
"os"
)
// ExtractFuncSourceByPosition 根据文件路径与行号,提取包含该行的整个方法源码
// 返回:方法名、完整源码、起止行号
func ExtractFuncSourceByPosition(filePath string, line int) (name string, source string, startLine int, endLine int, err error) {
// 读取源文件
src, readErr := os.ReadFile(filePath)
if readErr != nil {
err = fmt.Errorf("read file failed: %w", readErr)
return
}
// 解析 AST
fset := token.NewFileSet()
file, parseErr := parser.ParseFile(fset, filePath, src, parser.ParseComments)
if parseErr != nil {
err = fmt.Errorf("parse file failed: %w", parseErr)
return
}
// 在 AST 中定位包含指定行号的函数声明
var target *ast.FuncDecl
ast.Inspect(file, func(n ast.Node) bool {
fd, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
s := fset.Position(fd.Pos()).Line
e := fset.Position(fd.End()).Line
if line >= s && line <= e {
target = fd
startLine = s
endLine = e
return false
}View on GitHub (pinned to 3136500ef3)
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
Example fix
// before: passing a template placeholder that is not valid Go
name, src, _, _, err := ast.ExtractFuncSourceByPosition("server/initialize/router.tpl", 10)
// after: parse a valid .go file
name, src, _, _, err := ast.ExtractFuncSourceByPosition("server/initialize/router.go", 10) Defensive patterns
Strategy: try-catch
Validate before calling
if _, err := os.Stat(filePath); err != nil { return err }
if body, err := os.ReadFile(filePath); err != nil {
return err
} else if _, parseErr := parser.ParseFile(token.NewFileSet(), filePath, body, parser.ParseComments); parseErr != nil {
return fmt.Errorf("file not valid Go: %w", parseErr)
} Try / catch
name, src, _, _, err := ast.ExtractFuncSourceByPosition(filePath, line)
if err != nil {
var pe *scanner.ErrorList
if errors.As(err, &pe) {
// handle parse failure: log position, skip file
return fmt.Errorf("skip %s: %w", filePath, err)
}
return err
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- no function encloses line %d in %s
- invalid offsets for function: start=%d end=%d len=%d
- gorm 注入目标不能为空
- gorm 注入目标缺少 bizModel 函数
- bizModel 中未找到 %s.AutoMigrate 调用
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/f3c8fe0d43f30585.
Report an issue: GitHub.