Problem/Motivation

This issue I believe is after this update: https://git.drupalcode.org/project/drupal/commit/c8cc155#9013c0bcc39d0f1...

I ran into an issue after upgrading to Drupal 9.1.0, and attempting to run drush cr -y using PHP 7.3.9.

It also happens with drush st and probably all drush commands.

he issue is that two class methods updated in Drupal 9.1.0 are lacking the type hint string that the interface has in the declaration.

  • \Drupal\Core\ProxyClass\File\MimeType\ExtensionMimeTypeGuesser::guessMimeType($path)
  • \Drupal\Core\File\MimeType\MimeTypeGuesser::guessMimeType($path)

They should be updated to match the interface \Symfony\Component\Mime\MimeTypeGuesserInterface::guessMimeType(string $path): ?string

Without this running drush commands you get a PHP fatal error:

PHP Fatal error: Declaration of Drupal\Core\ProxyClass\File\MimeType\ExtensionMimeTypeGuesser::guessMimeType($path): string must be compatible with Symfony\Component\Mime\MimeTypeGuesserInterface::guessMimeType(string $path): ?string in /Users/nathanandersen/dev/mdb/web/core/lib/Drupal/Core/ProxyClass/File/MimeType/ExtensionMimeTypeGuesser.php on line 15

and

PHP Fatal error:  Declaration of Drupal\Core\File\MimeType\MimeTypeGuesser::guessMimeType($path): ?string must be compatible with Symfony\Component\Mime\MimeTypeGuesserInterface::guessMimeType(string $path): ?string in /Users/nathanandersen/dev/mdb/web/core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php on line 14
 [warning] Drush command terminated abnormally.

Steps to reproduce

For me, it was updating to Drupal 9.1.0 using PHP 7.3.9.

The code was updated recently in 9.1.0: #3055193: [Symfony 5] The "Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser" class is deprecated since Symfony 4.3, use "Symfony\Component\Mime\MimeTypes" instead.

Proposed resolution

add "string" declaration.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#28 3190265-28.patch1.27 KBpieterdc
#2 3190265-2.patch1.28 KBoknate

Comments

oknate created an issue. See original summary.

oknate’s picture

StatusFileSize
new1.28 KB

This patch fixes the issue for me.

oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
larowlan’s picture

Are you using drupal/core-recommended?

It could be your symfony version is greater than ours

oknate’s picture

I'm not using drupal/core-recommended. Is that the way I should be doing it, instead of using drupal/core directly?

My version of symfony/mime is v5.1.2.

drupal/core-recommended has

"symfony/mime": "v5.1.8",

https://github.com/drupal/core-recommended/blob/9.1.0/composer.json

The code looks the same for the interface in these two versions:

https://github.com/symfony/mime/blob/v5.1.2/MimeTypeGuesserInterface.php
https://github.com/symfony/mime/blob/v5.1.8/MimeTypeGuesserInterface.php

It has the string type declaration, while the core methods that implement it does not.

oknate’s picture

Title: guessMimeType declaration not compatible with Symfony class » guessMimeType declaration not compatible with Symfony interface

I switched to use drupal/core-recommended (9.1.0 tag) and the same error occurs (without the patch).

larowlan’s picture

Thanks, sounds like an issue then

larowlan’s picture

Status: Active » Needs review
oknate’s picture

Note:
Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser has the correct implementation:
public function guessMimeType(string $path): ?string {
So Drupal\Core\File\MimeType\MimeTypeGuesser which also implements the interface not adding the type hint "string" in the ::guessMimeType declaration is inconsistent.
The same goes for Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser.

oknate’s picture

Issue summary: View changes
_kom__’s picture

I had the same issue after attempt to deploy on production site (SSH-console running under PHP 7.3.10) after update from Drupal 9.0.9 to 9.1.2 and trying to 'drush updb'. There was no such error on local machine under PHP 7.4.9.
Is it due to PHP version running Drush? Will update to 7.4 help?

_kom__’s picture

After update to PHP 7.4.10 version in CLI on production server I've got the same error. Reverting back to D9.0.9. Waiting for applying patch in D9.1 branch.
(It is interesting that I don't see this bug on local machine: its configuration is almost the same as remote production server).

_kom__’s picture

I have 5.1.0 version of symfony/mime in my drupal/core-recommended version 9.0.9. It also contains "string" declaration and there was no such error when I had updated to 9.0.9.

oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
_kom__’s picture

This patch works well for me.

With a small notice: I've had to downgrade PHP version in CLI and Drush startup from 7.4.10 to 7.3.23. Otherwise drush updb starts normally, gives a list of pending updates, but then - there is a lot of errors related to Drush: In ProcessBase.php line 171: Unable to decode output into JSON: Syntax error Error: Call to a member function claimItem() on null in _drush_batch_worker () (line 237 of /home/c/******/vendor/drush/drush/includes/batch.inc) and so on
I think that it is due to complicated relationships between Drush 10.3.6, PHP 7.4 and Drupal 9.1. Something keeps going wrong.

Nevertheless, RTBC +1.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

damienmckenna’s picture

Just hit this with the Backup Migrate tests: https://www.drupal.org/pift-ci-job/2085911

damienmckenna’s picture

FWIW the tests run against PHP 7.1, MySQL 5.5 and Drupal 9.1.x.

longwave’s picture

Are we sure #2 fully fixes the issue? There is a subtle difference in the proxy class:

+  public function guessMimeType(string $path): ?string {

vs

+        public function guessMimeType(string $path): string

ie. the proxy class is missing the question mark that indicates a nullable return type.

longwave’s picture

Status: Needs review » Needs work

If I change the base class only and then rerun the generate-proxy-class.php script as indicated at the top of the file it does add the nullable return type.

Nullable types for proxy classes was fixed in #3156944: Support nullable typehints and return types in ProxyBuilder and as a result support PHP 8 but somehow this difference was missed at that time.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

simon.fryer’s picture

Don't know if this helps anyone, but I hit this issue trying to run `drush updb`, but, in particular, and exactly, by calling the PHP 7.4 binary directly because I was on a shared UAT server with multiple PHP versions through EasyApache.

In the end because my local was not experience any issues matching the exact versions of everything, even down to the exact minor PHP release, I set the default php to 7.4 and remove the call to the 7.4 binary, and the same command that had been failing for a few days, now works.

Unsure why, but that solved my issue without doing anything else.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pieterdc’s picture

StatusFileSize
new1.27 KB

Looks like this issue got partially fixed by #3055193: [Symfony 5] The "Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser" class is deprecated since Symfony 4.3, use "Symfony\Component\Mime\MimeTypes" instead. meanwhile.
But it still lacks the same fix, as pointed out in comment #23, in a similar place, at least in Drupal 9.4
See patch attached.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

joegraduate’s picture

Status: Needs work » Closed (outdated)