flipped-aurora/gin-vue-admin · error

go.mod 中未找到 module 定义

Error message

go.mod 中未找到 module 定义

What it means

detectGoModule scans a go.mod file line by line for the 'module <name>' directive. If the file exists and is fully read but no module line is found, it returns this error, which surfaces from applyStandaloneDefaults.

Solutions

  1. Add or restore the module directive at the top of go.mod (e.g. `module github.com/user/repo/server`)
  2. Regenerate go.mod with `go mod init <module-path>` if it was malformed
  3. Verify the file being parsed is the real go.mod at the intended project root

Example fix

// before (go.mod)
require (
  github.com/spf13/viper v1.18.2
)
// after (go.mod)
module github.com/flipped-aurora/gin-vue-admin/server

go 1.20

require (
  github.com/spf13/viper v1.18.2
)
Defensive patterns

Strategy: validation

Validate before calling

// validate go.mod before calling detectGoModule
func validateGoMod(path string) error {
  data, err := os.ReadFile(path)
  if err != nil { return err }
  if !bytes.Contains(data, []byte("\nmodule ")) && !bytes.HasPrefix(data, []byte("module ")) {
    return errors.New(path + " has no module directive")
  }
  return nil
}

Prevention

When it happens

Trigger: A go.mod was found at the detected root, but it contains no 'module x/y' declaration (empty file, malformed, or commented out).

Common situations: Hand-crafted or truncated go.mod; a file named go.mod used as a marker without valid module syntax; tooling that stripped the module line; reading the wrong go.mod in a nested dir.

Related errors


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

Appendix: source

Thrown at server/cmd/mcp/config.go:186

	file, err := os.Open(goModPath)
	if err != nil {
		return "", err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		if strings.HasPrefix(line, "module ") {
			return strings.TrimSpace(strings.TrimPrefix(line, "module ")), nil
		}
	}

	if err := scanner.Err(); err != nil {
		return "", err
	}

	return "", errors.New("go.mod 中未找到 module 定义")
}

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

View on GitHub (pinned to 3136500ef3)