Skip to content
Merged
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
33 changes: 32 additions & 1 deletion pkgs/id/src/repl/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,17 @@ pub fn preprocess_repl_line(line: &str) -> Result<ReplInput> {
let output = shell_capture(&left)?;

// Replace - in right side with stdin content marker
let mut new_result = right.replace(" - ", &format!(" __STDIN_CONTENT__:{output} "));
// The newline form comes first: a mid-line placeholder followed
// by a newline (the shape a command substitution expanding to
// "put -" + newline + " name" produces) is not matched by
// " - ", and without this replace the fallback below appends
// the marker at the end, where handle_stdin_content rejects
// the line (#4428; regression from PR #3096, which removed this
// replace along with the bogus trailing ones). The marker keeps
// its trailing newline, preserving the pre-#3096 shape.
let mut new_result = right
.replace(" -\n", &format!(" __STDIN_CONTENT__:{output}\n"))
.replace(" - ", &format!(" __STDIN_CONTENT__:{output} "));
Comment on lines +648 to +649

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid rescanning captured output for placeholders

When the right side uses the newline-form placeholder and the captured shell output contains -, the second replacement modifies the output inserted by the first replacement. For example, captured output a - b becomes a __STDIN_CONTENT__:a - b b inside the content marker. handle_stdin_content in repl/runner.rs then passes that corrupted content to put, silently storing different data. Replace placeholders in a single pass over the original right-hand side so inserted output is never searched again.

Useful? React with 👍 / 👎.


// If no - found, might be at end
if !new_result.contains("__STDIN_CONTENT__") {
Expand Down Expand Up @@ -942,6 +952,27 @@ mod tests {
}
}

#[test]
fn test_preprocess_pipe_newline_midline_placeholder_substitutes() {
// Regression #4428: PR #3096 removed the pipe stage's newline
// replace, so a mid-line placeholder followed by a newline
// produced no marker; the fallback appended the marker at the
// end and handle_stdin_content rejected the line, storing
// nothing. The placeholder must substitute in place, preserving
// the marker-plus-newline shape, yielding the same Ready line
// the space-separated form produces apart from the newline.
// End-to-end form: the substitution expands to "put -\n name".
match preprocess_repl_line("echo hi |> $(printf 'put -\\n name')").unwrap() {
ReplInput::Ready(s) => assert_eq!(s, "put __STDIN_CONTENT__:hi\n name"),
_ => panic!("expected Ready"),
}
// Direct form, without the command-substitution stage.
match preprocess_repl_line("echo hi |> put -\n name").unwrap() {
ReplInput::Ready(s) => assert_eq!(s, "put __STDIN_CONTENT__:hi\n name"),
_ => panic!("expected Ready"),
}
}

#[test]
fn test_preprocess_pipe_without_placeholder_appends() {
match preprocess_repl_line("echo hi |> put name").unwrap() {
Expand Down
Loading