flipped-aurora/gin-vue-admin · error

打印插件初始化文件失败

Error message

打印插件初始化文件失败

What it means

writePluginInitializeFile renders the AST-modified initialize file back to source with printer.Fprint. Failure (rare — usually an inconsistent fileSet/AST pair) is wrapped as '打印插件初始化文件失败'. Called by InitMenu/InitAPI/InitDictionary after inserting entries into the model array.

Source

Thrown at server/service/system/auto_code_plugin.go:1112

	if err != nil {
		return "", nil, nil, nil, errors.Wrap(err, "读取插件初始化文件失败")
	}
	fileSet := token.NewFileSet()
	astFile, err := parser.ParseFile(fileSet, path, src, parser.ParseComments)
	if err != nil {
		return "", nil, nil, nil, errors.Wrap(err, "解析插件初始化文件失败")
	}
	arrayAst := ast.FindArray(astFile, "model", selectorName)
	if arrayAst == nil {
		return "", nil, nil, nil, errors.Errorf("插件初始化文件缺少 []model.%s 数组", selectorName)
	}
	return path, fileSet, astFile, arrayAst, nil
}

func writePluginInitializeFile(path string, fileSet *token.FileSet, astFile *goast.File) error {
	var out bytes.Buffer
	if err := printer.Fprint(&out, fileSet, astFile); err != nil {
		return errors.Wrap(err, "打印插件初始化文件失败")
	}
	if err := os.WriteFile(path, out.Bytes(), 0666); err != nil {
		return errors.Wrap(err, "写入插件初始化文件失败")
	}
	return nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Re-run the init operation so the file is re-parsed and re-printed in one pass
  2. Ensure only one initialization runs at a time (no concurrent InitMenu/InitAPI/InitDictionary on the same plugin)
  3. Verify the file was not modified between read and write; restore from VCS if inconsistent
  4. If reproducible, regenerate the plugin initialize file to reset a clean AST state

Example fix

// before: concurrent InitMenu + InitAPI on the same plugin → inconsistent fileSet
// after: serialize the operations
await initMenu(plugin); await initAPI(plugin)
Defensive patterns

Strategy: retry

Try / catch

err := pluginSvc.InitMenu(ctx, pluginName)
if err != nil && strings.Contains(err.Error(), "打印插件初始化文件失败") {
    // transient/AST-state issue: re-run once; a persisting failure means
    // regenerate the initialize file from the plugin template
}

Prevention

When it happens

Trigger: printer.Fprint erroring while serializing the mutated goast.File — typically when AST nodes were built or modified with positions outside the provided token.FileSet, or the AST is malformed after concurrent modification.

Common situations: Two init operations modifying the same initialize file concurrently; custom AST node insertion with wrong Pos values; the file changed on disk between parse and print, invalidating the fileSet.

Related errors


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