flipped-aurora/gin-vue-admin · error

读取模版文件夹失败!

Error message

读取模版文件夹失败!

What it means

AutoCodePackage.Templates lists the code-generator template folders by reading the relative directory "resource" with os.ReadDir. When that read fails (directory missing, not a directory, or permission denied), the underlying error is wrapped with the message "读取模版文件夹失败!" and returned, so the frontend template picker gets no folder list.

Source

Thrown at server/service/system/auto_code_package.go:282

		filteredEntities := []model.SysAutoCodePackage{}
		for i := 0; i < len(entities); i++ {
			if existingPackageNames[entities[i].PackageName] {
				filteredEntities = append(filteredEntities, entities[i])
			}
		}
		entities = filteredEntities
	}

	return entities, nil
}

// Templates 获取所有模版文件夹
// @author: [SliverHorn](https://github.com/SliverHorn)
func (s *autoCodePackage) Templates(ctx context.Context) ([]string, error) {
	templates := make([]string, 0)
	entries, err := os.ReadDir("resource")
	if err != nil {
		return nil, errors.Wrap(err, "读取模版文件夹失败!")
	}
	ignoreDirs := map[string]bool{
		"page":     true, // page 为表单生成器
		"function": true, // function 为函数生成器
		"preview":  true, // preview 为预览代码生成器的代码
		"mcp":      true, // mcp 为mcp生成器的代码
		"ai_cli":   true, // ai_cli 为 AI CLI 生成器的代码
	}
	for i := 0; i < len(entries); i++ {
		if entries[i].IsDir() {
			if ignoreDirs[entries[i].Name()] {
				continue
			}
			templates = append(templates, entries[i].Name())
		}
	}
	return templates, nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Start the server with its working directory set to the server root (where server/resource exists), e.g. cd server && ./gva, or fix the WorkingDirectory in the systemd/supervisor unit.
  2. Verify the resource directory exists: ls ./resource; restore it from the repository if it was deleted.
  3. Check directory permissions so the process user can read resource/.
  4. If embedding/deploying differently, adjust the hard-coded "resource" path or run behind the documented launch script.

Example fix

// before (running from wrong cwd)
$ /opt/gva/server  # /opt/gva has no resource/
// after
$ cd /opt/gva/server && ./gva  # ./resource exists here
Defensive patterns

Strategy: validation

Validate before calling

if info, err := os.Stat("resource"); err != nil || !info.IsDir() {
    return fmt.Errorf("resource/ 模板目录不存在于工作目录 %s", os.Getwd())
}

Try / catch

names, err := svc.Templates(ctx)
if err != nil {
    log.Printf("读取模板列表失败(检查工作目录下是否存在 resource/): %v", err)
    return err
}

Prevention

When it happens

Trigger: Calling GET the auto-code package templates API (s.autoCodePackage.Templates) when the process working directory does not contain a "resource" directory — e.g. the server binary is launched from a directory other than the repo/server root, or the resource folder was deleted or not deployed.

Common situations: Running the compiled server binary from / or a systemd unit with a wrong WorkingDirectory; Docker image missing server/resource; renaming or moving resource/; running tests from a package directory instead of the server root.

Related errors


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