flipped-aurora/gin-vue-admin · error

[filpath:%s]非法模版文件!

Error message

[filpath:%s]非法模版文件!

What it means

A .tpl file directly under <templateDir>/server whose stem is neither "main.go" nor "plugin.go" cannot be classified by the generator, so templates() returns errors.Errorf("[filpath:%s]非法模版文件!"). Only main.go.tpl / plugin.go.tpl are recognized top-level file templates; everything else must live inside api/router/service subfolders.

Source

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

				if !secondDirs[j].IsDir() {
					ext := filepath.Ext(secondDirs[j].Name())
					if ext != ".tpl" {
						return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", three)
					}
					name := strings.TrimSuffix(secondDirs[j].Name(), ext)
					if name == "main.go" || name == "plugin.go" {
						pluginInitialize := &ast.PluginInitializeV2{
							Type:        ast.TypePluginInitializeV2,
							Path:        filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, name),
							PluginPath:  filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"),
							ImportPath:  fmt.Sprintf(`"%s/plugin/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
							PackageName: entity.PackageName,
						}
						asts[pluginInitialize.PluginPath+"=>"+pluginInitialize.Type.String()] = pluginInitialize
						creates[three] = pluginInitialize.Path
						continue
					}
					return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", three)
				}
				switch secondDirs[j].Name() {
				case "api", "router", "service":
					var threeDirs []os.DirEntry
					threeDirs, err = os.ReadDir(three)
					if err != nil {
						return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", three)
					}
					for k := 0; k < len(threeDirs); k++ {
						if threeDirs[k].Name() == ".DS_Store" {
							continue
						}
						four := filepath.Join(three, threeDirs[k].Name())
						if threeDirs[k].IsDir() {
							return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", four)
						}
						ext := filepath.Ext(four)
						if ext != ".tpl" {

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Move the reported .tpl file into the appropriate subfolder (api/, router/, service/, etc.) where arbitrary .tpl names are accepted.
  2. If it is the plugin entry template, keep the exact names main.go.tpl or plugin.go.tpl.
  3. Delete leftover/obsolete .tpl files at the server/ root level.
  4. Re-check after moving that the filename still contains api/enter/router/service keywords if it lives under api|router|service.

Example fix

// before
resource/package/server/utils.go.tpl
// after
mv resource/package/server/utils.go.tpl resource/package/server/service/  # or delete if unused
Defensive patterns

Strategy: validation

Validate before calling

allowed := map[string]bool{"main.go": true, "plugin.go": true}
entries, _ := os.ReadDir(filepath.Join(templateDir, "server"))
for _, e := range entries {
    if e.IsDir() || filepath.Ext(e.Name()) != ".tpl" {
        continue
    }
    stem := strings.TrimSuffix(e.Name(), ".tpl")
    if !allowed[stem] {
        return fmt.Errorf("server 层仅允许 main.go.tpl / plugin.go.tpl,发现 %s", e.Name())
    }
}

Type guard

func isServerRootTpl(name string) bool {
    stem := strings.TrimSuffix(name, ".tpl")
    return stem == "main.go" || stem == "plugin.go"
}

Try / catch

if err := generate(); err != nil && strings.Contains(err.Error(), "非法模版文件") {
    return fmt.Errorf("server 层存在无法识别的 .tpl 文件,移入 api/router/service 等子目录或删除: %w", err)
}

Prevention

When it happens

Trigger: Adding a custom .tpl file (e.g. config.tpl, xxx.go.tpl) directly in the server/ template level instead of inside api/, router/, service/, gen/, config/, initialize/, plugin/ or response/; renaming main.go.tpl to something else.

Common situations: Users extending templates by dropping new .tpl files at the wrong level; upgrading templates and leaving old files around; typos like Main.go.tpl or plugin.tpl at server/ level.

Related errors


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