Skip to content

[Console] Question helper is behaving differently when inside a Section #47411

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

Closed
bigfoot90 opened this issue Aug 28, 2022 · 10 comments
Closed

Comments

@bigfoot90
Copy link

bigfoot90 commented Aug 28, 2022

Symfony version(s) affected

6.1.4

Description

The question helper is going onto a new line when used inside a Section, therefore section cannot rewrite correctly

How to reproduce

Question helper only

Code
      $io = new SymfonyStyle($input, $output);

      $countdown = 3;
      while ($countdown > 0) {
          $io->write('Lorem ipsum ' . $countdown);
          $random = $io->ask('Press random key');
          $countdown--;
      }

Section helper only

Code
      $section = $output->section();

      $countdown = 3;
      while ($countdown > 0) {
          $section->clear();
          $section->write('Lorem ipsum ' . $countdown);
          $countdown--;
          sleep(1);
      }

Both combined

Code
      $section = $output->section();
      $io = new SymfonyStyle($input, $section);

      $countdown = 3;
      while ($countdown > 0) {
          $section->clear();
          $section->write('Lorem ipsum ' . $countdown);

          $random = $io->ask('Press random key');
          $countdown--;
      }

Expected

Final output should be

Lorem ipsum 1

 Press random key:
 > something

@chalasr
Copy link
Member

chalasr commented Aug 30, 2022

Thanks for the report, looks like a bug indeed. Are you wiling to investigate and try to send a fix?

@bigfoot90
Copy link
Author

I can try

@bigfoot90
Copy link
Author

Sorry I didn't had time to work on this yet. :-(

@maxbeckers
Copy link
Contributor

maxbeckers commented Nov 1, 2022

@bigfoot90 just a question, did you have this problems on a unix or on a windows environment? I can only reproduce this on windows.

/cc @chalasr what I saw on windows is again a lineending bug:

$this->bufferedOutput->write("\n");

Changing this php PHP_EOL would fix it (for windows env)

And affected versions are then symfony 4.4+

@bigfoot90
Copy link
Author

@maxbeckers I'm on Ubuntu system

@maxbeckers
Copy link
Contributor

maxbeckers commented Nov 2, 2022

@bigfoot90 you're right, there was sth wrong in my vendors. And the enter of the input is the problem you mentioned.

@nicolas-grekas
Copy link
Member

#48089 has been reverted in #48681

nicolas-grekas added a commit that referenced this issue Dec 16, 2022
…in section (maxbeckers) (chalasr)

This PR was merged into the 6.1 branch.

Discussion
----------

[Console] Revert "bug #48089  Fix clear line with question in section (maxbeckers)

This reverts commit caffee8, reversing changes made to f14901e.

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | /
| License       | MIT
| Doc PR        | -

The tests added in #48089 break on branch 6.2, and after checking the fix on both 6.1 and 6.2 based on the reproducer from #47411, I feel like the behavior is still not the expected one.
I propose to revert for now, hope we can fix this bug homogeneously in another PR.

Commits
-------

30caa8d Revert "bug #48089 [Console] Fix clear line with question in section (maxbeckers)"
@maxbeckers
Copy link
Contributor

I figured out that the behavior in symfony 6.2 changed here. That's the reason, why it was not possible to merge the fix into 6.2. But the behaviod didn't become better with symfony 6.2 in this cases:
47411-img
Now there are empty lines as well. I'll try to find out, what's happening here.

@chalasr
Copy link
Member

chalasr commented Jan 2, 2023

Thank you 🙏

@maxbeckers
Copy link
Contributor

maxbeckers commented Jan 3, 2023

The new changes of the behavior seem to be related to #37304 ... write('test'.\PHP_EOL) (result: "test\n\n") and writeln('test') (result: "test\n") have now different behavior.

This seems to be related to

parent::doWrite($deleteLastLine ? $lastLine.$message : $message, true);

Because there is for newline always true ... setting this to the parameter $newline seems to fix most of the problem. I could set it to $newline and create a PR, But I don't know the effects of this change, because it will have any reason that it's not $newline for now I guess.

@chalasr do you have any Idea, why it's not $newline there?

chalasr added a commit that referenced this issue Feb 19, 2023
This PR was merged into the 6.2 branch.

Discussion
----------

[Console] fix clear of section with question

| Q             | A
| ------------- | ---
| Branch?       |  6.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #47411
| License       | MIT
| Doc PR        | n/a

This PR fixes the problems to clear a section with a question included.

Example Code:
```
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $section1 = $output->section();
        $io = new SymfonyStyle($input, $section1);

        $output->writeln("foo");

        $countdown = 3;
        while ($countdown > 0) {
            $section1->clear();
            $io->writeln('start ' . $countdown);
            $io->write('foo');
            $io->write(' and bar'.\PHP_EOL);
            $givenAnswer = $io->ask('Dummy question?');
            $section1->write('bar');
            $countdown--;
        }

        return self::SUCCESS;
    }
```

Output loop 1:
![Screenshot 2023-01-06 142630](https://user-images.githubusercontent.com/11738128/211021767-e4109951-0519-4763-bdd0-6504ee875b46.png)

Output loop 1:
![Screenshot 2023-01-06 142653](https://user-images.githubusercontent.com/11738128/211021793-db987c4a-1ac5-422d-a8b9-f6c3f4a23c7f.png)

There was already a fix #48089 to be merged in 6.1, but the problem was that there were some changes in 6.2, so it was not possible to merge it into 6.2.

So this fix is only working for 6.2, but perhaps we could find a solution as well for the older versions. But because of the changes of console it was not possible to find a solution working for all versions.

`@chalasr` this fix is still with the newline always `true`
https://github.com/symfony/symfony/blob/4cf9855debc26e4323429ac8d87f02df582e2893/src/Symfony/Component/Console/Output/ConsoleSectionOutput.php#L181
A change of the newline to `$newline` would change the behavior. Maybe we could change that in symfony 7.

To make it easier to test is here a zip with 2 testcommands in the root and the changed vendors.
[test-48089.zip](https://github.com/symfony/symfony/files/10360423/test-48089.zip)

Commits
-------

f4c5518 [Console] fix clear of section with question
@chalasr chalasr closed this as completed Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants