Skip to content

Commit 79ea70d

Browse files
authored
Fix quoting in to nuon and refactor quoting functions (#14180)
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR fixes the quoting and escaping of column names in `to nuon`. Before the PR, column names with quotes inside them would get quoted, but not escaped: ```nushell > { 'a"b': 2 } | to nuon { "a"b": 2 } > { 'a"b': 2 } | to nuon | from nuon Error: × error when loading nuon text ╭─[entry #1:1:27] 1 │ { "a\"b": 2 } | to nuon | from nuon · ────┬──── · ╰── could not load nuon text ╰──── Error: × error when parsing nuon text ╭─[entry #1:1:27] 1 │ { "a\"b": 2 } | to nuon | from nuon · ────┬──── · ╰── could not parse nuon text ╰──── Error: × error when parsing ╭──── 1 │ {"a"b": 2} · ┬ · ╰── Unexpected end of code. ╰──── > [['a"b']; [2] [3]] | to nuon [["a"b"]; [2], [3]] > [['a"b']; [2] [3]] | to nuon | from nuon Error: × error when loading nuon text ╭─[entry #1:1:32] 1 │ [['a"b']; [2] [3]] | to nuon | from nuon · ────┬──── · ╰── could not load nuon text ╰──── Error: × error when parsing nuon text ╭─[entry #1:1:32] 1 │ [['a"b']; [2] [3]] | to nuon | from nuon · ────┬──── · ╰── could not parse nuon text ╰──── Error: × error when parsing ╭──── 1 │ [["a"b"]; [2], [3]] · ┬ · ╰── Unexpected end of code. ╰──── ``` After this PR, the quote is escaped properly: ```nushell > { 'a"b': 2 } | to nuon { "a\"b": 2 } > { 'a"b': 2 } | to nuon | from nuon ╭─────┬───╮ │ a"b │ 2 │ ╰─────┴───╯ > [['a"b']; [2] [3]] | to nuon [["a\"b"]; [2], [3]] > [['a"b']; [2] [3]] | to nuon | from nuon ╭─────╮ │ a"b │ ├─────┤ │ 2 │ │ 3 │ ╰─────╯ ``` The cause of the issue was that `to nuon` simply wrapped column names in `'"'` instead of calling `escape_quote_string`. As part of this change, I also moved the functions related to quoting (`needs_quoting` and `escape_quote_string`) into `nu-utils`, since previously they were defined in very ad-hoc places (and, in the case of `escape_quote_string`, it was defined multiple times with the same body!). # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> `to nuon` now properly escapes quotes in column names. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> All tests pass, including workspace and stdlib tests. # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
1 parent 3ec76af commit 79ea70d

File tree

12 files changed

+76
-86
lines changed

12 files changed

+76
-86
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nu-cli/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use nu_cmd_base::hook::eval_hook;
22
use nu_engine::{eval_block, eval_block_with_early_return};
3-
use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents};
3+
use nu_parser::{lex, parse, unescape_unquote_string, Token, TokenContents};
44
use nu_protocol::{
55
cli_error::report_compile_error,
66
debugger::WithoutDebug,
@@ -10,7 +10,7 @@ use nu_protocol::{
1010
};
1111
#[cfg(windows)]
1212
use nu_utils::enable_vt_processing;
13-
use nu_utils::perf;
13+
use nu_utils::{escape_quote_string, perf};
1414
use std::path::Path;
1515

1616
// This will collect environment variables from std::env and adds them to a stack.

crates/nu-parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ nu-engine = { path = "../nu-engine", version = "0.99.2" }
1919
nu-path = { path = "../nu-path", version = "0.99.2" }
2020
nu-plugin-engine = { path = "../nu-plugin-engine", optional = true, version = "0.99.2" }
2121
nu-protocol = { path = "../nu-protocol", version = "0.99.2" }
22+
nu-utils = { path = "../nu-utils", version = "0.99.2" }
2223

2324
bytesize = { workspace = true }
2425
chrono = { default-features = false, features = ['std'], workspace = true }

crates/nu-parser/src/deparse.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1+
use nu_utils::escape_quote_string;
2+
13
fn string_should_be_quoted(input: &str) -> bool {
24
input.starts_with('$')
35
|| input
46
.chars()
57
.any(|c| c == ' ' || c == '(' || c == '\'' || c == '`' || c == '"' || c == '\\')
68
}
79

8-
pub fn escape_quote_string(input: &str) -> String {
9-
let mut output = String::with_capacity(input.len() + 2);
10-
output.push('"');
11-
12-
for c in input.chars() {
13-
if c == '"' || c == '\\' {
14-
output.push('\\');
15-
}
16-
output.push(c);
17-
}
18-
19-
output.push('"');
20-
output
21-
}
22-
2310
// Escape rules:
2411
// input argument is not a flag, does not start with $ and doesn't contain special characters, it is passed as it is (foo -> foo)
2512
// input argument is not a flag and either starts with $ or contains special characters, quotes are added, " and \ are escaped (two \words -> "two \\words")

crates/nu-parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod parse_shape_specs;
1111
mod parser;
1212
mod type_check;
1313

14-
pub use deparse::{escape_for_script_arg, escape_quote_string};
14+
pub use deparse::escape_for_script_arg;
1515
pub use flatten::{
1616
flatten_block, flatten_expression, flatten_pipeline, flatten_pipeline_element, FlatShape,
1717
};

crates/nu-test-support/src/macros.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ macro_rules! nu_with_plugins {
235235

236236
use crate::{Outcome, NATIVE_PATH_ENV_VAR};
237237
use nu_path::{AbsolutePath, AbsolutePathBuf, Path, PathBuf};
238+
use nu_utils::escape_quote_string;
238239
use std::{
239240
ffi::OsStr,
240241
process::{Command, Stdio},
@@ -421,21 +422,6 @@ where
421422
Outcome::new(out, err.into_owned(), output.status)
422423
}
423424

424-
fn escape_quote_string(input: &str) -> String {
425-
let mut output = String::with_capacity(input.len() + 2);
426-
output.push('"');
427-
428-
for c in input.chars() {
429-
if c == '"' || c == '\\' {
430-
output.push('\\');
431-
}
432-
output.push(c);
433-
}
434-
435-
output.push('"');
436-
output
437-
}
438-
439425
fn with_exe(name: &str) -> String {
440426
#[cfg(windows)]
441427
{

crates/nu-utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ bench = false
1717
bench = false
1818

1919
[dependencies]
20+
fancy-regex = { workspace = true }
2021
lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] }
2122
log = { workspace = true }
2223
num-format = { workspace = true }
24+
once_cell = { workspace = true }
2325
strip-ansi-escapes = { workspace = true }
2426
serde = { workspace = true }
2527
sys-locale = "0.3"

crates/nu-utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod deansi;
44
pub mod emoji;
55
pub mod filesystem;
66
pub mod locale;
7+
mod quoting;
78
mod shared_cow;
89
pub mod utils;
910

@@ -18,6 +19,7 @@ pub use deansi::{
1819
strip_ansi_likely, strip_ansi_string_likely, strip_ansi_string_unlikely, strip_ansi_unlikely,
1920
};
2021
pub use emoji::contains_emoji;
22+
pub use quoting::{escape_quote_string, needs_quoting};
2123
pub use shared_cow::SharedCow;
2224

2325
#[cfg(unix)]

crates/nu-utils/src/quoting.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use fancy_regex::Regex;
2+
use once_cell::sync::Lazy;
3+
4+
// This hits, in order:
5+
// • Any character of []:`{}#'";()|$,
6+
// • Any digit (\d)
7+
// • Any whitespace (\s)
8+
// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
9+
static NEEDS_QUOTING_REGEX: Lazy<Regex> = Lazy::new(|| {
10+
Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\d\s]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
11+
.expect("internal error: NEEDS_QUOTING_REGEX didn't compile")
12+
});
13+
14+
pub fn needs_quoting(string: &str) -> bool {
15+
if string.is_empty() {
16+
return true;
17+
}
18+
// These are case-sensitive keywords
19+
match string {
20+
// `true`/`false`/`null` are active keywords in JSON and NUON
21+
// `&&` is denied by the nu parser for diagnostics reasons
22+
// (https://github.com/nushell/nushell/pull/7241)
23+
"true" | "false" | "null" | "&&" => return true,
24+
_ => (),
25+
};
26+
// All other cases are handled here
27+
NEEDS_QUOTING_REGEX.is_match(string).unwrap_or(false)
28+
}
29+
30+
pub fn escape_quote_string(string: &str) -> String {
31+
let mut output = String::with_capacity(string.len() + 2);
32+
output.push('"');
33+
34+
for c in string.chars() {
35+
if c == '"' || c == '\\' {
36+
output.push('\\');
37+
}
38+
output.push(c);
39+
}
40+
41+
output.push('"');
42+
output
43+
}

crates/nuon/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ version = "0.99.2"
1313
nu-parser = { path = "../nu-parser", version = "0.99.2" }
1414
nu-protocol = { path = "../nu-protocol", version = "0.99.2" }
1515
nu-engine = { path = "../nu-engine", version = "0.99.2" }
16-
once_cell = { workspace = true }
17-
fancy-regex = { workspace = true }
16+
nu-utils = { path = "../nu-utils", version = "0.99.2" }
1817

1918
[dev-dependencies]
2019
chrono = { workspace = true }

crates/nuon/src/to.rs

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use core::fmt::Write;
2-
use fancy_regex::Regex;
3-
use once_cell::sync::Lazy;
42

53
use nu_engine::get_columns;
6-
use nu_parser::escape_quote_string;
74
use nu_protocol::{Range, ShellError, Span, Value};
5+
use nu_utils::{escape_quote_string, needs_quoting};
86

97
use std::ops::Bound;
108

@@ -129,11 +127,12 @@ fn value_to_string(
129127
let headers: Vec<String> = headers
130128
.iter()
131129
.map(|string| {
132-
if needs_quotes(string) {
133-
format!("{idt}\"{string}\"")
130+
let string = if needs_quoting(string) {
131+
&escape_quote_string(string)
134132
} else {
135-
format!("{idt}{string}")
136-
}
133+
string
134+
};
135+
format!("{idt}{string}")
137136
})
138137
.collect();
139138
let headers_output = headers.join(&format!(",{sep}{nl}{idt_pt}"));
@@ -199,19 +198,15 @@ fn value_to_string(
199198
Value::Record { val, .. } => {
200199
let mut collection = vec![];
201200
for (col, val) in &**val {
202-
collection.push(if needs_quotes(col) {
203-
format!(
204-
"{idt_po}\"{}\": {}",
205-
col,
206-
value_to_string_without_quotes(val, span, depth + 1, indent)?
207-
)
201+
let col = if needs_quoting(col) {
202+
&escape_quote_string(col)
208203
} else {
209-
format!(
210-
"{idt_po}{}: {}",
211-
col,
212-
value_to_string_without_quotes(val, span, depth + 1, indent)?
213-
)
214-
});
204+
col
205+
};
206+
collection.push(format!(
207+
"{idt_po}{col}: {}",
208+
value_to_string_without_quotes(val, span, depth + 1, indent)?
209+
));
215210
}
216211
Ok(format!(
217212
"{{{nl}{}{nl}{idt}}}",
@@ -247,7 +242,7 @@ fn value_to_string_without_quotes(
247242
) -> Result<String, ShellError> {
248243
match v {
249244
Value::String { val, .. } => Ok({
250-
if needs_quotes(val) {
245+
if needs_quoting(val) {
251246
escape_quote_string(val)
252247
} else {
253248
val.clone()
@@ -256,30 +251,3 @@ fn value_to_string_without_quotes(
256251
_ => value_to_string(v, span, depth, indent),
257252
}
258253
}
259-
260-
// This hits, in order:
261-
// • Any character of []:`{}#'";()|$,
262-
// • Any digit (\d)
263-
// • Any whitespace (\s)
264-
// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
265-
static NEEDS_QUOTES_REGEX: Lazy<Regex> = Lazy::new(|| {
266-
Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\d\s]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
267-
.expect("internal error: NEEDS_QUOTES_REGEX didn't compile")
268-
});
269-
270-
fn needs_quotes(string: &str) -> bool {
271-
if string.is_empty() {
272-
return true;
273-
}
274-
// These are case-sensitive keywords
275-
match string {
276-
// `true`/`false`/`null` are active keywords in JSON and NUON
277-
// `&&` is denied by the nu parser for diagnostics reasons
278-
// (https://github.com/nushell/nushell/pull/7241)
279-
// TODO: remove the extra check in the nuon codepath
280-
"true" | "false" | "null" | "&&" => return true,
281-
_ => (),
282-
};
283-
// All other cases are handled here
284-
NEEDS_QUOTES_REGEX.is_match(string).unwrap_or(false)
285-
}

src/command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use nu_engine::{command_prelude::*, get_full_help};
2-
use nu_parser::{escape_for_script_arg, escape_quote_string, parse};
2+
use nu_parser::{escape_for_script_arg, parse};
33
use nu_protocol::{
44
ast::{Expr, Expression},
55
engine::StateWorkingSet,
66
report_parse_error,
77
};
8-
use nu_utils::stdout_write_all_and_flush;
8+
use nu_utils::{escape_quote_string, stdout_write_all_and_flush};
99

1010
pub(crate) fn gather_commandline_args() -> (Vec<String>, String, Vec<String>) {
1111
// Would be nice if we had a way to parse this. The first flags we see will be going to nushell

0 commit comments

Comments
 (0)