siyuan-note/siyuan · error

builtin color must not be null

Error message

builtin color must not be null

What it means

normalizeInlineStyleBuiltin validates the builtin section of /storage/inline-styles.json before it is used. It rejects any entry in builtin.colors that deserialized to nil (an explicit JSON null element in the colors array). The library refuses to continue rather than later dereference a nil color struct.

Solutions

  1. Open /storage/inline-styles.json in the workspace and remove the null element from builtin.colors
  2. Validate the JSON with a schema/linter (each colors entry needs index, light, dark) before writing it back
  3. If the file was corrupted by sync, stop sync, restore a known-good copy of inline-styles.json, then resync
  4. If produced by code, ensure every element appended to Colors is a non-nil &InlineStyleBuiltinColor{...}

Example fix

// before (inline-styles.json)
"builtin": { "colors": [ {"index": 1, ...}, null ] }
// after
"builtin": { "colors": [ {"index": 1, "light": {...}, "dark": {...}} ] }
Defensive patterns

Strategy: validation

Validate before calling

const cfg = JSON.parse(await fs.readFile('/workspace/storage/inline-styles.json', 'utf8'));
const bad = (cfg.builtin?.colors ?? []).findIndex(c => c == null);
if (bad !== -1) throw new Error(`builtin.colors[${bad}] is null`);

Type guard

function isBuiltinColor(c) { return c !== null && typeof c === 'object' && Number.isInteger(c.index); }

Try / catch

try { styles = loadInlineStyles(); } catch (err) { if (strings.Contains(err.Error(), "builtin color must not be null")) { /* restore inline-styles.json from backup, then retry */ } return err; }

Prevention

When it happens

Trigger: Calling setInlineStylesData or loadInlineStyles when the workspace inline-styles.json contains `"colors": [null, ...]` under builtin, or when Go code passes an InlineStyleBuiltin whose Colors slice holds a nil *InlineStyleBuiltinColor.

Common situations: Hand-edited or externally synced inline-styles.json with a stray null element; a sync merge tool or script rewriting the JSON and emitting null placeholders; a buggy generator that appends a zero-value entry.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/959e59e64e7828cb. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/inline_style.go:713

		if lightColor != darkColor || lightBackground != darkBackground {
			return nil, fmt.Errorf("inline style [%s] must use the same fields in light and dark themes", id)
		}

		ret = append(ret, &InlineStyle{ID: id, Name: name, Hidden: style.Hidden, Light: light, Dark: dark})
	}
	return ret, nil
}

func normalizeInlineStyleBuiltin(builtin *InlineStyleBuiltin) (ret *InlineStyleBuiltin, err error) {
	ret = newEmptyInlineStyleBuiltin()
	if builtin == nil {
		return ret, nil
	}

	colorIndexes := make(map[int]struct{}, len(builtin.Colors))
	for _, color := range builtin.Colors {
		if color == nil {
			return nil, errors.New("builtin color must not be null")
		}
		if color.Index < minBuiltinColorIndex || neutralAVColorIndex < color.Index {
			return nil, fmt.Errorf("builtin color index [%d] must be between %d and %d", color.Index,
				minBuiltinColorIndex, neutralAVColorIndex)
		}
		if _, exists := colorIndexes[color.Index]; exists {
			return nil, fmt.Errorf("duplicate builtin color index [%d]", color.Index)
		}
		colorIndexes[color.Index] = struct{}{}

		light, dark, err := normalizeInlineStyleThemePair(color.Light, color.Dark, fmt.Sprintf("builtin color [%d]", color.Index))
		if err != nil {
			return nil, err
		}
		ret.Colors = append(ret.Colors, &InlineStyleBuiltinColor{Index: color.Index, Light: light, Dark: dark})
	}
	sort.Slice(ret.Colors, func(i, j int) bool {
		return ret.Colors[i].Index < ret.Colors[j].Index

View on GitHub (pinned to 9f775e8a12)