diff -Nuar a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php
--- a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php
+++ b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php
@@ -87,6 +87,7 @@ protected function configure()
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         try {
+            $resultCode = \Magento\Framework\Console\Cli::RETURN_SUCCESS;
             $keepGenerated = $input->getOption(self::INPUT_KEY_KEEP_GENERATED);
             $installer = $this->installerFactory->create(new ConsoleLogger($output));
             $installer->updateModulesSequence($keepGenerated);
@@ -97,10 +98,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
                 $importConfigCommand = $this->getApplication()->find(ConfigImportCommand::COMMAND_NAME);
                 $arrayInput = new ArrayInput([]);
                 $arrayInput->setInteractive($input->isInteractive());
-                $importConfigCommand->run($arrayInput, $output);
+                $resultCode = $importConfigCommand->run($arrayInput, $output);
             }

-            if (!$keepGenerated && $this->appState->getMode() === AppState::MODE_PRODUCTION) {
+            if ($resultCode !== \Magento\Framework\Console\Cli::RETURN_FAILURE
+                && !$keepGenerated && $this->appState->getMode() === AppState::MODE_PRODUCTION) {
                 $output->writeln(
                     '<info>Please re-run Magento compile command. Use the command "setup:di:compile"</info>'
                 );
@@ -110,6 +112,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
             return \Magento\Framework\Console\Cli::RETURN_FAILURE;
         }

-        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
+        return $resultCode;
     }
 }
