flipped-aurora/gin-vue-admin · error

[filpath:%s]读取模版文件失败!

Error message

[filpath:%s]读取模版文件失败!

What it means

During generate (used by Create and Preview), each template file is parsed with text/template ParseFiles; a parse/read failure is wrapped as '[filpath:%s]读取模版文件失败!'. It means the .tpl file at `key` is missing, unreadable, or has invalid template syntax, so template generation aborts.

Source

Thrown at server/service/system/auto_code_template.go:164

		builder.WriteString("```" + suffix + "\n\n")
		builder.WriteString(writer.String())
		builder.WriteString("\n\n```")
		preview[key] = builder.String()
	}
	return preview, nil
}

func (s *autoCodeTemplate) generate(ctx context.Context, info request.AutoCode, entity model.SysAutoCodePackage) (map[string]strings.Builder, map[string]string, map[string]utilsAst.Ast, error) {
	templates, asts, _, err := AutoCodePackage.templates(ctx, entity, info, false)
	if err != nil {
		return nil, nil, nil, err
	}
	code := make(map[string]strings.Builder)
	for key, create := range templates {
		var files *template.Template
		files, err = template.New(filepath.Base(key)).Funcs(autocode.GetTemplateFuncMap()).ParseFiles(key)
		if err != nil {
			return nil, nil, nil, errors.Wrapf(err, "[filpath:%s]读取模版文件失败!", key)
		}
		var builder strings.Builder
		err = files.Execute(&builder, info)
		if err != nil {
			return nil, nil, nil, errors.Wrapf(err, "[filpath:%s]生成文件失败!", create)
		}
		code[create] = builder
	} // 生成文件
	injectedCode, injections, err := renderAutoCodeInjections(info, asts)
	if err != nil {
		return nil, nil, nil, err
	}
	for key, builder := range injectedCode {
		code[key] = builder
	}
	// 注入代码
	return code, templates, injections, nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Check the filepath printed in the error exists and is readable (ls -l <path>); restore the missing .tpl from the official server/resource/template directory.
  2. Validate the template syntax — unbalanced {{if}}/{{range}}/{{end}} or bad pipeline causes ParseFiles to fail; revert recent custom edits to that .tpl.
  3. Verify config.yaml autoCode section (root/server/web paths) points to the directory that actually contains the template resources.

Example fix

// bad template
{{ if .HasDataSource }}
  ...missing {{ end }}
// fixed
{{ if .HasDataSource }}
  ...
{{ end }}
Defensive patterns

Strategy: validation

Validate before calling

tplPath := key
if _, err := os.Stat(tplPath); err != nil {
  return fmt.Errorf("template missing: %s", tplPath)
}
if _, err := template.New(filepath.Base(tplPath)).ParseFiles(tplPath); err != nil {
  return fmt.Errorf("template syntax invalid: %s: %v", tplPath, err)
}

Try / catch

result, err := svc.generate(...)
if err != nil {
  if strings.Contains(err.Error(), "读取模版文件失败") {
    log.Printf("restore template from server/resource/template: %v", err)
  }
  return err
}

Prevention

When it happens

Trigger: generate() iterating the templates map when: the template path does not exist on disk (AutoCode.Root/Server/Web config wrong), file permissions deny read, or the .tpl content has malformed {{ }} actions that text/template cannot parse.

Common situations: Upgrading gin-vue-admin but resource/template directories out of sync; running the server from a working directory where GVA_CONFIG.AutoCode.Root is relative and wrong; custom-edited .tpl with unbalanced {{if}}/{{end}}; missing template files in Docker image due to incomplete COPY.

Related errors


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