flipped-aurora/gin-vue-admin · error

invalid offsets for function: start=%d end=%d len=%d

Error message

invalid offsets for function: start=%d end=%d len=%d

What it means

After locating the enclosing FuncDecl, the extractor converts its Pos/End to byte offsets and sanity-checks them against the source buffer length before slicing. This defensive error fires when the computed offsets are inconsistent with the source bytes (start<0, end>len(src), or start>=end), which should be nearly impossible for a freshly parsed file.

Source

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

        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. Re-read the file and call ExtractFuncSourceByPosition again on a fresh read/parse (do not reuse a stale src buffer)
  2. Check that the file is not being concurrently rewritten while extracting
  3. Ensure the file has no encoding oddities (BOM, NUL bytes) that could desync offsets
  4. If it persists, report with the printed start/end/len values from the error message

Example fix

// before: reusing src from an earlier read while file changed on disk
src, _ := os.ReadFile(path)
// ...file rewritten by generator...
name, out, _, _, err := ast.ExtractFuncSourceByPosition(path, line)
// after: single call re-reads and parses atomically inside the function
name, out, _, _, err := ast.ExtractFuncSourceByPosition(path, line)
Defensive patterns

Strategy: retry

Validate before calling

if fi, err := os.Stat(filePath); err == nil && !fi.Mode().IsRegular() {
    return fmt.Errorf("%s is not a regular file", filePath)
}

Try / catch

name, src, _, _, err := ast.ExtractFuncSourceByPosition(filePath, line)
if err != nil && strings.Contains(err.Error(), "invalid offsets") {
    // retry once on a fresh read after a short sleep (file may have been mid-write)
    time.Sleep(50 * time.Millisecond)
    name, src, _, _, err = ast.ExtractFuncSourceByPosition(filePath, line)
}

Prevention

When it happens

Trigger: Calling ExtractFuncSourceByPosition when the AST node offsets do not map into the provided src buffer — e.g. src was mutated/replaced between parse and slice, a custom/empty FileSet mismatch, or a corrupted/truncated file that still parsed with recovered errors.

Common situations: Parsing with parser errors recovered (ParseFile returning a partial AST plus error handled elsewhere) and then slicing offsets; concurrent modification of the file; exotic BOM/encoding issues shifting offsets.

Related errors


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