Skip to content

fix: fallback to default width when terminal size is 0 (#17946)#23364

Open
thiago-carbonera wants to merge 1 commit into
pantsbuild:mainfrom
thiago-carbonera:fix/terminal-width-fallback
Open

fix: fallback to default width when terminal size is 0 (#17946)#23364
thiago-carbonera wants to merge 1 commit into
pantsbuild:mainfrom
thiago-carbonera:fix/terminal-width-fallback

Conversation

@thiago-carbonera
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor

@benjyw benjyw left a comment

Choose a reason for hiding this comment

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

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:
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.

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.

)

Signed-off-by: Thiago Riemma Carbonera <thiagoriemma@gmail.com>
@thiago-carbonera thiago-carbonera force-pushed the fix/terminal-width-fallback branch from c2d6242 to 1cbe483 Compare May 25, 2026 13:23
@benjyw
Copy link
Copy Markdown
Contributor

benjyw commented May 26, 2026

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

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.

Running pants help commands in Emacs with pantsd enable causes errors

2 participants