{"record":{"id":"dc435b8f91b57fb5","repo":"flipped-aurora/gin-vue-admin","slug":"no-function-encloses-line-d-in-s","errorCode":null,"errorMessage":"no function encloses line %d in %s","messagePattern":"no function encloses line (.+?) in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/extract_func.go","lineNumber":48,"sourceCode":"    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        }\n        return true\n    })\n\n    if target == nil {\n        err = fmt.Errorf(\"no function encloses line %d in %s\", line, filePath)\n        return\n    }\n\n    // 使用字节偏移精确提取源码片段（包含注释与原始格式）\n    start := fset.Position(target.Pos()).Offset\n    end := fset.Position(target.End()).Offset\n    if start < 0 || end > len(src) || start >= end {\n        err = fmt.Errorf(\"invalid offsets for function: start=%d end=%d len=%d\", start, end, len(src))\n        return\n    }\n    source = string(src[start:end])\n    name = target.Name.Name\n    return\n}","sourceCodeStart":30,"sourceCodeEnd":62,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/extract_func.go#L30-L62","documentation":"After parsing the file, ExtractFuncSourceByPosition walks all ast.FuncDecl nodes looking for a declaration whose line range encloses the requested line. This error is returned when no function declaration spans that line. It is a lookup/positioning failure, not a parse failure.","triggerScenarios":"Calling ExtractFuncSourceByPosition(filePath, line) with a line number that falls outside every FuncDecl: top-of-file imports/consts, blank lines between functions, a line past EOF, or a line inside a var/struct block rather than a function.","commonSituations":"Caller computed the line from a stale snapshot while the file changed since; passing line=1 expecting the first function (but line 1 is usually 'package x'); off-by-one from 0-based vs 1-based line numbers; targeting a method declared in a different file.","solutions":["Print the file and confirm the line actually falls inside a func/method body or signature","Re-read the file and recompute the line number against the current file contents","Check for off-by-one: line numbers are 1-based token.FileSet positions","If the target is a top-level func at package scope, the line must be at or inside its declaration (from 'func' keyword through closing brace)"],"exampleFix":"// before: line points at the import block\nast.ExtractFuncSourceByPosition(\"server/initialize/router.go\", 5)\n// after: line points inside the target function body\nast.ExtractFuncSourceByPosition(\"server/initialize/router.go\", 42)","handlingStrategy":"validation","validationCode":"// ensure the line falls inside some function before extraction\nif line < 1 {\n    return fmt.Errorf(\"line must be 1-based, got %d\", line)\n}\n// optionally parse and check ranges yourself with ast.Inspect for FuncDecl line spans","typeGuard":null,"tryCatchPattern":"name, src, _, _, err := ast.ExtractFuncSourceByPosition(filePath, line)\nif err != nil && strings.Contains(err.Error(), \"no function encloses line\") {\n    return nil // caller asked for a line outside any function: skip gracefully\n}","preventionTips":["Always use 1-based line numbers matching token.FileSet positions","Recompute line numbers from the current file content, not a stale snapshot","Only request lines inside function bodies or signatures, not imports/consts/structs"],"tags":["go","ast","position-lookup","codegen"],"backgroundTag":"no-function-at-position","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}