Skip to content

fix(scripting): CRLF bug replace decomment with jsonc-parser - #9602

Closed
abhishekp-bruno wants to merge 8 commits into
usebruno:mainfrom
abhishekp-bruno:feat/replace-decomment-with-jsonc-parser
Closed

abhishekp-bruno wants to merge 8 commits into
usebruno:mainfrom
abhishekp-bruno:feat/replace-decomment-with-jsonc-parser

Conversation

@abhishekp-bruno

@abhishekp-bruno abhishekp-bruno commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

BRU-4710

Description

A valid // comment in a request's pre-request script can make the script fail with SyntaxError: expecting ')', even though the JavaScript is valid. Removing the comment, or changing it to a /* */ block comment, makes the request run normally.

Problem

in certain conditions when we have CRLF and LF endings in a merged script, decomment library would fail to parse it at the line where a line had CRLF and next line would have LF or vice versa and return whatever it parsed till that point.

we wrap our scripts in a async wrapper and this incomplete parser would miss the closing })(...) causing the script to fail while executing

Fix

with the wrapper in place we do not need decomment anymore in the scripting part of our app.
it is needed in the JSON body of the request.
we replace the decomment library with the jsonc-parcer since the decomment library is not maintained anymore and causes the parser to fail on any new JS features introduced after 2018 since it is internally dependant on esprima 4 which is outdated now.

Screenshots

Before After

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.
  • I've run the claude code review skill locally.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • Bug Fixes
    • Comments in JSON request bodies and GraphQL variables are handled correctly, while comment-like text inside strings and numeric separators are preserved.
    • Scripts with comments and CRLF or CR-only line endings now run reliably across supported runtimes, including comment-only scripts.

…decomment

Comments in JSON request bodies and GraphQL variables were stripped with
`decomment`, which tokenises the text with esprima 4 to locate regex literals.
Esprima 4 has not kept up with the language, so a body containing a numeric
separator such as `1_000` threw before the request was sent. `decomment` also
picks a single line ending for the whole text by majority vote, which is wrong
for any input that mixes CRLF and LF.

`jsonc-parser` is the comment-aware JSON scanner VS Code uses. It is already a
dependency of bruno-app, has no dependencies of its own, scans only JSON tokens
so no JavaScript syntax can trip it, and produces byte-identical output to
`decomment` on every body shape the existing specs cover.

- Swap the JSON body and GraphQL variable sites in the app and the CLI to
  `stripComments` from `jsonc-parser`.
- Declare `jsonc-parser` in both manifests; it previously resolved only by
  hoisting.
- Add body specs for CRLF input, a trailing comment on a single line, `//`
  inside a string value, and a numeric separator.

`decomment` stays declared for now because the script runtimes on main still
import it; that removal lands with fix/decomment-crlf-bug, after which the
dependency can be dropped.
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between b13fe92 and 2405898.

⛔ Files ignored due to path filters: 1
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing: 24
  • packages/bruno-cli/package.json
  • packages/bruno-cli/src/runner/prepare-request.js
  • packages/bruno-cli/src/runner/run-single-request.js
  • packages/bruno-cli/src/utils/collection.js
  • packages/bruno-cli/tests/runner/prepare-request.spec.js
  • packages/bruno-cli/tests/utils/collection/wrap-and-join-scripts.spec.js
  • packages/bruno-common/src/index.ts
  • packages/bruno-common/src/utils/string.spec.ts
  • packages/bruno-common/src/utils/string.ts
  • packages/bruno-electron/package.json
  • packages/bruno-electron/src/ipc/network/grpc-script-orchestration.js
  • packages/bruno-electron/src/ipc/network/index.js
  • packages/bruno-electron/src/ipc/network/prepare-request.js
  • packages/bruno-electron/src/utils/collection.js
  • packages/bruno-electron/tests/network/prepare-request.spec.js
  • packages/bruno-electron/tests/utils/collection.spec.js
  • packages/bruno-js/src/grpc/grpc-script-runtime.js
  • packages/bruno-js/tests/runtime.spec.js
  • tests/scripting/crlf-line-comment/crlf-line-comment.spec.ts
  • tests/scripting/crlf-line-comment/fixtures/collections/crlf-line-comment/01-with-comment.yml
  • tests/scripting/crlf-line-comment/fixtures/collections/crlf-line-comment/02-only-comment.yml
  • tests/scripting/crlf-line-comment/fixtures/collections/crlf-line-comment/bruno.json
  • tests/scripting/crlf-line-comment/fixtures/collections/crlf-line-comment/opencollection.yml
  • tests/scripting/crlf-line-comment/init-user-data/preferences.json

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The CLI and Electron packages replace decomment with jsonc-parser for request-body comments. Script source now passes directly to runtimes, and collection scripts use LF line endings before wrapping. Unit, runtime, and Playwright tests cover these changes.

Changes

Comment handling and script execution

