Review main Merged

71606[BUGFIX] Fix new keyword replacement in ContainerBuilder

Benjamin Franzke CR +2 V +2 Normal +199 −1 updated 10/13/21, 16:52

Commit message

With the introduction of dependency injection a workaround
was added that substitutes `new ClassName(…)` with
`GeneralUtility::makeInstanceForDi(ClassName::class, …)
in the compiled container code. This is used to synchronize
container instances with Singleton usage in GeneralUtility.

It has been forgotten that preg_replace() works across multiple
lines – differently to the command line tool `sed` – and
therefore the regular expression that replaces `new` with
`GeneralUtility::makeInstanceForDi` incorrectly matched
new…( accross multiple lines and caused 'new ' in strings
to be replaced by accident. With the introduction of lazy command
in #93174 it became likely that 'new ' is part of command
descriptions and therefore the regular expression is now adapted
to ignore newlines (and actually any whitespace) between
`new` and the opening parenthesis.

Review

  • Code review CR +2 Done
    • Benjamin Franzke +2
    • Simon Gilli +1
    • Stefan Bürk +1
    • Nikita Hovratov +1
  • Verified V +2 Done
    • Benjamin Franzke +2
    • Simon Gilli +1
    • Stefan Bürk +1
    • Nikita Hovratov +1
    • core-ci +1 CI

Issues 3

Files 6

Review log 1618

  1. Benjamin Franzke uploaded patch set 1 10/12/21, 21:21

  2. Simon Gilli commented on patch set 1 V +1 CR +1 10/12/21, 21:37

    Thx for the quick fix!
  3. core-ci voted on patch set 1 V +1 10/12/21, 21:41 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/17997

  4. Wouter Wolters voted on patch set 1 CR +1 10/12/21, 21:42

  5. Stefan Bürk commented on patch set 1 CR -1 10/12/21, 22:01

    Sry, I reverted the preg_replace regex but tests are not failing, thus not covering what this patch try to fix nor which was broken before. Even not covering that service container should be broken as stated in the container. Issue states that new keyword in description breakes the whole DI container. Maybe unit tests are not enough here and should be covered by functional test to cover this ? Sry if it seems to picky.
  6. Benjamin Franzke commented on patch set 1 CR -1 10/13/21, 07:18

    Reply

    No, alright, good you tested that. The test setup is incorrect :\ Will fix.
  7. Benjamin Franzke uploaded patch set 2 10/13/21, 08:46

  8. Benjamin Franzke commented on patch set 2 10/13/21, 08:46

    Reply

    Done
  9. core-ci voted on patch set 2 V +1 10/13/21, 09:07 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/18016

  10. Simon Gilli voted on patch set 2 V +1 CR +1 10/13/21, 09:08

  11. Nikita Hovratov voted on patch set 2 V +1 CR +1 10/13/21, 09:38

  12. Stefan Bürk commented on patch set 2 CR -1 10/13/21, 09:41

    Thanks for adding that test. As now old regexp would fail. And now I could test what I had in mind (which fails now). Having such a command description: ``` Symfony\Component\Console\Command\Command: tags: - name: 'console.command' command: 'test:cmd' description: 'Dummy description including new ClassName() as word' schedulable: false ``` will replace that in the description, changeing the Description text (not breaking the di container as before, but also a follow up bug). It will get replaced and tests gives following: ``` 1) TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ConsoleCommandPassTest::commandRegistrationViaTags Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'Dummy description including new ClassName() as word' +'Dummy description including \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceForDi(ClassName::class) as word' ``` Patch fixing the hard fail/exception, but do not prevent for changing the command description which is not expected.
  13. Benjamin Franzke commented on patch set 2 10/13/21, 10:05

    Reply

    Yup, I know that this fails. Talked to Simon Gilli about that case yesterday as well. I think (and we agreed) that is acceptable given that such strings "new Foobar(" are unlikely to be added – and we'd need a full lexer to properly fix this. And I think that is too much just for this tiny workaround (which will hopefully varnish with v13). Fixing that with regex to exclude string will causes more troubles than it helps. Unfortunatley there is no other way to influence the object creation inside the symfony container.
  14. Simon Gilli commented on patch set 2 10/13/21, 10:08

    Reply

    Agree. You were faster, was writing almost the same.
  15. Stefan Bürk commented on patch set 2 V +1 CR +1 10/13/21, 10:14

    Reply

    Okay, as it do not fails hard, and eventually only mangle the description text - and it was discussed, I can aggree with it. Maybe having an eye on it if there are more cases in the wild we can adjust it further. I was not aware that there were a discussion about it already - just wanted to point the case out for discussion.
  16. Benjamin Franzke commented on patch set 2 V +2 CR +2 10/13/21, 10:21

    Reply

    yup, thx, alright.
  17. Benjamin Franzke merged patch set 3 10/13/21, 10:21

  18. Simon Gilli commented on patch set 3 10/13/21, 16:52

    Reply

    Yes, that was the plan, fixing issues when they appear.