Skip to content

fix(open): chain the byte streams of several files like cat - #19341

Merged
fdncred merged 5 commits into
nushell:mainfrom
dualfroz:fix-open-multiple-files
Nov 23, 2026
Merged

fdncred merged 5 commits into
nushell:mainfrom
dualfroz:fix-open-multiple-files

Conversation

@dualfroz

@dualfroz dualfroz commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

open a.bin b.bin flattened every file's byte stream into the output, so each file came out as 8192 byte chunks that had nothing to do with the content ([8192, 1808, 8192, 8192, 3616] for a 10000 and a 20000 byte file), which broke lines, decode and anything else looking at the text. when every file stays a byte stream (--raw or no from converter) they are now chained into one byte stream like cat, read one after another from a flat list so many files dont nest readers. if all streams share a type it is kept, otherwise it falls back to Unknown. tables from converters are still flattened together like before. tests: open_multiple_files_chains_byte_streams and open_multiple_files_keeps_converter_stream_type.

User-facing changes (Release notes)

open with several files that stay byte streams now returns one byte stream, like cat, instead of 8 KiB chunks:

  • open a.txt b.txt | str length gives the combined length, and | length no longer counts chunks
  • open a.txt b.txt | get 0 doesnt work anymore since the result isnt a list
  • like cat, a file without a trailing newline runs into the next file's first line with lines

for one value per file use [a.txt b.txt] | each { open $in }.

Additional notes

fixes #10027

@fdncred

fdncred commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

We don't want to collect files, we want to stream them. If you want to collect you should | collect.

@dualfroz

Copy link
Copy Markdown
Contributor Author

fair, makes sense to keep it streaming. would you take it if each file stayed its own stream instead of being merged into one, or are the 8k chunks just the expected behaviour here?

@fdncred

fdncred commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

fair, makes sense to keep it streaming. would you take it if each file stayed its own stream instead of being merged into one, or are the 8k chunks just the expected behaviour here?

The 8k chunks are the bug from #10027, not behavior we want. They're read-buffer boundaries with nothing to do with the content, and they break anything downstream that looks at the text. Two copies of a 2000-line file:

> open lines.txt lines.txt | lines | length
4004
> open lines.txt lines.txt | lines | where $it !~ "^line \d+$"
╭───┬──────────╮
│ 0 │ li       │
│ 1 │ ne 923   │
│ 2 │ l        │
│ 3 │ ine 1750 │
│ 4 │ li       │
│ 5 │ ne 923   │
│ 6 │ l        │
│ 7 │ ine 1750 │
╰───┴──────────╯

A list where each file is its own stream can't be built. List items are Values, and a Value can't hold a ByteStream. What I'd take is one byte stream across all the files, like cat. When every output is a ByteStream, chain the file readers with std::io::Read::chain and wrap the result in ByteStream::read(reader, call_span, signals, ByteStreamType::Unknown). It still streams, lines and decode see the real content instead of buffer chunks, and | collect gives you one value. Converted tables can keep flattening like they do now.

So open --raw first.txt second.txt | str length would be 30000. If someone wants one value per file, [first.txt second.txt] | each { open $in } already does that.

@fdncred

fdncred commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

where are we at on this?

@dualfroz

Copy link
Copy Markdown
Contributor Author

hey @fdncred ill get on that on weekend, sorry, completely forgot about it

@dualfroz

Copy link
Copy Markdown
Contributor Author

pushed, several files are chained into one byte stream now like cat, so lines and str length see the real content instead of 8k chunks. merged main in since the branch was behind

@fdncred fdncred left a comment •

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.

Review overview

🟡 Changes recommended

Chaining fixes the 8 KiB chunks. On the two-file cases str length gives 30000, and lines gives 4000 lines with no fragments. With very many files the nested Chain gets quadratically slow and then overflows the stack, and a converter's typed byte stream loses its type.

Findings: 1 High severity · 0 Medium severity · 3 Low severity

Open (4)

Notes from my clanker, checked on fe3bbd7. Built the branch in debug and release. Ran cargo test -p nu-command --test tests -- commands::open (131 passed), plus clippy with CI's flags, cargo fmt --all --check and typos, all clean. Each case also ran on the branch binary and on main 1dc849d. CI hasn't run on this head yet.

…m type

Nesting one Chain per file made reads quadratic and overflowed the stack
with many files, so the readers are now read one after another from a
flat list. When every stream has the same type it is kept, otherwise it
falls back to Unknown. Also adds an example for opening several files.
@dualfroz dualfroz changed the title fix(open): keep each file whole when opening several files fix(open): chain the byte streams of several files like cat Nov 23, 2026
@dualfroz

Copy link
Copy Markdown
Contributor Author

thanks, applied all four. the readers are a flat list now so nothing nests per file, the stream type is kept when all files share it (added a test with your from foo case), theres an example for several files, and the title, description and release note match what it does now. i didnt rerun the 100k+ file case here since it needs a raised open file limit

@fdncred fdncred left a comment •

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.

Review overview

🟡 Changes recommended

All four earlier threads are fixed on 4e2cdc6 and resolved. One thing left: every file's read buffer is allocated before the first read, so memory grows by about 8 KiB per file.

Findings: 0 High severity · 1 Medium severity · 0 Low severity

Open (1)

Notes from my clanker, checked on 4e2cdc6. Built the branch in debug and release. cargo test -p nu-command --test tests -- commands::open gave 132 passed, clippy with CI's flags and cargo fmt --all --check are clean, and CI is green. Peak memory came from /usr/bin/time -l on release builds of main 1dc849d, the branch and the lazy-reader trial. I ran my probe scripts on the trial but not cargo test.

Collecting every reader up front allocated each file's read buffer
before anything was read. The byte streams are kept instead and turned
into readers one at a time.
@dualfroz

Copy link
Copy Markdown
Contributor Author

makes sense, done. it keeps the streams now and only turns the next one into a reader when the previous one runs out, so theres one buffer alive at a time. open tests still pass, i didnt redo the memory numbers here

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 23, 2026
@fdncred
fdncred merged commit 9d87334 into nushell:main Nov 23, 2026
14 checks passed
@fdncred

fdncred commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

open --raw with multiple files splits contents into chunks of 8KB

2 participants