larksuite/cli · info
marshal default config: %w
Error message
marshal default config: %w
What it means
EnsureDefaultConfig marshals the built-in default content-safety rules with json.MarshalIndent before writing the default file. This error wraps a marshal failure. Since the default config is a fixed static structure of strings and arrays, this is effectively unreachable and is a defensive guard.
Source
Thrown at internal/security/contentsafety/config.go:66
if err != nil {
return nil, fmt.Errorf("compile rule %q pattern: %w", r.ID, err)
}
rules = append(rules, rule{ID: r.ID, Pattern: compiled})
}
return &Config{Allowlist: raw.Allowlist, Rules: rules}, nil
}
func EnsureDefaultConfig(configDir string, errOut io.Writer) error {
path := filepath.Join(configDir, configFileName)
if _, err := vfs.Stat(path); err == nil {
return nil
}
if err := vfs.MkdirAll(configDir, 0700); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
data, err := json.MarshalIndent(defaultRawConfig(), "", " ")
if err != nil {
return fmt.Errorf("marshal default config: %w", err)
}
if err := vfs.WriteFile(path, append(data, '\n'), fs.FileMode(0600)); err != nil {
return err
}
fmt.Fprintf(errOut, "notice: created default content-safety config at %s\n", path)
return nil
}
func defaultRawConfig() rawConfig {
return rawConfig{
Allowlist: []string{"all"},
Rules: []rawRule{
{
ID: "instruction_override",
Pattern: `(?i)ignore\s+(all\s+|any\s+|the\s+)?(previous|prior|above|earlier)\s+(instructions?|prompts?|directives?)`,
},
{
ID: "role_injection",View on GitHub (pinned to 7fd6ef3c07)
Solutions
- Inspect the wrapped %w cause to identify the unmarshalable value
- Ensure defaultRawConfig returns only JSON-serializable types (strings, slices, maps)
- Add a test asserting EnsureDefaultConfig succeeds to catch regressions
Example fix
// before
func defaultRawConfig() rawConfig { return rawConfig{Rules: []rawRule{{Pattern: badFuncType}}} }
// after
func defaultRawConfig() rawConfig { return rawConfig{Rules: []rawRule{{ID: "x", Pattern: "y"}}} } Defensive patterns
Strategy: try-catch
Try / catch
if err := contentsafety.EnsureDefaultConfig(dir, os.Stderr); err != nil {
if strings.Contains(err.Error(), "marshal default config") {
// unreachable with stock defaults; indicates a modified defaultRawConfig
}
return err
} Prevention
- Keep defaultRawConfig limited to JSON-native types
- Add a test asserting EnsureDefaultConfig succeeds
- Review changes to defaults for unmarshalable fields
When it happens
Trigger: json.MarshalIndent(defaultRawConfig(), "", " ") returning an error. With the current static defaultRawConfig (plain strings/arrays) this cannot happen; it would only fire if defaults gained unsupported types (channels, funcs, cyclic references).
Common situations: Encountered only if the source defaultRawConfig was modified to include unmarshalable values — not reachable in normal use.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- parse content-safety config: %w
- malformed config
- invalid JSON in %s: %w
- invalid JSON in %s: %w
- SecretRef.source must be env|file|exec, got %q
AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04).
Data as JSON: /api/errors/7e6bd8fb1fd3f47a.
Report an issue: GitHub.