Skip to content

[Console] Refactor isTtySupported() #46971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2022

Conversation

Yurunsoft
Copy link
Contributor

Q A
Branch? 6.2
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

stream_isatty(): https://www.php.net/manual/en/function.stream-isatty.php (PHP 7 >= 7.2.0, PHP 8)

@Yurunsoft Yurunsoft requested a review from chalasr as a code owner July 18, 2022 09:10
@carsonbot carsonbot added this to the 6.2 milestone Jul 18, 2022
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.2 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title Refactor isTtySupported() [Console] Refactor isTtySupported() Jul 18, 2022
if (null === $isTtySupported && \function_exists('proc_open')) {
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
if (null === $isTtySupported) {
$isTtySupported = ('/' === \DIRECTORY_SEPARATOR && stream_isatty(\STDOUT));
Copy link
Member

Choose a reason for hiding this comment

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

IIUC, stream_isatty supports Windows, so do we need the '/' === \DIRECTORY_SEPARATOR part?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Windows does not support echo 1 >/dev/null, so we need '/' === \DIRECTORY_SEPARATOR

Copy link
Member

Choose a reason for hiding this comment

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

I don't get it, sorry. Are you saying that stream_isatty is using echo 1 >/dev/null behind the scenes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry!
Windows does not support echo 1 >/dev/null, so we need '/' === \DIRECTORY_SEPARATOR.
Use stream_isatty() to determine if tty is supported.

Copy link
Member

Choose a reason for hiding this comment

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

but you don't use echo 1 >/dev/null anymore, and stream_isatty supports windows natively. So this check become useless, isn't?

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, I think having $isTtySupported = stream_isatty(\STDOUT); is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getCurrentPosition() does not support in windows.

stream_isatty(\STDOUT) may also return true in windows.

php:

$cursor = new Cursor($output);
var_dump($cursor->getCurrentPosition());

log:

'stty' is not recognized as an internal or external command,
operable program or batch file.
'stty' is not recognized as an internal or external command,
operable program or batch file.

'stty' is not recognized as an internal or external command,
operable program or batch file.
array(2) {
  [0]=>
  NULL
  [1]=>
  NULL
}

@fabpot
Copy link
Member

fabpot commented Jul 21, 2022

Thank you @Yurunsoft.

@fabpot fabpot merged commit ab3d9fc into symfony:6.2 Jul 21, 2022
@jsandfordhughes
Copy link

This PR has broken the isTtySupported specifically in PHPStorm using the built in run window on a Mac M1 Pro Chip.

By manually changing the method to the old code, everything works fine.

How can we debug the issue here?

@Yurunsoft
Copy link
Contributor Author

This PR has broken the isTtySupported specifically in PHPStorm using the built in run window on a Mac M1 Pro Chip.

By manually changing the method to the old code, everything works fine.

How can we debug the issue here?

I run this code in Windows+PHPStorm 2022.3.1+wsl and the result is consistent. Could you test this code?

<?php

declare(strict_types=1);

var_dump((bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes));
var_dump(stream_isatty(\STDOUT));

image

@audiojames
Copy link

Also having this issue, please see screenshot below of your code, the upper is the phpstorm terminal, the lower is the phpstorm run window. Phpstorm 2022.3.1 on 2021 Mac M1 Pro.

Screenshot 2023-02-06 at 16 24 06

@jsandfordhughes
Copy link

Thanks for helping @audiojames

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants