Magento2: 2.2.1 Cannot set production mode. Error compiling

Created on 8 Nov 2017  路  10Comments  路  Source: magento/magento2

Preconditions

  1. An upgraded installation from 2.2.0 to 2.2.1.

Steps to reproduce

  1. Unfortunately I haven't deployed the production mode in the 2.2.0 version, so I don't know if the error was thrown already
  2. The upgrade from 2.2.0 to 2.2.1 went without any issue, done using composer.
  3. When trying to set the store to production mode, I get an error in the command line

Expected result


Production mode enabled

Actual result

Error in the command line:

[2017-11-08 16:17:55] main.ERROR: /usr/bin/php -f /home/dev/public_html/bin/magento setup:di:compile 2>&1
Compilation was started.
%message% 0/7 [>---------------------------]   0% < 1 sec 54.0 MiB%message% 0/7 [>---------------------------]   0% < 1 sec 54.0 MiBProxies code generation... 0/7 [>---------------------------]   0% < 1 sec 54.0 MiB
Proxies code generation... 1/7 [====>-----------------------]  14% < 1 sec 58.0 MiB
Repositories code generation... 1/7 [====>-----------------------]  14% < 1 sec 58.0 MiBPHP Fatal error:  Cannot use Magento\Framework\App\Helper\Context as Context because the name is already in use in /home/dev/public_html/vendor/magento/module-customer/Model/AttributeChecker.php on line 11 [] []
Cannot Reproduce Clear Description Format is valid

Most helpful comment

Same problem here. Production mode already set.
When I try to run bin/magento setup:di:compile the following error appears:
Fatal error: Cannot use Magento\Framework\App\Helper\Context as Context because the name is already in use in /var/www/html/vendor/magento/module-customer/Model/AttributeChecker.php on line 11

I searched for that script and found this unused include in
vendor/magento/module-customer/Model/AttributeChecker.php:

use Magento\Framework\App\Helper\Context;

LE:
If you comment that line from vendor/magento/module-customer/Model/AttributeChecker.php, the compile process works fine.
Here is a snippet of modified code in AttributeChecker.php which solved the problem for me:

<?php
/**
 * Copyright 漏 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Customer\Model;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Customer\Model\Metadata\AttributeResolver;
//use Magento\Framework\App\Helper\Context;

/**
 * Customer attribute checker.
 */
class AttributeChecker

All 10 comments

Same issue here, upgrading from 2.2.0 to 2.2.1.

@bvboas, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.2.0, 2.2.1

Same problem here. Production mode already set.
When I try to run bin/magento setup:di:compile the following error appears:
Fatal error: Cannot use Magento\Framework\App\Helper\Context as Context because the name is already in use in /var/www/html/vendor/magento/module-customer/Model/AttributeChecker.php on line 11

I searched for that script and found this unused include in
vendor/magento/module-customer/Model/AttributeChecker.php:

use Magento\Framework\App\Helper\Context;

LE:
If you comment that line from vendor/magento/module-customer/Model/AttributeChecker.php, the compile process works fine.
Here is a snippet of modified code in AttributeChecker.php which solved the problem for me:

<?php
/**
 * Copyright 漏 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Customer\Model;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Customer\Model\Metadata\AttributeResolver;
//use Magento\Framework\App\Helper\Context;

/**
 * Customer attribute checker.
 */
class AttributeChecker

Thank you for the work-around. Any idea of the ramifications of this patch? Curious why Magento cannot reproduce. Perhaps there is a 3rd party extension on our installs that is occupying the "context" already which makes this instance throw the error?

@theodorhanu I've commented that line but another error appears.

PHP Fatal error:  Cannot use Magento\Framework\Setup\FilePermissions as FilePermissions because the name is already in use in /home/dev/public_html/setup/src/Magento/Setup/Model/Installer.php on line 25

@tstamplis I had production mode previously enabled. Maybe magento team is testing only for default/development mode

@bvboas I couldn't identify any plausible reason for your issue, but I think you can try to replace the first argument in __construct of \Magento\Setup\Model\Installer with full qualified name. Something like this:

namespace Magento\Setup\Model;
////includes
class Installer
{ 
//additional code here

    public function __construct(
        \Magento\Framework\Setup\FilePermissions $filePermissions,
        Writer $deploymentConfigWriter,
        Reader $deploymentConfigReader,
        \Magento\Framework\App\DeploymentConfig $deploymentConfig,
        ModuleListInterface $moduleList,
        ModuleLoader $moduleLoader,
        AdminAccountFactory $adminAccountFactory,
        LoggerInterface $log,
        ConnectionFactory $connectionFactory,
        MaintenanceMode $maintenanceMode,
        Filesystem $filesystem,
        ObjectManagerProvider $objectManagerProvider,
        Context $context,
        SetupConfigModel $setupConfigModel,
        CleanupFiles $cleanupFiles,
        DbValidator $dbValidator,
        SetupFactory $setupFactory,
        DataSetupFactory $dataSetupFactory,
        \Magento\Framework\Setup\SampleData\State $sampleDataState,
        ComponentRegistrar $componentRegistrar,
        PhpReadinessCheck $phpReadinessCheck
    ) {

I am getting this error in 2.2.1 in Development mode, when visiting the customer/address/new page.

Fatal error: Cannot use Magento\Framework\App\Helper\Context as Context because the name is already in use in /var/www/magento2/vendor/magento/module-customer/Model/AttributeChecker.php on line 11

Commenting out that line use Magento\Framework\App\Helper\Context; also fixed it for me. I think the issue is that there is a Context class at the same directory level (Magento\Customer\Model\Context) which is causing a conflict.

I get also the error:

php bin/magento deploy:mode:set production
Enabled maintenance mode
Config "dev/debug/debug_logging = 0" has been saved.
Starting compilation
Something went wrong while compiling generated code. See the error log for details.
Command returned non-zero exit code:
bin/php -f bin/magento setup:di:compile

php bin/magento setup:di:compile
Compilation was started.
Repositories code generation... 1/7 [====>-----------------------] 14% 3 secs 62.0 MiBPHP Fatal error: Cannot use Magento\Framework\App\HelperContext as Context because the name is already in use in vendor/magento/module-customer/Model/AttributeChecker.php on line 11

Update: That was also a clean magento 2.2.1 Installation and without a upgrade.

Had the same issue with a CLEAN installation of magento 2.1.2 which i upgraded to 2.2.1 using the official upgrade guide. @theodorhanu his workaround works like a charm.

I had the same error with php7.0 but works fine with 7.1, so I think it could relate to PHP version or configuration.

Was this page helpful?
0 / 5 - 0 ratings