Skip to content

tty: add missing newline to output#10252

Open
ChrisX101010 wants to merge 1 commit into
uutils:mainfrom
ChrisX101010:fix-tty-missing-newline
Open

tty: add missing newline to output#10252
ChrisX101010 wants to merge 1 commit into
uutils:mainfrom
ChrisX101010:fix-tty-missing-newline

Conversation

@ChrisX101010
Copy link
Copy Markdown

Fixes #10239

Comment thread src/uu/tty/src/tty.rs Outdated
@@ -37,7 +37,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let name = nix::unistd::ttyname(std::io::stdin());

let write_result = match name {
Ok(name) => stdout.write_all_os(name.as_os_str()),
Ok(name) => {
stdout.write_all_os(name.as_os_str())?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(/bin/tty >&- 2>/dev/null); echo $?

This command shows what happens when theres an error in writing to stdout, the error code that GNU is expecting is 3, but by adding that ? it will make it handled and return a 1. You can get around this by doing:

Ok(name) => stdout
    .write_all_os(name.as_os_str())
    .and_then(|_| writeln!(stdout)),

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.

target/debug/tty > /dev/full silently returns 3. But GNU have tty: write error: No space left on device.

@ChrisX101010
Copy link
Copy Markdown
Author

@ChrisDryden can you check now, sir? thank you!

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Jan 15, 2026

Would you add test to check newline (and exit code of >/dev/full if possible ) to
https://github.com/uutils/coreutils/blob/main/tests/by-util/test_tty.rs ? Thankyou.

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Note: The gnu test tests/basenc/bounded-memory is now being skipped but was previously passing.

@ChrisX101010
Copy link
Copy Markdown
Author

@oech3 Hi, sir! Please check now and let me know. Thank you! Have a nice weekend. :)

Comment thread src/uu/tty/src/tty.rs Outdated
if write_result.is_err() || stdout.flush().is_err() {

if let Err(e) = write_result {
eprintln!("tty: write error: {}", e);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why did you change this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@ChrisDryden I removed it because I thought it was redundant (flush was already called in line 37), but I've now restored it to match the original pattern.

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.

I guess we can extract it from OS provided err message.
(Also please cargo fmt)

@ChrisX101010
Copy link
Copy Markdown
Author

I changed it to print the error message before exiting (as requested in the previous feedback about matching GNU tty's 'write error' output).

However, I notice I removed the separate stdout.flush() check. The flush is still being called in the Ok(name) branch (line 37), but I can restore the separate check if you prefer. Should I keep both checks separately?

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 17, 2026

Merging this PR will not alter performance

✅ 309 untouched benchmarks
⏩ 46 skipped benchmarks1


Comparing ChrisX101010:fix-tty-missing-newline (de6e612) with main (26223ad)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@sylvestre
Copy link
Copy Markdown
Contributor

many jobs are failing

@ChrisX101010
Copy link
Copy Markdown
Author

many jobs are failing

I ran tests locally, and everything worked for me. I don't know why these specific jobs keep failing when I fixed the new line issue. There is no other workaround that I know of, GNU panics if something is wrong but it didn't produce an output so I made custom test like OP mentioned and I don't understand why is it failing now.. Can you suggest fix to this?

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Jan 17, 2026

I guess you wrote the test writing to /dev/full based on @naoNao89 wrote one.
When I tried to do same things at different PR to remove panic, it failed.

I guess it is generic test framefork's issue.

@ChrisX101010
Copy link
Copy Markdown
Author

I guess you wrote the test writing to /dev/full based on @naoNao89 wrote one. When I tried to do same things at different PR to remove panic, it failed.

I guess it is generic test framefork's issue.

Thank you, sir. If it's okay on your end, can we close this issue for now and this other issue can be a separate PR?

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Jan 17, 2026

Please keep open and add ignore to the test.

@ChrisX101010
Copy link
Copy Markdown
Author

Please keep open and add ignore to the test.

Understood, sir. Will follow your advice, thank you for the suggestion.

@ChrisX101010 ChrisX101010 force-pushed the fix-tty-missing-newline branch from 2c92d38 to b88708c Compare January 18, 2026 14:48
@ChrisX101010
Copy link
Copy Markdown
Author

@oech3 done, sir.

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)

@sylvestre
Copy link
Copy Markdown
Contributor

Some jobs are failing

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Feb 5, 2026

conflicting.

@ChrisX101010
Copy link
Copy Markdown
Author

conflicting.

What do you suggest to fix this issue?

Thank you in advance, sir. 🙏

Best,
Chris

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Feb 9, 2026

Would you rebase agaist main locally?

@ChrisX101010
Copy link
Copy Markdown
Author

Would you rebase agaist main locally?

Yes, sir. Give me some time I will finish today. 🫡

@oech3
Copy link
Copy Markdown
Contributor

oech3 commented Apr 17, 2026

Do you still want to do this?

@ChrisX101010
Copy link
Copy Markdown
Author

Do you still want to do this?

I will clean up today. Thanks! 🙏 will work on it now. After I finish this one, could you please add me to other issues I wanna practice and improve my Rust skills. 🙂 I will check what is the issue, CI/CD tests I think crashing.

@ChrisX101010
Copy link
Copy Markdown
Author

Do you still want to do this?

I apologize I was away, I had some private stuff to finish.

@ChrisX101010 ChrisX101010 force-pushed the fix-tty-missing-newline branch from b88708c to de6e612 Compare April 17, 2026 19:28
@ChrisX101010
Copy link
Copy Markdown
Author

Do you still want to do this?

I've rebased against main and resolved all conflicts. The performance regression flagged by CodSpeed has been fixed by combining the tty path and newline into a single buffer before writing:

rust
let mut buf = name.as_bytes().to_vec();
buf.push(b'\n');
stdout.write_all(&buf)
This reduces the I/O operations from three to two (one write + one flush), eliminating the 3.26% overhead. All tests pass locally including newline output and /dev/full error handling (exit code 3).

@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/tail-n0f (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/cut/bounded-memory is now passing!

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.

tty: missing newline

4 participants