Skip to contentSkip to conversation

CodingGuidelines: document which output goes to stdout vs. stderr

PATCH Superseded Eric Sunshine 5 participants 12 messages Dec 1, 2021 – Dec 2, 2021 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CodingGuidelines: document which output goes to stdout vs. stderr

It has long been practice in this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this idiomatic Unix practice is not necessarily universally understood and may be at odds with other schools of thought, such as the somewhat common one that only error messages should go to stderr, and all other messages to stdout. Let's help newcomers by documenting how stdout and stderr are used on this project.

---
 Documentation/CodingGuidelines | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
Documentation/CodingGuidelines +26 -0
index 711cb9171e..44dd178dc9 100644
@@ -499,6 +499,32 @@ For Python scripts:
  - Where required libraries do not restrict us to Python 2, we try to
    also be compatible with Python 3.1 and later.
 
+
+Program Output
+
+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate
+ output on the stdout stream.
+
+ Not all commands have primary output; this is often true of commands
+ whose main function is to perform an action. Some action commands are
+ silent, whereas others are chatty. An example of a chatty action
+ commands is `git clone` with its "Cloning into '<path>'..." and
+ "Checking connectivity..." status messages which it sends to the
+ stderr stream.
+
+ Error messages are always sent to the stderr stream.
+
+
 Error Messages
 
  - Do not end error messages with a full stop.
Signature · 1 line
2.34.1.75.gabe6bb3905
#1 lore raw
Quoted 38 lines · Eric Sunshine

It has long been practice in this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this idiomatic Unix practice is not necessarily universally understood and may be at odds with other schools of thought, such as the somewhat common one that only error messages should go to stderr, and all other messages to stdout. Let's help newcomers by documenting how stdout and stderr are used on this project.

---
Documentation/CodingGuidelines | 26 ++++++++++++++++++++++++++

1 file changed, 26 insertions(+)
Documentation/CodingGuidelines +15 -0
index 711cb9171e..44dd178dc9 100644
@@ -499,6 +499,32 @@ For Python scripts:
 - Where required libraries do not restrict us to Python 2, we try to
   also be compatible with Python 3.1 and later.
 
+
+Program Output
+
+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate
+ output on the stdout stream.

Documentation/CodingGuidelines For Python scripts: · line 526

Quoted 10 lines
Documentation/CodingGuidelines +10 -0
+
+ Not all commands have primary output; this is often true of commands
+ whose main function is to perform an action. Some action commands are
+ silent, whereas others are chatty. An example of a chatty action
+ commands is `git clone` with its "Cloning into '<path>'..." and
+ "Checking connectivity..." status messages which it sends to the
+ stderr stream.
+
+ Error messages are always sent to the stderr stream.
+

This is not necessarily true in the context of the tests.
We just spoke about this in: https://lore.kernel.org/git/[email protected]/T/#u

I don't think it necessary to bloat this explanation with the test details. But mentioning it as an exception would be good.

#2 lore raw
Quoted 27 lines · Fabian Stelzer
Quoted 11 lines · Eric Sunshine

+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.

Documentation/CodingGuidelines +5 -0
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate

Documentation/CodingGuidelines For Python scripts: · line 525

Quoted 10 lines
Documentation/CodingGuidelines +10 -0
+ output on the stdout stream.
+
+ Not all commands have primary output; this is often true of commands
+ whose main function is to perform an action. Some action commands are
+ silent, whereas others are chatty. An example of a chatty action
+ commands is `git clone` with its "Cloning into '<path>'..." and
+ "Checking connectivity..." status messages which it sends to the
+ stderr stream.
+
+ Error messages are always sent to the stderr stream.

This is not necessarily true in the context of the tests. We just spoke about this in:
https://lore.kernel.org/git/[email protected]/T/#u

Quoted 2 lines

I don't think it necessary to bloat this explanation with the test details. But mentioning it as an exception would be good.

Yep, I tried to be clear about that by repeatedly stating that _command_ output should follow this guideline, where "command" means "Git command". I strongly considered writing "Git command" to be perfectly clear, but figured reviewers would insist that it was redundant to mention "Git".

However, I can certainly change these to say "Git command" if you think it would make the intent clearer, and can update the final point to say:

Error messages from Git commands should always be sent to the stderr stream.

#3 lore raw
Quoted 37 lines · Eric Sunshine
Quoted 27 lines · Fabian Stelzer
Quoted 11 lines · Eric Sunshine

+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.

Documentation/CodingGuidelines +5 -0
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate

Documentation/CodingGuidelines For Python scripts: · line 525

