Skip to content

jq comments (#) are rejected in all positions, and the limitation is undocumented #33

Description

@gh123man

Description

fastjq rejects jq comments (#) in every position. jq 1.8.1 and gojq both accept them.

This isn't listed under Limitations in the README, and docs/SYNTAX.md doesn't mention
comments at all — so a caller migrating a working jq program has no warning. It bites
hardest when queries are authored by humans in a config file or UI, where a trailing
# why this filter exists is completely ordinary.

Reproducer

package main

import (
	"fmt"

	"github.com/DataDog/fastjq"
)

func main() {
	for _, q := range []string{
		`.foo = "baz" # set foo`,
		"# set foo\n.foo = \"baz\"",
		".foo = \"baz\"\n# trailing note",
		".a # mid\n| .b",
	} {
		_, err := fastjq.Compile(q)
		fmt.Printf("%-28q -> %v\n", q, err)
	}
}

Expected output: all four compile; comments are stripped as whitespace.

$ echo '{}' | jq '.foo = "baz" # set foo'
{
  "foo": "baz"
}

Actual output:

".foo = \"baz\" # set foo"    -> unexpected trailing input: "# set foo"
"# set foo\n.foo = \"baz\""   -> unexpected character "#"
".foo = \"baz\"\n# trailing note" -> unexpected trailing input: "# trailing note"
".a # mid\n| .b"              -> unexpected trailing input: "# mid\n| .b"

Suggested fix

Comment skipping belongs in the scanner's whitespace handling: on #, consume to the next
\n or end of input. jq's rule is that # starts a comment except inside a string literal,
and a backslash-newline continuation does not extend a comment (jq 1.7+ treats \ at end
of a comment line literally). The common cases are the four above.

If comments are out of scope by design, listing them under Limitations next to the
regex/decnum notes would be enough to save the next migrator the debugging.

Environment

  • fastjq version: c3336f252fa23bbda9a611024f6398904330cd9a (master, untagged)
  • Go version: go version go1.25.4 darwin/arm64
  • OS / arch: macOS, arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions