Skip to content

fix(csv): don't infer fieldsPerRecord from a leading blank or comment line - #7352

Open
fhgffy wants to merge 1 commit into
denoland:mainfrom
fhgffy:csv-fields-per-record-comment
Open

fhgffy wants to merge 1 commit into
denoland:mainfrom
fhgffy:csv-fields-per-record-comment

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 9, 2026

Copy link
Copy Markdown

With fieldsPerRecord: 0 the field count is taken from the first record the parser sees, even when that record is a blank line or a comment that ends up being skipped. The count becomes 0 and the first real row then fails:

parse("\na,b\nc,d", { fieldsPerRecord: 0 });
// SyntaxError: Syntax error on line 2: expected 0 fields but got 2

parse("# note\na,b\nc,d", { comment: "#", fieldsPerRecord: 0 });
// SyntaxError: Syntax error on line 2: expected 0 fields but got 2

CsvParseStream already skips blank lines, but a leading comment line has the same problem there, and with skipFirstRow: true the comment line is taken as the (empty) header:

ReadableStream.from(["# note\na,b\nc,d"])
  .pipeThrough(new CsvParseStream({ comment: "#", skipFirstRow: true }));
// SyntaxError: Syntax error on line 2: The record has 2 fields, but the header has 0 fields

parse() now infers the count from the first non-empty record, and CsvParseStream skips comment lines before the first-row handling. Both examples above now give [["a", "b"], ["c", "d"]] and [{ a: "c", b: "d" }], which matches Go's encoding/csv.

Added test cases to parse_test.ts and parse_stream_test.ts; they fail without the change. deno test -A csv/, deno lint csv/ and deno fmt --check csv/ pass.

@github-actions github-actions Bot added the csv label Oct 9, 2026
@CLAassistant

CLAassistant commented Oct 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.03%. Comparing base (f834d02) to head (142f0c7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7352      +/-   ##
==========================================
- Coverage   95.04%   95.03%   -0.01%     
==========================================
  Files         619      619              
  Lines       52012    52012              
  Branches     9450     9448       -2     
==========================================
- Hits        49434    49431       -3     
- Misses       2031     2033       +2     
- Partials      547      548       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants