nats-io/nats-server · error

Parse error on line %d: '%s'

Error message

Parse error on line %d: '%s'

What it means

processItem receives items from the lexer and, when the item type is itemError (lexical failure), converts it into "Parse error on line %d: '%s'". The library surfaces lexer-level problems — unterminated strings, illegal characters, bad tokens — at a precise line number.

Source

Thrown at conf/parse.go:283

	}
	li := len(p.ikeys) - 1
	last := p.ikeys[li]
	p.ikeys = p.ikeys[0:li]
	return last
}

func (p *parser) processItem(it item, fp string) error {
	setValue := func(it item, v any) {
		if p.pedantic {
			p.setValue(&token{it, v, false, fp})
		} else {
			p.setValue(v)
		}
	}

	switch it.typ {
	case itemError:
		return fmt.Errorf("Parse error on line %d: '%s'", it.line, it.val)
	case itemKey:
		// Keep track of the keys as items and strings,
		// we do this in order to be able to still support
		// includes without many breaking changes.
		p.pushKey(it.val)

		if p.pedantic {
			p.pushItemKey(it)
		}
	case itemMapStart:
		newCtx := make(map[string]any)
		p.pushContext(newCtx)
	case itemMapEnd:
		setValue(it, p.popContext())
	case itemString:
		// FIXME(dlc) sanitize string?
		setValue(it, it.val)
	case itemInteger:

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Go to the reported line and fix the quoted token — close the quote or escape inner quotes
  2. Wrap problematic values (passwords, URLs with special chars) in single or double quotes properly
  3. Replace illegal characters or remove BOM/encoding artifacts
  4. Validate with `nats-server -t -c file.conf` after editing

Example fix

// before
authorization {
  password: "p@ss'word
}
// after
authorization {
  password: "p@ss'word"
}
Defensive patterns

Strategy: try-catch

Try / catch

cfg, err := conf.ParseFile(fp)
if err != nil {
    var line int
    if n, _ := fmt.Sscanf(err.Error(), "Parse error on line %d", &line); n == 1 {
        log.Fatalf("lexer failure in %s at line %d: inspect quotes/characters there", fp, line)
    }
    return err
}

Prevention

When it happens

Trigger: A config file contains a token the lexer cannot handle, e.g. an unterminated quoted string, an invalid escape, or an unexpected character; processItem is invoked from parse for every token.

Common situations: Secrets pasted with a trailing unclosed quote; special characters (backticks, unescaped quotes) in passwords; variables expanded with characters the lexer rejects; Windows line-ending or encoding oddities.

Understand the failure class

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/8cd0e9d5816f179f. Report an issue: GitHub.