flipped-aurora/gin-vue-admin · error

read file failed: %w

Error message

read file failed: %w

What it means

ExtractFuncSourceByPosition reads the file at filePath with os.ReadFile before AST parsing; any read failure (file missing, permission denied, path is a directory) is wrapped as 'read file failed: %w'. It's a filesystem-level failure, independent of Go syntax validity.

Source

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

package ast

import (
    "fmt"
    "go/ast"
    "go/parser"
    "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
        }

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Pass an absolute path (filepath.Abs / root-relative to repo) and verify with os.Stat before calling
  2. Confirm the file still exists at that location; regenerate/relocate if it was moved
  3. Check file permissions if the path exists but read is denied

Example fix

// before
name, src, _, _, err := ExtractFuncSourceByPosition("sys_user.go", 55)
// after
abs, _ := filepath.Abs("server/service/system/sys_user.go")
name, src, _, _, err := ExtractFuncSourceByPosition(abs, 55)
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(filePath); err != nil {
    return fmt.Errorf("file not readable before extraction: %w", err)
}

Type guard

func isReadableFile(path string) bool {
    info, err := os.Stat(path)
    return err == nil && !info.IsDir()
}

Try / catch

name, source, start, end, err := ast.ExtractFuncSourceByPosition(path, line)
if err != nil {
    if strings.HasPrefix(err.Error(), "read file failed") {
        log.Warnf("cannot read %s: %v — check path/cwd", path, err)
    }
}

Prevention

When it happens

Trigger: Passing a relative path from a different working directory; file deleted/moved after the error was captured; typo'd path; reading a path you lack permission for.

Common situations: Tooling resolving paths from a stale index while files were refactored; running the extractor from a different cwd than the error's file root; container/host path mismatch.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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