Skip to content

Warn when piped data is not used - #727

Closed
mathiasrw with Copilot wants to merge 2 commits into
mainfrom
copilot/add-warning-for-piped-data
Closed

mathiasrw with Copilot wants to merge 2 commits into
mainfrom
copilot/add-warning-for-piped-data

Conversation

Copilot AI commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

When data is piped in alongside target files and no flag (such as --replacement-pipe or --replacement-js) indicates how to use it, the piped data is silently ignored. This change introduces warnings as a third message kind alongside info and errors, alerting users to unused piped input.

Summary of Changes

  • Warning Output (src/output.ts):

    • Implemented warn(msg, data) writing to stderr with a 🟡 prefix.
    • Suppressed when --quiet or --quiet-total is set.
    • Does not abort execution, including when --halt or --bail is enabled.
  • Unused Pipe Detection (src/engine.ts):

    • Emits 🟡 Data is piped in, but no flag tells what to do with it. It is not used. in handlePipedData() when files are specified and piped data exists.
    • Omits warning when piped data is explicitly handled via --replacement-pipe, referenced via --replacement-js, dismissed via --void-pipe, or consists only of empty content/single line breaks (\n, \r\n).
  • Documentation (README.md):

    • Removed Error != warning from the future ideas list.
  • Test Suite:

    • Added unit test coverage in test/js/fix/719.test.js covering emission, non-halting behavior, pipe consumption flags, and quiet flag suppression.
    • Added CLI test cases in test/cli/run.sh.

Example

# Before: silently ignores piped input and modifies my.file
cat notes.txt | rexreplace foo bar my.file
my.file

# After: emits warning to stderr without blocking file replacement
cat notes.txt | rexreplace foo bar my.file
 🟡 Data is piped in, but no flag tells what to do with it. It is not used.
my.file

Copilot AI linked an issue Oct 12, 2026 that may be closed by this pull request
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Add warning message for unused piped data Warn when piped data is not used Oct 12, 2026
Copilot AI requested a review from mathiasrw October 12, 2026 17:58
@mathiasrw

Copy link
Copy Markdown
Owner

@copilot Are we able to detect this before we start listening to the pipe so we send the warning but simley ignore the content and dont load it into mem?

@mathiasrw mathiasrw left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Comment thread src/engine.ts
Comment on lines +165 to +166
config.pipedData !== '\n' &&
config.pipedData !== '\r\n'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Why this? what is the reasoning?

Copilot AI commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Are we able to detect this before we start listening to the pipe so we send the warning but simley ignore the content and dont load it into mem?

Yes. In src/cli.ts, before attaching listeners to process.stdin, we can check if !process.stdin.isTTY, files are specified (config.includeGlob.length > 0), and no flags will use the pipe (!config.replacementPipe && !config.replacementJs && !config.voidPipe). When met, we can emit the warning and run rexreplace.engine(config) immediately without reading or buffering stdin into memory.

The trade-off is that without reading from the pipe, we cannot inspect the content, so we cannot distinguish between actual data and an empty pipe or single newline (cat /dev/null or printf '\n'); any redirected/piped stdin would trigger the warning. If we still want to ignore empty/newline-only input without loading the entire stream into memory, we would need to read just the first chunk to check it and then discard the rest of the stream.

Copilot AI commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

@copilot

Regarding lines 165-166 (config.pipedData !== '\n' && config.pipedData !== '\r\n'):

This follows the behavior on the reference v8.0 branch ('' !== pipeData && '\n' !== pipeData), mentioned in the issue description under "To decide: Empty piped data, or only a line break: is that also a warning? On the v8.0 branch it was not."

Commands like echo "" frequently produce a trailing newline (\n or \r\n on Windows) even when no meaningful content was intended to be piped. Treating a lone newline the same as empty input avoids false-positive warnings in those cases.

If you would prefer to warn on single line breaks as well, let me know and I can remove those conditions.

@mathiasrw

Copy link
Copy Markdown
Owner

Closing, as this is covered elsewhere now.

The idea from the thread about warning before the pipe is read is dropped, to keep the implementation simple. --void-pipe / RR_VOID_PIPE already makes sure the piped data is not loaded.

@mathiasrw mathiasrw closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when piped data is not used

2 participants