Skip to content

last/1 on an empty generator produces no output; jq produces null #349

Not planned

Description

@yppp

Summary

In gojq, last/1 applied to a generator that produces no values yields no
output at all. In jq it yields null.

Versions

  • gojq 0.12.20
  • jq 1.7 (reference implementation)

Reproduction

$ echo null | gojq -n 'last(empty)'
(no output)
$ echo null | jq -n 'last(empty)'
null

$ echo null | gojq -n '[last(empty)]'
[]
$ echo null | jq -n '[last(empty)]'
[null]

$ echo '[]' | gojq -c 'last(.[])'
(no output)
$ echo '[]' | jq -c 'last(.[])'
null

last(range(0)) behaves the same way.

Notes

  • first/1 agrees between the two implementations: first(empty) produces
    no output in both (jq defines it via label/break). Only last differs.
  • In jq, last(g) is defined in src/builtin.jq as
    def last(g): reduce g as $item (null; $item);, so an empty generator
    reduces to the initial value, null. gojq implements last/1 through its
    internal _last function, which appears to emit nothing when the generator
    is empty.
  • This difference is not mentioned in the README's "Difference to jq"
    section, so I am reporting it in case it is unintended.

Disclosure: this investigation was done with AI assistance (differential
testing of gojq against jq).

Activity

  1. g4lb commented on Nov 30, 2026

    @g4lb
    Contributor

    This matches jq 1.8.0, which changed last(empty) to output nothing (#3179). jq 1.7 gives null.

  2. daichunghy commented on Nov 30, 2026

    @daichunghy
    Contributor

    I've reproduced this on current main. last(empty) prints nothing, whereas jq 1.7 prints null. The same happens with last(range(0)) and last(.[]) on [].

    The issue is in compileLast in compiler.go. When the generator yields no values, the $x flag stays true, causing the compiled code to backtrack without producing any output. jq defines last(g) as reduce g as $item (null; $item), so an empty generator reduces to null.

    I'd like to take this one. My plan is to have compileLast push null and return instead of backtracking when no value was seen. I'll add CLI test cases for last(empty), [last(empty)], and last(range(0)), and update the existing empty-input last case to expect [null]. I'll check all of these against jq 1.7 and run the full test suite before opening the PR.

  3. wader commented on Nov 30, 2026

    @wader
    SponsorContributor

    As jq is a generator based language it makes sense for functions like last to emit no values.

  4. itchyny commented on Nov 30, 2026

    @itchyny
    Owner

    last(empty) should yield nothing. This issue and linked PR are truly awful. They don’t even bother to check the behavior of the latest release version of jq, nor do they use git blame to look into the history of the functions they've fixed. See 94dd99a (Why do I have to take my time to link this commit for you). If last(empty) can yield null, simpler definition using reduce is enough. Sigh. Honestly, wasting time on this AI-generated slop is just absolute bullshit. Close #350.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions