flipped-aurora/gin-vue-admin · error

no function encloses line %d in %s

Error message

no function encloses line %d in %s

What it means

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.

Source

Thrown at server/utils/ast/extract_func.go:48

    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
        }
        return true
    })

    if target == nil {
        err = fmt.Errorf("no function encloses line %d in %s", line, filePath)
        return
    }

    // 使用字节偏移精确提取源码片段(包含注释与原始格式)
    start := fset.Position(target.Pos()).Offset
    end := fset.Position(target.End()).Offset
    if start < 0 || end > len(src) || start >= end {
        err = fmt.Errorf("invalid offsets for function: start=%d end=%d len=%d", start, end, len(src))
        return
    }
    source = string(src[start:end])
    name = target.Name.Name
    return
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Print the file and confirm the line actually falls inside a func/method body or signature
  2. Re-read the file and recompute the line number against the current file contents
  3. Check for off-by-one: line numbers are 1-based token.FileSet positions
  4. 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)

Example fix

// before: line points at the import block
ast.ExtractFuncSourceByPosition("server/initialize/router.go", 5)
// after: line points inside the target function body
ast.ExtractFuncSourceByPosition("server/initialize/router.go", 42)
Defensive patterns

Strategy: validation

Validate before calling

// ensure the line falls inside some function before extraction
if line < 1 {
    return fmt.Errorf("line must be 1-based, got %d", line)
}
// optionally parse and check ranges yourself with ast.Inspect for FuncDecl line spans

Try / catch

name, src, _, _, err := ast.ExtractFuncSourceByPosition(filePath, line)
if err != nil && strings.Contains(err.Error(), "no function encloses line") {
    return nil // caller asked for a line outside any function: skip gracefully
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/dc435b8f91b57fb5. Report an issue: GitHub.