Layer / File(s) Summary
Strip comments from request bodies
packages/bruno-cli/package.json, packages/bruno-cli/src/runner/prepare-request.js, packages/bruno-cli/tests/runner/prepare-request.spec.js, packages/bruno-electron/package.json, packages/bruno-electron/src/ipc/network/prepare-request.js, packages/bruno-electron/tests/network/prepare-request.spec.js
The CLI and Electron request preparers use jsonc-parser to strip comments from JSON bodies and GraphQL variables. Tests cover comment forms, interpolation placeholders, CRLF input, strings containing //, and numeric separators.
Normalize collection script line endings
packages/bruno-common/src/utils/string.ts, packages/bruno-common/src/utils/string.spec.ts, packages/bruno-common/src/index.ts, packages/bruno-cli/src/utils/collection.js, packages/bruno-cli/tests/utils/collection/wrap-and-join-scripts.spec.js, packages/bruno-electron/src/utils/collection.js, packages/bruno-electron/tests/utils/collection.spec.js
normalizeLineEndings converts CRLF and lone-CR endings to LF. The CLI and Electron collection wrappers use it before embedding scripts. Tests cover conversion and request line metadata.
Pass scripts directly to runtimes
packages/bruno-cli/src/runner/run-single-request.js, packages/bruno-electron/src/ipc/network/index.js, packages/bruno-electron/src/ipc/network/grpc-script-orchestration.js, packages/bruno-js/src/grpc/grpc-script-runtime.js, packages/bruno-js/tests/runtime.spec.js, packages/bruno-electron/tests/utils/collection.spec.js, tests/scripting/crlf-line-comment/*
The CLI and Electron runners pass script source to runtimes without comment preprocessing. Runtime and integration tests cover comments in request, response, test, and gRPC scripts, including CRLF input and comment-only scripts.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: lohit-bruno

Merge Risk: ⚪ Minimal · up to 24058

The comment-handling change is mergeable after normal checks; no actionable issue remains identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the scripting CRLF fix and the replacement of decomment with jsonc-parser, which are the main changes in the pull request.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 17 files. (7 skipped: 7 unsupported.)

✨ Finishing Touches: 💡 1
⚔️ Resolve merge conflicts: 💡
  • Resolve merge conflict in branch feat/replace-decomment-with-jsonc-parser
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

CRLF turns to LF,
JSON comments slip away,
Scripts reach runtimes whole,
Tests trace each line and case,
Two runners share the path.

Comment @coderabbitai help to get the list of available commands.

Scripts never needed comment stripping: QuickJS and the Node VM ignore
comments natively, and every wrapper puts a newline before its closing brace.
Running `decomment` over the merged script was also the cause of the
`expecting ')'` failure on mixed CRLF/LF input, since it picks one line ending
for the whole text by majority vote.

- Pass pre-request, post-response, test and gRPC hook scripts straight to the
  runtime in both the app and the CLI.
- Remove `decomment` from both manifests and the lockfile. Its only dependency,
  esprima 4, remains solely as dev-time tooling under bruno-app.

This overlaps with fix/decomment-crlf-bug on the three script files; the edits
are identical, so whichever lands second rebases cleanly.
@pull-request-size pull-request-size Bot added size/L and removed size/M labels Oct 22, 2026
@abhishekp-bruno
abhishekp-bruno force-pushed the feat/replace-decomment-with-jsonc-parser branch from 47711f3 to 0d14809 Compare October 22, 2026 13:52
@abhishekp-bruno abhishekp-bruno changed the title feat(request): strip JSON body comments with jsonc-parser instead of … fix(scripting): CRLF bug replace decomment with jsonc-parser Oct 23, 2026
@abhishekp-bruno
abhishekp-bruno marked this pull request as ready for review October 23, 2026 11:22
@abhishekp-bruno
abhishekp-bruno requested a review from a team October 23, 2026 11:22
return '';
}

// wrapAndJoinScripts counts lines by splitting on \n, so every segment must be LF.

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.

rm comment

});

describe('CLI wrapAndJoinScripts: CR-only line endings', () => {
test('counts lines of a CR-only collection script so the request segment maps to the right lines', () => {

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.

Add CRLF, LF cases too like

CRLF -> LF
LF -> LF unchnaged

await expect(runtime.runRequestScript(merge('// Set the pet ID before sending the request'), {}, {}, {}, '.', onConsoleLog, process.env)).resolves.toBeDefined();
});

it('runs the same scripts in the Node VM', async () => {

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.

Why don't we have the ['quickjs', 'nodevm'] here too? Also I guess we don't have to test post-response and tests, i guess, we just need to make sure that the scripts is running without crashing! Running prerequestscript is more than enough in that case

@sanish-bruno

Copy link
Copy Markdown
Collaborator

@abhishekp-bruno I do not see any converter changes! aren't we supposed to do the normalization for postman imports too?

@sanish-bruno

sanish-bruno commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

@abhishekp-bruno can we check how the error formatter works, I guess it may also need some normalization, so that we can accurately point to the exact line from which the error is thrown, packages/bruno-js/src/utils/error-formatter.js getSourceContextFromContent, Error mapping consumes the raw script, if i am not wrong! especially for lone CR line ending, we will normalize then to \n during the normalization, but the error formatter never sees the normalized \n as it takes the raw script! Having lone CR endings are highly unlikely! But as we are handling them elsewhere, we need to handle them here too for consistency!

@sanish-bruno

Copy link
Copy Markdown
Collaborator

@abhishekp-bruno we will have to be careful replacing decomment with jsonc-parser, decomment is js aware, but jsonc-parser is understand json but it can have issues with JS code. Although we are not using this for scripts!

@abhishekp-bruno

abhishekp-bruno commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Since jsonc-parser is made purely for JSON and does not handle single quotes will not be going ahead with this approach.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants