1

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$ 

1 Answer 1

2

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.

4
  • thanks for your reply , I get to know file name , can we do further more to get line no of error in a file ? Commented Jun 28, 2023 at 3:48
  • @AmitDwivedi You're welcome. You can add this code if (is_array($token)) { echo PHP_EOL, "Line {$token[2]}: ", PHP_EOL; } before switch ($token[0]) { to print the line. As you have resolved your issue, could you please share with us the root cause of your issue?
    – Tu Van
    Commented Jun 28, 2023 at 7:33
  • 1
    The Root cause as I observed is , I defined a Constant named "NAMESPACE" inside a Api interface file, I resolved it by changing the name of the constant. Commented Jun 28, 2023 at 14:55
  • Cool, thanks for sharing @AmitDwivedi
    – Tu Van
    Commented Jun 28, 2023 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.