alibaba/open-code-review · critical

read embedded system_rules.json: %w

Error message

read embedded system_rules.json: %w

What it means

LoadDefault reads the embedded system_rules.json from the go:embed filesystem. This error wraps the embed.FS.ReadFile failure. Because the file is embedded at compile time, this should be impossible in a normal build and almost always indicates a broken embed directive, missing file, or a nonstandard build.

Source

Thrown at internal/config/rules/system_rules.go:96

		}
		// Read value
		var value string
		if err := dec.Decode(&value); err != nil {
			return fmt.Errorf("read path_rule_map value for %q: %w", key, err)
		}
		r.PathRules = append(r.PathRules, PathRule{Pattern: key, Rule: value})
	}
	return nil
}

//go:embed system_rules.json rule_docs/*
var rulesFS embed.FS

// LoadDefault parses the embedded system_rules.json and resolves rule file references.
func LoadDefault() (*SystemRule, error) {
	data, err := rulesFS.ReadFile("system_rules.json")
	if err != nil {
		return nil, fmt.Errorf("read embedded system_rules.json: %w", err)
	}
	var rule SystemRule
	if err := json.Unmarshal(data, &rule); err != nil {
		return nil, fmt.Errorf("unmarshal default system rules: %w", err)
	}
	content, err := rulesFS.ReadFile("rule_docs/" + rule.DefaultRule)
	if err != nil {
		return nil, fmt.Errorf("read default rule file %q: %w", rule.DefaultRule, err)
	}
	rule.DefaultRule = strings.TrimRight(string(content), "\n")
	for i := range rule.PathRules {
		content, err := rulesFS.ReadFile("rule_docs/" + rule.PathRules[i].Rule)
		if err != nil {
			return nil, fmt.Errorf("read rule file %q for pattern %q: %w", rule.PathRules[i].Rule, rule.PathRules[i].Pattern, err)
		}
		rule.PathRules[i].Rule = strings.TrimRight(string(content), "\n")
	}
	return &rule, nil

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Verify internal/config/rules/system_rules.json exists and the //go:embed directive lists it (go:embed system_rules.json rule_docs/*)
  2. Run go build ./... to regenerate/validate embedding; check the file is not in .gitignore and present in the module
  3. If the error persists in a custom build, rebuild with the standard Go toolchain so embed files are packed

Example fix

// before
//go:embed rule_docs/*
var rulesFS embed.FS
// after
//go:embed system_rules.json rule_docs/*
var rulesFS embed.FS
Defensive patterns

Strategy: try-catch

Try / catch

rule, err := rules.LoadDefault()
if err != nil {
	if strings.Contains(err.Error(), "read embedded system_rules.json") {
		log.Fatalf("broken build: embedded assets missing (%v) — rebuild with the standard Go toolchain", err)
	}
	return fmt.Errorf("load default rules: %w", err)
}

Prevention

When it happens

Trigger: rulesFS.ReadFile("system_rules.json") fails: the //go:embed pattern was edited, the file was renamed/deleted, or the package is built in an environment that stripped embedded files.

Common situations: After refactoring the package directory, the file moved but the embed pattern wasn't updated; build tooling that post-processes/strip binaries; custom build systems bypassing go:embed.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/1bdd9211791e1d2c. Report an issue: GitHub.