Skip to content

fix(jq): fail assignment through unsupported path filters instead of writing to the root - #787

Open
anhnt256 wants to merge 8 commits into
vercel-labs:mainfrom
anhnt256:fix/jq-783-update-path
Open

anhnt256 wants to merge 8 commits into
vercel-labs:mainfrom
anhnt256:fix/jq-783-update-path

Conversation

@anhnt256

@anhnt256 anhnt256 commented Dec 1, 2026

Copy link
Copy Markdown

Fixes #783

Problem

(.rows[] | select(.id == "a")).s = "y" silently wrote "s": "y" at the
document root (exit 0) instead of updating the selected element. The same
happened for |=, for getpath(...) |= ..., and even for static paths
through a parenthesized pipe such as (.a | .b) = 1.

Root cause: updateRecursive in packages/just-bash/src/commands/query-engine/evaluator.ts
had no Paren case, so a parenthesized path fell into default: return transform(val)
and applied the update to the current value; and the Pipe case applied the
right-hand update at the root instead of inside the left-hand selection.

Fix

  • Unwrap Paren nodes in updateRecursive.
  • Fix the Pipe case to apply the right-hand update inside the value the
    left-hand path selects.
  • Throw Invalid path expression in assignment: ... is not supported (exit
    code 5) for path nodes that cannot be resolved to static paths yet
    (select(), map(), first(), getpath(), ...), instead of silently
    writing to the wrong location — the interim the issue asks for until full
    path-through-filter support lands.

Verification

@vercel

vercel Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@anhnt256 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@auto-maintain

auto-maintain Bot commented Dec 1, 2026

Copy link
Copy Markdown

🤖 auto-maintain review

Automated, advisory triage for @anhnt256's PR. Facts below are read from the GitHub API.

Check Result
Author's merged PRs (this repo) 0 — ⚠️ first-time contributor
Account established ✅ (age 3863d · 2 followers · 70 public repos)
Commits signed/verified ✅ 3/3
Changeset included ✅ (.changeset/jq-update-path-783.md)

Review panel: 🔴 high highest severity

just-bash maintainer code review: 🟡 medium

The pipe update fix mishandles valid paths whose right-hand index depends on the left-hand selection.

  • packages/just-bash/src/commands/query-engine/evaluator.ts:1317 — Pipe RHS dynamic indices are evaluated against the document root, not the value selected by the left side. For example, `(.a | .[.key]) = 1` on `{"a":{"key":"x","x":0}}` reads `.key` from the root and leaves `a.x` unchanged instead of setting it to 1.

General code review: 🟡 medium

The fix still has data-dependent behavior and can update the wrong key for dynamic paths through a pipe.

  • packages/just-bash/src/commands/query-engine/evaluator.ts:1317 — Pipe updates evaluate dynamic right-hand indices against the document root, not the value selected by the pipe. For `{"a":{"i":"x","x":0},"i":"y"}`, `(.a | .[.i]) = 1` writes `a.y` instead of updating `a.x`.
  • packages/just-bash/src/commands/query-engine/evaluator.ts:1316 — Unsupported filters are only rejected if the left side selects a value. With `{"rows":[]}`, the advertised failing expression `(.rows[] | select(.id=="a")).s = "y"` never invokes the callback containing `select`, so it silently exits 0 instead of reporting unsupported syntax.

Adversarial security: 🔴 high

Merge blocker: the evaluator has a syntax error.

  • packages/just-bash/src/commands/query-engine/evaluator.ts:1333 — The new default block removes updateRecursive's closing brace; the file now has an unmatched `{` and cannot compile. Restore a `}` after the switch.

Adversarial security (second opinion): 🔴 high

No malicious or supply-chain changes found, and the Paren/Pipe fix is directionally sound, but the head commit leaves evaluator.ts syntactically invalid (missing switch closing brace), so it cannot build and the PR's verification claims are unsupported — blocking.

  • packages/just-bash/src/commands/query-engine/evaluator.ts:1333 — The `default:` block was given its own braces but the `switch (path.type)` closing brace was deleted, so `updateRecursive` is left unclosed. Whole-file brace balance goes from 0 (base) to +1 (head): `evaluator.ts` no longer parses, so the entire `just-bash` package fails to compile and every jq code path is dead. The single `}` on this line must become two (close the switch, then close `updateRecursive`).
  • packages/just-bash/src/commands/query-engine/evaluator.ts:1335 — Because the switch brace is missing, `const transformer = ...` and `return updateRecursive(root, pathExpr, transformer)` now sit inside `updateRecursive` itself. If this is "fixed" by appending a brace at the end of the function rather than after the switch, `updateRecursive` calls itself with the same arguments on every invocation — unbounded recursion / stack exhaustion on any jq assignment. Place the brace immediately after the switch.
  • packages/just-bash/src/commands/query-engine/evaluator.ts:1309 — `case "Paren": {` is indented 12 spaces inside a 6-space switch body. Together with the unbalanced braces this shows the PR body's verification claims ("tsc --noEmit clean, biome check clean, 377 passed, 768 passed") cannot have been produced from this head commit — the stated evidence does not match the committed code, so none of the behavioural claims about the fix have actually been demonstrated.

Standard Bash and host portability: 🔴 high

Merge blocker: the changed evaluator is syntactically invalid due to a missing closing brace.

  • packages/just-bash/src/commands/query-engine/evaluator.ts:1333 — The new default block closes, then only the switch closes; `updateRecursive` is missing its closing `}`. The file has 419 opening braces versus 418 closing braces, so TypeScript parsing fails and the package cannot build.

Posted by auto-maintain. This automated code review is advisory; a human maintainer makes the call.

@privatenumber privatenumber added awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working commands Behavior of, or support for, individual commands p1 Prompt action for substantial impact. Priority is not acceptance or a delivery promise. labels Dec 1, 2026

@privatenumber privatenumber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed follow-ups covering edge cases found in review:

  • Enforce work and recursion depth limits across Paren, Pipe, Field, Index, and Iterate in updateRecursive.
  • Preserve special object keys (such as constructor) safely on Iterate updates.
  • Evaluate dynamic Index expressions against pipe-stage input, support multi-index updates, and avoid creating missing parent containers when an index selection is empty.
  • Align = RHS evaluation, null/primitive container handling, compound assignment limits/arithmetic (+= null, /= 0), yq currentPath propagation, and multi-document yq -i output.

All 845 jq/yq/query-engine tests, 262 xan tests, typecheck, and lint pass. Looks good to merge.

Comment on lines +1915 to +1919
(current, _selected, currentCtx) => {
const results = evaluate(current, valueExpr, currentCtx);
if (results.length > 0) return results[0];
markDeletion(currentCtx.updatePath);
return noSelection;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newly supported update behaviors leave seven spec cases marked as expected failures, causing the jq spec suite and normal test run to fail with UNEXPECTED PASS errors.

Fix on Vercel

This branch has not been deployed

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

Labels

awaiting-review Maintainer review or re-review is next. Does not imply approval or merge readiness. bug Something isn't working commands Behavior of, or support for, individual commands p1 Prompt action for substantial impact. Priority is not acceptance or a delivery promise.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jq: assignment through select() writes to the root instead of the selected element

2 participants