-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Open
Labels
Issue: ready for confirmationReported on 2.4.7-p3Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it
Description
Bug Report: LocalizedException Type Error in DeployPackage.php (PHP 8.2)
Preconditions
- Magento 2.4.7-p3 Community Edition
- PHP 8.2.x with strict typing enabled
- module-deploy version 100.4.7
Steps to reproduce
- Have a LESS compilation error in any theme (e.g., invalid syntax in a
.lessfile) - Run
php bin/magento setup:static-content:deploy -f
Expected result
The actual LESS compilation error message should be displayed, e.g.:
Compilation from source: /path/to/file.less
[Actual LESS error message]
Actual result
A PHP type error is thrown instead, masking the real error:
Magento\Framework\Exception\LocalizedException::__construct(): Argument #1 ($phrase) must be of type Magento\Framework\Phrase, string given, called in /var/www/html/vendor/magento/module-deploy/Service/DeployPackage.php on line 142
Root Cause
In vendor/magento/module-deploy/Service/DeployPackage.php at line 142, the code throws a LocalizedException with a string instead of a Phrase object:
// Current code (lines 136-142):
try {
$this->processFile($file, $package);
} catch (ContentProcessorException $exception) {
$errorMessage = __('Compilation from source: ')
. $file->getSourcePath()
. PHP_EOL . $exception->getMessage() . PHP_EOL;
$this->errorsCount++;
$this->logger->critical($errorMessage);
$package->deleteFile($file->getFileId());
throw new LocalizedException($errorMessage); // BUG: $errorMessage is a string, not Phrase
}The Problem
__('Compilation from source: ')returns aMagento\Framework\Phraseobject- When concatenated with strings (
$file->getSourcePath(), etc.), PHP casts it to a string LocalizedException::__construct()requires aPhraseobject as its first argument- PHP 8.2 enforces strict type checking, causing the error
Why Other Code Works
Examining other uses of LocalizedException in the same module shows the correct pattern:
// Correct usage in ConsoleLoggerFactory.php:60
throw new LocalizedException(new Phrase("Wrong logger interface specified."));
// Correct usage in SetModeCommand.php:108
throw new LocalizedException(__('The mode can\'t be switched to "%1".', $toMode));Proposed Fix
Change line 142 from:
throw new LocalizedException($errorMessage);To:
throw new LocalizedException(new \Magento\Framework\Phrase($errorMessage));Or alternatively, restructure to use __() properly:
throw new LocalizedException(
__('Compilation from source: %1%2%3', $file->getSourcePath(), PHP_EOL, $exception->getMessage())
);Patch
--- a/Service/DeployPackage.php
+++ b/Service/DeployPackage.php
@@ -139,7 +139,7 @@
$this->errorsCount++;
$this->logger->critical($errorMessage);
$package->deleteFile($file->getFileId());
- throw new LocalizedException($errorMessage);
+ throw new LocalizedException(new \Magento\Framework\Phrase($errorMessage));
} catch (\Exception $exception) {
$this->logger->critical(
'Compilation from source ' . $file->getSourcePath() . ' failed' . PHP_EOL . (string)$exceptionImpact
- Severity: Medium-High
- Affected Versions: Magento 2.4.7-p3 with PHP 8.2+
- User Impact: When a LESS compilation error occurs, users cannot see the actual error message, making debugging very difficult
Workaround
Apply the patch using cweagans/composer-patches:
{
"extra": {
"patches": {
"magento/module-deploy": {
"Fix LocalizedException type error with PHP 8.2": "patches/magento-deploy-localized-exception-fix.patch"
}
}
}
}Environment:
- Magento 2.4.7-p3
- PHP 8.2.29
- Ubuntu 22.04.5 LTS
- module-deploy 100.4.7
Related: This bug was discovered while debugging Amasty JetTheme LESS compilation issues. The type error masked the actual LESS syntax errors.
Metadata
Metadata
Assignees
Labels
Issue: ready for confirmationReported on 2.4.7-p3Indicates original Magento version for the Issue report.Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject itIssue related to Developer Experience and needs help with Triage to Confirm or Reject it
Type
Projects
Status
Ready for Confirmation