You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[p5.js 2.0+ Bug Report]: loadTable() drops the last row of a one-column CSV without a trailing newline #9222
alice\nbob\ncarol\n (with a trailing newline) gives 3 rows, as expected.
A file containing just 42 throws Cannot read properties of undefined (reading 'length'). FES then reports "there was a problem loading your table file. Try checking if the file path is correct", which sends people looking at the wrong thing.
Multi-column files aren't affected: x,y\n1,2 parses fine without a trailing newline.
The cause is in src/io/csv.js, at the end of parse():
// flush the last valueif(ctx.entry.length!==0){valueEnd(ctx);entryEnd(ctx);}
ctx.entry only gets a value when a separator or newline ends it. In a one-column row, the last value is still sitting in ctx.value with ctx.entry empty, so the flush is skipped. parse('a\nb') returns [["a"]], parse('only') and parse('"only"') return [], and parse('a\n\nb') loses the b. For the single-value file, loadTable then does data[0].length on an empty array.
I think the fix is to flush whenever the parser is partway through a value, i.e. if (state !== 0 || ctx.entry.length !== 0). State 2 covers an unquoted value and 4 covers a closed quote. A trailing newline leaves state 0 with an empty entry, so it still doesn't add a phantom row. A separate guard in loadTable for an empty file (data[0] undefined) would make that case return an empty table instead of throwing. I'm happy to open a PR with both and unit tests for parse if this sounds right.
thanks for the detailed writeup @Abhist17 . This is genuine and your fix looks right: single-column files keep their last row, 42 and "only" parse fine, and files with a trailing newline don't get an extra row. The empty file guard in loadTable is worth adding too, since parse('') still returns [] and it affects the header path as well (data.shift() gives undefined columns). So, go ahead with the PR, tests for parse covering these cases would be great !
Thanks @aashu2006. The PR is up at #9237. It includes the empty-file guard in loadTable and parse() tests for those cases (one column, single value plain and quoted, trailing newline, empty string).
Activity
aashu2006 commentedon Oct 2, 2026
thanks for the detailed writeup @Abhist17 . This is genuine and your fix looks right: single-column files keep their last row,
42and"only"parse fine, and files with a trailing newline don't get an extra row. The empty file guard inloadTableis worth adding too, sinceparse('')still returns[]and it affects theheaderpath as well (data.shift()gives undefined columns). So, go ahead with the PR, tests forparsecovering these cases would be great !Keep the last row of a one-column CSV without a trailing newline
Abhist17 commentedon Oct 3, 2026
Thanks @aashu2006. The PR is up at #9237. It includes the empty-file guard in
loadTableandparse()tests for those cases (one column, single value plain and quoted, trailing newline, empty string).