d2lang/d2 · error
invalid gradient syntax
Error message
invalid gradient syntax
What it means
lib/color.ParseGradient parses CSS gradient strings like 'linear-gradient(...)' or 'radial-gradient(...)'. If the trimmed input does not match the required outer syntax — the gradient function name with parentheses wrapping parameters — it returns 'invalid gradient syntax'. The parser is strict: it accepts only these two gradient types with a balanced parenthesized parameter list.
Source
Thrown at lib/color/gradient.go:32
type Gradient struct {
Type string
Direction string
ColorStops []ColorStop
ID string
}
type ColorStop struct {
Color string
Position string
}
func ParseGradient(cssGradient string) (Gradient, error) {
cssGradient = strings.TrimSpace(cssGradient)
re := regexp.MustCompile(`^(linear-gradient|radial-gradient)\((.*)\)$`)
matches := re.FindStringSubmatch(cssGradient)
if matches == nil {
return Gradient{}, errors.New("invalid gradient syntax")
}
gradientType := matches[1]
params := matches[2]
gradient := Gradient{
Type: strings.TrimSuffix(gradientType, "-gradient"),
}
paramList := splitParams(params)
if len(paramList) == 0 {
return Gradient{}, errors.New("no parameters in gradient")
}
firstParam := strings.TrimSpace(paramList[0])
if gradient.Type == "linear" && (strings.HasSuffix(firstParam, "deg") || strings.HasPrefix(firstParam, "to ")) {View on GitHub (pinned to 0d69dca6f5)
Solutions
- Use only linear-gradient(...) or radial-gradient(...) syntax as the color value
- Trim whitespace and remove any trailing punctuation (e.g. ';') from the value before parsing
- Validate the string with lib/color.ValidColor (which uses ParseGradient) before assigning it to a style
Example fix
// before
grad, err := color.ParseGradient("conic-gradient(from 0deg, red, blue)") // invalid syntax
// after
grad, err := color.ParseGradient("linear-gradient(to right, red, blue)") Defensive patterns
Strategy: validation
Validate before calling
re := regexp.MustCompile(`^(linear-gradient|radial-gradient)\(.*\)$`)
if !re.MatchString(strings.TrimSpace(cssGradient)) {
return errors.New("not a supported linear/radial gradient")
} Type guard
func isSupportedGradient(s string) bool {
s = strings.TrimSpace(s)
return regexp.MustCompile(`^(linear-gradient|radial-gradient)\(.*\)$`).MatchString(s)
} Try / catch
grad, err := color.ParseGradient(input)
if err != nil && strings.Contains(err.Error(), "invalid gradient syntax") {
// fall back to a solid color
} Prevention
- Only emit linear-gradient/radial-gradient values into style fields
- Trim whitespace and stray punctuation before parsing
- Prefer ValidColor to validate any custom color/gradient input
When it happens
Trigger: Calling ParseGradient (directly or indirectly through ValidColor or color parsing of a style value) with a string that is not of the form 'linear-gradient(...)' or 'radial-gradient(...)', e.g. 'conic-gradient(red, blue)', 'linear-gradient(red, blue' (unbalanced parens), or extra text outside the parens.
Common situations: D2 style values using unsupported gradient types like conic-gradient; typos in the gradient function name; trailing semicolons or whitespace/newline artifacts; copying gradients from other CSS contexts that include vendor prefixes or surrounding declarations.
Related errors
- no parameters in gradient
- no color stops in gradient
- invalid color "%s"
- cannot parse hex color %v
- failed to unmarshal Range from %q: missing End field
AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31).
Data as JSON: /api/errors/28de4e29cfccdc36.
Report an issue: GitHub.