Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions crates/bashkit/src/builtins/generated/format_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ pub enum QuotingStyle {
SHELL_ESCAPE,
}

impl QuotingStyle {
/// Stand-in for uucore's `QuotingStyle::always_quote` setter,
/// deliberately a no-op.
///
/// Upstream's `%q` arm became
/// `QuotingStyle::SHELL_ESCAPE.always_quote(has_brace)` so that GNU
/// `printf %q` wraps a brace-bearing argument in single quotes
/// (`'a{b'`). bashkit's `printf` is bash's, and bash backslash-escapes
/// braces instead - `printf %q 'a{b'` is `a\{b` in bash 5.2 - so
/// honouring the flag would make `%q` diverge from the shell bashkit
/// implements. [`locale_aware_escape_name`] routes every shell style to
/// the bash-faithful [`shell_quote`] regardless of the flags carried on
/// the style, so the style is returned unchanged and the vendored call
/// site keeps compiling and keeps bash's output.
///
/// The shim's variants are unit-only for the same reason: it carries
/// the *names* the vendored module uses, not uucore's quoting
/// behaviour.
#[must_use]
pub fn always_quote(self, _always_quote: bool) -> Self {
self
}
}

/// Style the vendored `format` module quotes a rejected numeric argument
/// with before reporting it.
///
Expand Down Expand Up @@ -241,6 +265,30 @@ fn shell_quote(s: &str) -> String {
mod tests {
use super::*;

/// bash backslash-escapes braces in `printf %q` and never switches to
/// single quotes (`printf %q 'a{b'` is `a\{b` in bash 5.2). The
/// vendored `%q` arm now asks for [`QuotingStyle::always_quote`] when
/// the argument carries a brace, so the shim has to ignore the request;
/// honouring it would make bashkit's `%q` quote like GNU `printf`
/// rather than like the shell bashkit implements.
#[test]
fn always_quote_does_not_change_how_braces_are_escaped() {
for input in ["a{b", "a}b", "{}", "{a b}", "plain"] {
let os = OsStr::new(input);
let forced =
locale_aware_escape_name(os, QuotingStyle::SHELL_ESCAPE.always_quote(true));
assert_eq!(
forced,
locale_aware_escape_name(os, QuotingStyle::SHELL_ESCAPE),
"always_quote changed {input:?}" // debug-ok: assert-failure message
);
assert!(
!forced.to_string_lossy().starts_with('\''),
"{input:?} was single-quoted" // debug-ok: assert-failure message
);
}
}

/// Upstream renamed `C_NO_QUOTES` to the `Escape` variant; both select
/// uucore's `CQuoter` with `CQuotesEnforce::None`, and both are what
/// `--quoting-style=escape` resolves to. Pin the equivalence so the two
Expand Down
12 changes: 12 additions & 0 deletions crates/bashkit/src/builtins/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,18 @@ mod tests {
);
}

/// bash backslash-escapes braces in `%q`; it never single-quotes the
/// argument. GNU/uutils `printf %q` switched to single quotes for a
/// brace-bearing argument, so pin bash's spelling here: the vendored
/// `%q` arm asks for that switch and the shim has to keep refusing it.
#[test]
fn quoted_string_escapes_braces_the_way_bash_does() {
assert_eq!(render_printf("%q", &["a{b".into()]).unwrap(), "a\\{b");
assert_eq!(render_printf("%q", &["a}b".into()]).unwrap(), "a\\}b");
assert_eq!(render_printf("%q", &["{}".into()]).unwrap(), "\\{\\}");
assert_eq!(render_printf("%q", &["a{b c".into()]).unwrap(), "a\\{b\\ c");
}

#[test]
fn strips_zero_hex_escapes_at_stdout_boundary() {
assert_eq!(render_printf("a\\x00b", &[]).unwrap(), "ab");
Expand Down
28 changes: 28 additions & 0 deletions knowledge/runtimes/coreutils-args-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,34 @@ Worked example (2026-10): upstream renamed
`CQuotesEnforce::None`, and both are what `--quoting-style=escape`
resolves to — so the shim maps them to one arm.

#### Shim refuses upstream behaviour bash does not have

A new uucore API is not always a rename to mirror. When upstream adds
one that encodes *GNU coreutils* behaviour, the shim must provide the
**name** so the vendored call site compiles, and deliberately drop the
**behaviour** when bash disagrees — bashkit's builtins are bash's, and
the drift PR may only touch generated files, so a shim that forwarded
the semantics would silently change a builtin's output.

Worked example (2026-11): upstream's `%q` arm in
`format/spec.rs` became
`QuotingStyle::SHELL_ESCAPE.always_quote(has_brace)`, so GNU
`printf %q` wraps a brace-bearing argument in single quotes (`'a{b'`).
bash backslash-escapes braces instead: `printf %q 'a{b'` is `a\{b` in
bash 5.2. `format_support::QuotingStyle::always_quote` therefore returns
`self` unchanged, which is sound because
`locale_aware_escape_name` routes every shell style to the
bash-faithful `shell_quote` and never consults the flags carried on the
style. The shim's variants stay unit-only for the same reason: it
carries the names the vendored module uses, not uucore's quoting
behaviour.

Two tests pin it:
`format_support::tests::always_quote_does_not_change_how_braces_are_escaped`
(the flag is inert) and
`printf::tests::quoted_string_escapes_braces_the_way_bash_does`
(the resulting `%q` output is bash's).

#### The drift PR cannot fix its own consumers

The `open-pr` job enforces a generated-only diff, so an upstream change
Expand Down
Loading