Skip to content

Bug Report: LocalizedException Type Error in DeployPackage.php (PHP 8.2) #40356

@willfreeze

Description

@willfreeze

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

  1. Have a LESS compilation error in any theme (e.g., invalid syntax in a .less file)
  2. 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

  1. __('Compilation from source: ') returns a Magento\Framework\Phrase object
  2. When concatenated with strings ($file->getSourcePath(), etc.), PHP casts it to a string
  3. LocalizedException::__construct() requires a Phrase object as its first argument
  4. 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)$exception

Impact

  • 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

No one assigned

    Labels

    Issue: ready for confirmationReported on 2.4.7-p3Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    Status

    Ready for Confirmation

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions