ent/ent · error

unexpected bracket

Error message

unexpected bracket

What it means

sqljson.ParsePath treats `[` as the start of an array-index segment. This error is returned when the `[` is the last character of the path, so no index or closing bracket can follow. It signals a truncated path expression.

Source

Thrown at dialect/sql/sqljson/sqljson.go:629

	)
	for i < len(dotpath) {
		switch r := dotpath[i]; {
		case r == '"':
			if i == len(dotpath)-1 {
				return nil, fmt.Errorf("unexpected quote")
			}
			idx := strings.IndexRune(dotpath[i+1:], '"')
			if idx == -1 || idx == 0 {
				return nil, fmt.Errorf("unbalanced quote")
			}
			i += idx + 2
		case r == '[':
			if p != i {
				path = append(path, dotpath[p:i])
			}
			p = i
			if i == len(dotpath)-1 {
				return nil, fmt.Errorf("unexpected bracket")
			}
			idx := strings.IndexRune(dotpath[i:], ']')
			if idx == -1 || idx == 1 {
				return nil, fmt.Errorf("unbalanced bracket")
			}
			if !isNumber(dotpath[i+1 : i+idx]) {
				return nil, fmt.Errorf("invalid index %q", dotpath[i:i+idx+1])
			}
			i += idx + 1
		case r == '.' || r == ']':
			if p != i {
				path = append(path, dotpath[p:i])
			}
			i++
			p = i
		default:
			i++
		}

View on GitHub (pinned to 69d5d4deb1)

Solutions

  1. Append the numeric index and closing bracket: `a[0]`
  2. Remove the stray `[` if indexing was not intended
  3. Ensure index loops append `[i]` pairs together

Example fix

// before
sqljson.Path(`items[`)
// after
sqljson.Path(`items[0]`)
Defensive patterns

Strategy: validation

Validate before calling

if strings.HasSuffix(p, "[") {
    return errors.New("truncated bracket segment")
}

Try / catch

path, err := sqljson.ParsePath(p)
if err != nil {
    return fmt.Errorf("invalid json path %q: %w", p, err)
}

Prevention

When it happens

Trigger: Calling ParsePath/DotPath with a path ending in `[`, e.g. `a[`, or a path like `items[0][` built by incomplete loop logic.

Common situations: Dynamically built paths where the index part was never appended; copy-paste truncation of long paths; template rendering that dropped the index.

Related errors


AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03). Data as JSON: /api/errors/2b29fee76c7ac44e. Report an issue: GitHub.