Quoted 10 lines
Documentation/CodingGuidelines +10 -0
+ output on the stdout stream.
+
+ Not all commands have primary output; this is often true of commands
+ whose main function is to perform an action. Some action commands are
+ silent, whereas others are chatty. An example of a chatty action
+ commands is `git clone` with its "Cloning into '<path>'..." and
+ "Checking connectivity..." status messages which it sends to the
+ stderr stream.
+
+ Error messages are always sent to the stderr stream.

This is not necessarily true in the context of the tests. We just spoke about this in:
https://lore.kernel.org/git/[email protected]/T/#u

Quoted 2 lines

I don't think it necessary to bloat this explanation with the test details. But mentioning it as an exception would be good.

Yep, I tried to be clear about that by repeatedly stating that _command_ output should follow this guideline, where "command" means "Git command". I strongly considered writing "Git command" to be perfectly clear, but figured reviewers would insist that it was redundant to mention "Git".

Quoted 6 lines

However, I can certainly change these to say "Git command" if you think it would make the intent clearer, and can update the final point to say:

Error messages from Git commands should always
be sent to the stderr stream.

Maybe it was just because i was working on test-lib stuff earlier that i did not connect a commands output to a git command but basically understood it as all output in git code. Still, I think your addition to the last sentence is a good idea and won't hurt.

Thanks

#4 lore raw
In reply to Eric Sunshine
Quoted 6 lines · Eric Sunshine

However, I can certainly change these to say "Git command" if you think it would make the intent clearer, and can update the final point to say:

Error messages from Git commands should always be sent to the stderr stream.

In an earlier round, I think there was a version without "should" in there, but I am sure we have bugs that do not follow the guideline. The proposed phrasing sounds good to me.

Thanks.

#5 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Eric Sunshine

However, I can certainly change these to say "Git command" if you think it would make the intent clearer, and can update the final point to say:

Error messages from Git commands should always be sent to the stderr stream.

In an earlier round, I think there was a version without "should" in there, but I am sure we have bugs that do not follow the guideline. The proposed phrasing sounds good to me.

Yes, the addition of "should" was intentional, though not for any concrete reason (it just felt appropriate). I don't feel strongly either way.

#6 lore raw
In reply to Eric Sunshine
Quoted 9 lines · Eric Sunshine

It has long been practice in this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this idiomatic Unix practice is not necessarily universally understood and may be at odds with other schools of thought, such as the somewhat common one that only error messages should go to stderr, and all other messages to stdout. Let's help newcomers by documenting how stdout and stderr are used on this project.

I agree with everything you wrote here and below, which I think captures what we want to communicate to folks adding new messages or commands.

I am not quite sure _everyone_ would agree with "this idiomatic Unix practice" above. It does seem to be a matter of taste (it is just that what you wrote very much agrees with my taste :) ). And "idiomatic Unix practice" is probably not to be chatty at all, but I think that has been changing over the years.

So I'm not sure if your commit message is being nicely assertive about its taste, or is being uncharitable to people who may have different tastes (but again, IMHO we should pick a direction and this seems like the best one to me). :)

-Peff

#7 lore raw
Quoted 20 lines · Jeff King
Quoted 9 lines · Eric Sunshine

It has long been practice in this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this idiomatic Unix practice is not necessarily universally understood and may be at odds with other schools of thought, such as the somewhat common one that only error messages should go to stderr, and all other messages to stdout. Let's help newcomers by documenting how stdout and stderr are used on this project.

I agree with everything you wrote here and below, which I think captures what we want to communicate to folks adding new messages or commands.

I am not quite sure _everyone_ would agree with "this idiomatic Unix practice" above. It does seem to be a matter of taste (it is just that what you wrote very much agrees with my taste :) ). And "idiomatic Unix practice" is probably not to be chatty at all, but I think that has been changing over the years.

Quoted 4 lines

So I'm not sure if your commit message is being nicely assertive about its taste, or is being uncharitable to people who may have different tastes (but again, IMHO we should pick a direction and this seems like the best one to me). :)

Thanks for the feedback. I'll tone down the commit message when I reroll to make the patch text spell out "Git command" explicitly so that it's less likely to mislead the reader (as it misled Fabian) into thinking the new guideline applies to all output, including test output.

#8 lore raw
In reply to Eric Sunshine
Quoted 22 lines · Eric Sunshine

It has long been practice in this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this idiomatic Unix practice is not necessarily universally understood and may be at odds with other schools of thought, such as the somewhat common one that only error messages should go to stderr, and all other messages to stdout. Let's help newcomers by documenting how stdout and stderr are used on this project.

---
 Documentation/CodingGuidelines | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
Documentation/CodingGuidelines +0 -0
index 711cb9171e..44dd178dc9 100644
@@ -499,6 +499,32 @@ For Python scripts:
  - Where required libraries do not restrict us to Python 2, we try to
    also be compatible with Python 3.1 and later.

Documentation/CodingGuidelines For Python scripts: · line 510

Quoted 10 lines
Documentation/CodingGuidelines +9 -0
 
+
+Program Output
+
+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.

Is there a case for commenting on whether chatty output may be suppressed if not feeding a terminal, or is that mentioned elsewhere? I often see comments about the isatty() detection.

Quoted 10 lines
Documentation/CodingGuidelines +10 -0
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate
+ output on the stdout stream.
+
+ Not all commands have primary output; this is often true of commands
+ whose main function is to perform an action. Some action commands are
+ silent, whereas others are chatty. An example of a chatty action

Documentation/CodingGuidelines For Python scripts: · line 530

Quoted 10 lines
Documentation/CodingGuidelines +7 -0
+ commands is `git clone` with its "Cloning into '<path>'..." and
+ "Checking connectivity..." status messages which it sends to the
+ stderr stream.
+
+ Error messages are always sent to the stderr stream.
+
+
 Error Messages
 
  - Do not end error messages with a full stop.

Philip

#9 lore raw
Quoted 13 lines · Philip Oakley

Documentation/CodingGuidelines For Python scripts: · line 510

Quoted 8 lines · Eric Sunshine
Documentation/CodingGuidelines +8 -0
+Program Output
+
+ We make a distinction between a command's primary output and output
+ which is merely chatty feedback (for instance, status messages,
+ running transcript, or progress display), as well as error messages.
+ Roughly speaking, a command's primary output is that which one might
+ want to capture to a file or send down a pipe; its chatty output
+ should not interfere with those use-cases.

Is there a case for commenting on whether chatty output may be suppressed if not feeding a terminal, or is that mentioned elsewhere? I often see comments about the isatty() detection.

I don't think I saw any such mention when reading through CodingGuidelines before composing the new text. That's certainly a topic which could be addressed, but I don't plan on adding it to this patch since I don't have any specific idea in mind for how it would be discussed. However, the new "Program Output" section added by this patch seems a good place to add that discussion if someone wants to have a go at it as a separate patch atop this one.

#10 lore raw

PATCH v2 1/1 CodingGuidelines: document which output goes to stdout vs. stderr

In reply to Eric Sunshine

It has long been practice on this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this practice is not necessarily universal; another common practice is to send only error messages to stderr, and all other messages to stdout. Therefore, help newcomers out by documenting how stdout and stderr are used on this project.

---

Changes since v1[*]:

* tone down the commit message (peff)

* replace "command" in the patch body with "Git command" in a few places to make it more clear that the new "Program Output" section is providing guidelines for how Git commands should behave, as opposed to, say, how stdout/stderr should be used in test scripts (fabian)

No interdiff/range-diff since they are too noisy in this case, and the patch is short enough to easily re-read in its entirety.

[*]: https://lore.kernel.org/git/[email protected]/

 Documentation/CodingGuidelines | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
Documentation/CodingGuidelines +27 -0
index 711cb9171e..0e27b5395d 100644
@@ -499,6 +499,33 @@ For Python scripts:
  - Where required libraries do not restrict us to Python 2, we try to
    also be compatible with Python 3.1 and later.
 
+
+Program Output
+
+ We make a distinction between a Git command's primary output and
+ output which is merely chatty feedback (for instance, status
+ messages, running transcript, or progress display), as well as error
+ messages. Roughly speaking, a Git command's primary output is that
+ which one might want to capture to a file or send down a pipe; its
+ chatty output should not interfere with these use-cases.
+
+ As such, primary output should be sent to the standard output stream
+ (stdout), and chatty output should be sent to the standard error
+ stream (stderr). Examples of commands which produce primary output
+ include `git log`, `git show`, and `git branch --list` which generate
+ output on the stdout stream.
+
+ Not all Git commands have primary output; this is often true of
+ commands whose main function is to perform an action. Some action
+ commands are silent, whereas others are chatty. An example of a
+ chatty action commands is `git clone` with its "Cloning into
+ '<path>'..." and "Checking connectivity..." status messages which it
+ sends to the stderr stream.
+
+ Error messages from Git commands should always be sent to the stderr
+ stream.
+
+
 Error Messages
 
  - Do not end error messages with a full stop.
Signature · 1 line
2.34.1.173.g76aa8bc2d0
#11 lore raw
Quoted 9 lines · Eric Sunshine

It has long been practice on this project for a command to emit its primary output to stdout so that it can be captured to a file or sent down a pipe, and to emit "chatty" messages (such as those reporting progress) to stderr so that they don't interfere with the primary output. However, this practice is not necessarily universal; another common practice is to send only error messages to stderr, and all other messages to stdout. Therefore, help newcomers out by documenting how stdout and stderr are used on this project.

Quoted 6 lines

---

Changes since v1[*]:

* tone down the commit message (peff)

:) This looks great to me. Thanks for doing it.

-Peff