Magento 2.4.6 – Namespace Not Defined Properly Error During Compile

adminfatal errormagento2.4.6modulesetup-di-compile

When I am trying to compile, then I am getting Below error:

Namespace not defined properly

you can see proper command running here :

www-data@robert-php-fpm:/var/www/html$ bin/magento setup:di:compile -v
Compilation was started.
Repositories code generation... 1/9 [===>------------------------]  11% < 1 sec 109.0 MiB
In FileClassScanner.php line 201:
                                                              
  [Magento\Setup\Module\Di\Code\Reader\InvalidFileException]  
  Namespace not defined properly                              
                                                              

Exception trace:
  at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php:201
 Magento\Setup\Module\Di\Code\Reader\FileClassScanner->isBracedNamespace() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php:151
 Magento\Setup\Module\Di\Code\Reader\FileClassScanner->extract() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php:89
 Magento\Setup\Module\Di\Code\Reader\FileClassScanner->getClassName() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php:115
 Magento\Setup\Module\Di\Code\Reader\ClassesScanner->extract() at /var/www/html/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php:87
 Magento\Setup\Module\Di\Code\Reader\ClassesScanner->getList() at /var/www/html/setup/src/Magento/Setup/Module/Di/App/Task/Operation/RepositoryGenerator.php:61
 Magento\Setup\Module\Di\App\Task\Operation\RepositoryGenerator->doOperation() at /var/www/html/setup/src/Magento/Setup/Module/Di/App/Task/Manager.php:56
 Magento\Setup\Module\Di\App\Task\Manager->process() at /var/www/html/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php:216
 Magento\Setup\Console\Command\DiCompileCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1040
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:301
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/magento/framework/Console/Cli.php:116
 Magento\Framework\Console\Cli->doRun() at /var/www/html/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /var/www/html/bin/magento:23

setup:di:compile

www-data@robert-php-fpm:/var/www/html$ 

Best Answer

Edit the isBracedNamespace method in the setup/src/Magento/Setup/Module/Di/Code/Reader/FileClassScanner.php file to replace the following code:

throw new InvalidFileException('Namespace not defined properly');

with

throw new InvalidFileException('Namespace not defined properly. File name: ' . $this->filename);

And then recompile the code, it will print the file name related to this error, then you can easily resolve the issue.

Related Topic