fix: fallback to default width when terminal size is 0 (#17946)#23364
Open
thiago-carbonera wants to merge 1 commit into
Open
fix: fallback to default width when terminal size is 0 (#17946)#23364thiago-carbonera wants to merge 1 commit into
thiago-carbonera wants to merge 1 commit into
Conversation
benjyw
reviewed
May 23, 2026
Contributor
benjyw
left a comment
There was a problem hiding this comment.
Thanks for the fix! If you could add that comment, then I'll run CI.
| def terminal_width(*, fallback: int = 96, padding: int = 2) -> int: | ||
| return shutil.get_terminal_size(fallback=(fallback, 24)).columns - padding | ||
| columns = shutil.get_terminal_size(fallback=(fallback, 24)).columns | ||
| if columns == 0: |
Contributor
There was a problem hiding this comment.
A naive reader might see this as redundant due to the prior use of thefallback argument to get_terminal_size(). So a comment explaining that some limited terminals may report a width of 0 would be great.
c2d6242 to
1cbe483
Compare
Contributor
|
The CI failures are real: The test and lint errors need fixing, and please add an entry in the release notes at docs/notes/2.33.x.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #17946
Problem Description
The bug occurred specifically when running help commands (pants help) within the Emacs editor's eshell terminal. Because it is a more limited environment, eshell incorrectly reported the terminal width as 0 columns.
The function responsible for calculating this width (terminal_width), located in the docutil.py file, used the shutil.get_terminal_size() method to capture the value. Upon receiving the value 0, the function simply subtracted a standard padding of 2. This mathematical calculation resulted in a negative width of -2. Consequently, when the system attempted to format the help text to fit on an impossible -2 column screen, the program suffered a total collapse, triggering the fatal error ValueError: invalid width -2 (must be > 0).
What changed:
Refactored terminal_width in docutil.py to explicitly check if shutil.get_terminal_size().columns returns 0.
If columns == 0 (which happens in limited terminals like Emacs eshell), the function now applies the default fallback value before subtracting the padding, preventing a ValueError with negative widths.
Added a regression test (test_terminal_width_falls_back_when_columns_are_zero) in docutil_test.py to ensure the fallback logic holds via monkeypatching.