Skip to content

to csv/tsv: add --quote and --escape, matching from csv/tsv - #19328

Merged
fdncred merged 4 commits into
nushell:mainfrom
stackedbyaradhya:feat/to-csv-tsv-quote-escape
Nov 20, 2026
Merged

fdncred merged 4 commits into
nushell:mainfrom
stackedbyaradhya:feat/to-csv-tsv-quote-escape

Conversation

@stackedbyaradhya

Copy link
Copy Markdown
Contributor

Description

from csv/from tsv already expose --quote/-q and --escape/-e
flags, wired straight into csv::ReaderBuilder::quote()/.escape(). The
write-side siblings to csv/to tsv only ever exposed
--separator/--noheaders/--columns -- the shared writer helper never
touched csv::WriterBuilder::quote()/.escape() at all, even though the
same csv crate's WriterBuilder supports both symmetrically to
ReaderBuilder. Net effect: you could parse CSV/TSV written with a custom
quote or escape character, but never produce it, so round-tripping
non-default-quoted data was impossible.

This adds the same --quote/--escape flags to to csv and to tsv,
threaded through the shared to_delimited_data helper's two
WriterBuilder construction sites (the materialized-input and streaming-
input paths both build a writer independently).

One thing worth calling out for reviewers: WriterBuilder::escape (unlike
ReaderBuilder's) only has an effect once double_quote is turned off --
by default csv escapes embedded quotes by doubling them, and the
idiosyncratic single-escape-character style is an alternative to that, not
an addition on top of it. So --escape also disables the default
doubled-quote escaping when set, matching the csv crate's own documented
behavior.

User-facing changes (Release notes)

  • Added --quote/-q and --escape/-e flags to to csv and to tsv,
    matching the existing flags on from csv/from tsv, so CSV/TSV output
    can use a custom quote or escape character instead of the default "
    and doubled-quote escaping.

Additional notes

n/a

@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

With --escape, escape characters that are already in the data aren't escaped, so values with a backslash don't survive to csv --escape '\' | from csv --escape '\'. The ASCII check also lets Latin-1 characters through and produces invalid UTF-8.

Findings: 1 High severity · 2 Medium severity · 7 Low severity

Open (10)

Notes from my clanker, checked on 943bea5. Built the branch and ran 18 probes against it (escape round trips, Latin-1 quote/escape, error spans, quote == separator, streaming input), plus the formats::to unit tests and the format_conversions::csv/tsv integration tests.

Comment thread crates/nu-command/src/formats/to/csv.rs Resolved
Comment thread crates/nu-command/src/formats/to/csv.rs Resolved
Comment thread crates/nu-command/src/formats/to/csv.rs Resolved
Comment thread crates/nu-command/src/formats/to/csv.rs Resolved
Comment thread crates/nu-command/src/formats/to/tsv.rs Resolved
@fdncred

fdncred commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

when complete, please reply to each review comment so we can be sure that everything has been fixed

@stackedbyaradhya

Copy link
Copy Markdown
Contributor Author

Done!
Thanks for the review @fdncred

@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

Two small cleanups in delimited.rs. The --escape '"' round trip in the escape thread is still the one that matters.

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

Open (2)

Notes from my clanker, checked on cfcafc2. Built the branch with both suggestions applied: cargo fmt is clean and the format_conversions::csv tests pass.

@fdncred

fdncred commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

where are we at on this?

from csv and from tsv already expose --quote/--escape, wired straight into
csv::ReaderBuilder::quote()/escape(). The write side only ever exposed
--separator/--noheaders/--columns, and the shared writer helper
(ToDelimitedDataArgs, to_delimited_data) never touched
csv::WriterBuilder::quote()/escape() at all, even though the same csv
crate's WriterBuilder supports both symmetrically to ReaderBuilder. Net
effect: you could parse CSV/TSV written with a custom quote or escape
character, but never produce it, so round-tripping non-default-quoted
data was impossible.

Add the same --quote/--escape flags to to csv and to tsv, threaded through
to_delimited_data's two WriterBuilder construction sites (materialized and
streaming paths). Note WriterBuilder::escape (unlike ReaderBuilder's) only
has an effect once double_quote is turned off, so --escape also disables
the default doubled-quote escaping when set, matching the csv crate's own
documented behavior for idiosyncratic (non-doubled) quote escaping.
- Double an escape byte that's already in the data before writing,
  not just the quote byte, so --escape round-trips through from's
  --escape instead of silently dropping the character. Applies to
  both the materialized and streaming writers, and to headers too.
- Check is_ascii() before converting quote/escape/separator to a
  byte, instead of relying on u8::try_from(char), which also accepts
  the Latin-1 range and corrupted the output stream for those.
- Reject quote or escape equal to the separator, since that produces
  output that can't be split back into columns.
- Parse quote/escape as Spanned<char> so a bad value's own span is
  reported instead of the command's head, and so the "single
  character ..." error names the flag instead of always saying
  "separator".
- Build the WriterBuilder once instead of inside both per-row
  closures.
- Fold the now 8-argument to_csv into ToCsv::run and drop the
  (quote, escape) tuple on to_csv so it matches to_tsv's separate
  parameters.
- Add write-side tests for --quote/--escape, including the streaming
  (ListStream) branch, round trips, and the new validation errors.
- Fix the --quote help text and give to tsv a --quote example.
Drop the unneeded raw string literals in two of the new round-trip
and validation tests.
…equals --quote

Per review: use a let chain instead of an expect() on escape_span (it
can't fire today, but AGENTS.md asks for no unwrap/expect outside
tests), and stop write_csv_header's doc comment from pointing at a
review-comment thread instead of explaining itself.

Also caught while re-testing: csv-core's own writer already doubles
every quote byte it writes inside a quoted field once escape is set,
so when the escape character is the same as the quote character,
escape_delimited_field's pre-doubling pass and the writer's own
quote-escaping compounded into 'a""""b' instead of 'a""b'. Skip
the pre-doubling pass in that one case, since the writer's own
quote-escaping already covers it.
@stackedbyaradhya
stackedbyaradhya force-pushed the feat/to-csv-tsv-quote-escape branch from 1871adb to 01242b9 Compare November 20, 2026 09:57
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Nov 20, 2026
@fdncred
fdncred merged commit ca70639 into nushell:main Nov 20, 2026
14 checks passed
@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks

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

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants