go-kratos/kratos Β· error

🚫 failed to parse `go.mod`: %v

Error message

🚫 failed to parse `go.mod`: %v

What it means

After locating the module root in no-module mode, the CLI reads go.mod via base.ModulePath to extract the module path. If the file cannot be read or the module directive cannot be extracted (empty, truncated, or malformed go.mod), the parse error is wrapped and creation aborts.

Source

Thrown at cmd/kratos/internal/project/project.go:103

			done <- p.New(ctx, workingDir, repoURL, branch)
			return
		}
		projectRoot := getgomodProjectRoot(workingDir)
		if gomodIsNotExistIn(projectRoot) {
			done <- fmt.Errorf("🚫 go.mod don't exists in %s", projectRoot)
			return
		}

		packagePath, e := filepath.Rel(projectRoot, filepath.Join(workingDir, projectName))
		if e != nil {
			done <- fmt.Errorf("🚫 failed to get relative path: %v", e)
			return
		}
		packagePath = strings.ReplaceAll(packagePath, "\\", "/")

		mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod"))
		if e != nil {
			done <- fmt.Errorf("🚫 failed to parse `go.mod`: %v", e)
			return
		}
		// Get the relative path for adding a project based on Go modules
		p.Path = filepath.Join(strings.TrimPrefix(workingDir, projectRoot+"/"), p.Name)
		done <- p.Add(ctx, workingDir, repoURL, branch, mod, packagePath)
	}()
	select {
	case <-ctx.Done():
		if errors.Is(ctx.Err(), context.DeadlineExceeded) {
			fmt.Fprint(os.Stderr, "\033[31mERROR: project creation timed out\033[m\n")
			return
		}
		fmt.Fprintf(os.Stderr, "\033[31mERROR: failed to create project(%s)\033[m\n", ctx.Err().Error())
	case err = <-done:
		if err != nil {
			fmt.Fprintf(os.Stderr, "\033[31mERROR: Failed to create project(%s)\033[m\n", err.Error())
		}
	}

View on GitHub (pinned to 668db92c2c)

Solutions

  1. Run go mod tidy / go mod init <path> in the project root and confirm go build succeeds
  2. Open go.mod and make sure the first non-comment line is a valid module <path> directive
  3. Check go.mod permissions and content integrity (no truncation or conflict markers)

Example fix

# before - go.mod with no module directive
go 1.22

# after
module github.com/me/shop
go 1.22
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("go", "mod", "edit", "-json").Output()
if err != nil {
    log.Fatal(err)
}
var m struct {
    Module struct{ Path string }
}
_ = json.Unmarshal(out, &m)
if m.Module.Path == "" {
    log.Fatal("go.mod has no module directive")
}

Prevention

When it happens

Trigger: base.ModulePath(projectRoot/go.mod) returns an error β€” file unreadable, empty, or missing a valid module directive.

Common situations: Hand-edited go.mod with syntax errors; aborted tooling leaving an empty go.mod; merge-conflict markers left in go.mod; permissions problems.

Understand the failure class

Related errors


AI-assisted analysis of go-kratos/kratos@668db92c2c (2026-08-16). Data as JSON: /api/errors/4d20f504fb3291ac. Report an issue: GitHub.