diff --git a/bin/magento b/bin/magento
index 0f2bc933d586aa6d3d37a86c9cae72f28788edde..9f305ed1fa68d02c7d5277eb6bb4d8f4a9e28273 100755
--- a/bin/magento
+++ b/bin/magento
@@ -28,5 +28,5 @@ try {
         echo "\n\n";
         $e = $e->getPrevious();
     }
-    exit(Cli::RETURN_FAILURE);
+    exit(Magento\Framework\Console\Cli::RETURN_FAILURE);
 }
diff --git a/lib/internal/Magento/Framework/Console/Cli.php b/lib/internal/Magento/Framework/Console/Cli.php
index dca6bbd0da611c355d238e2e40988036a38fa723..bf5fa572d3b487505c3c217c1a999662d70f329f 100644
--- a/lib/internal/Magento/Framework/Console/Cli.php
+++ b/lib/internal/Magento/Framework/Console/Cli.php
@@ -8,7 +8,9 @@ namespace Magento\Framework\Console;
 use Magento\Framework\App\DeploymentConfig;
 use Magento\Framework\App\Filesystem\DirectoryList;
 use Magento\Framework\App\State;
+use Magento\Framework\Code\Generator\Io;
 use Magento\Framework\Composer\ComposerJsonFinder;
+use Magento\Framework\Exception\FileSystemException;
 use Magento\Setup\Model\ObjectManagerProvider;
 use Symfony\Component\Console;
 use Magento\Framework\App\Bootstrap;
@@ -62,8 +64,6 @@ class Cli extends Console\Application
     private $objectManager;
 
     /**
-     * Constructor.
-     *
      * @param string $name the application name
      * @param string $version the application version
      */
@@ -150,17 +150,24 @@ class Cli extends Console\Application
      * Object Manager initialization.
      *
      * @return void
+     * @SuppressWarnings(PHPMD.ExitExpression)
      */
     private function initObjectManager()
     {
-        $params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
-        $params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
+        try {
+            $params = (new ComplexParameter(self::INPUT_KEY_BOOTSTRAP))->mergeFromArgv($_SERVER, $_SERVER);
+            $params[Bootstrap::PARAM_REQUIRE_MAINTENANCE] = null;
+
+            $this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();
 
-        $this->objectManager = Bootstrap::create(BP, $params)->getObjectManager();
+            /** @var ObjectManagerProvider $omProvider */
+            $omProvider = $this->serviceManager->get(ObjectManagerProvider::class);
+            $omProvider->setObjectManager($this->objectManager);
+        } catch (\RuntimeException $exception) {
+            $this->writeGenerationDirectoryReadError();
 
-        /** @var ObjectManagerProvider $omProvider */
-        $omProvider = $this->serviceManager->get(ObjectManagerProvider::class);
-        $omProvider->setObjectManager($this->objectManager);
+            exit(static::RETURN_FAILURE);
+        }
     }
 
     /**
@@ -175,7 +182,11 @@ class Cli extends Console\Application
      */
     private function assertGenerationPermissions()
     {
-        $generationDirectoryAccess = new GenerationDirectoryAccess($this->serviceManager);
+        /** @var GenerationDirectoryAccess $generationDirectoryAccess */
+        $generationDirectoryAccess = $this->objectManager->create(
+            GenerationDirectoryAccess::class,
+            ['serviceManager' => $this->serviceManager]
+        );
         /** @var DeploymentConfig $deploymentConfig */
         $deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
         /** @var State $state */
@@ -186,18 +197,14 @@ class Cli extends Console\Application
             && $state->getMode() !== State::MODE_PRODUCTION
             && !$generationDirectoryAccess->check()
         ) {
-            $output = new \Symfony\Component\Console\Output\ConsoleOutput();
-            $output->writeln(
-                '<error>Command line user does not have read and write permissions on var/generation directory.  Please'
-                . ' address this issue before using Magento command line.</error>'
-            );
+            $this->writeGenerationDirectoryReadError();
 
             exit(static::RETURN_FAILURE);
         }
     }
 
     /**
-     * Checks whether compiler preparation is being prepared.
+     * Checks whether compiler is being prepared.
      *
      * @return void
      * @SuppressWarnings(PHPMD.ExitExpression)
@@ -217,20 +224,30 @@ class Cli extends Console\Application
 
             try {
                 $compilerPreparation->handleCompilerEnvironment();
-            } catch (\Magento\Framework\Exception\FileSystemException $e) {
-                $output = new \Symfony\Component\Console\Output\ConsoleOutput();
-                $output->writeln(
-                    '<error>'
-                    . 'Command line user does not have read and write permissions on var/generation directory.  Please'
-                    . ' address this issue before using Magento command line.'
-                    . '</error>'
-                );
+            } catch (FileSystemException $e) {
+                $this->writeGenerationDirectoryReadError();
 
                 exit(static::RETURN_FAILURE);
             }
         }
     }
 
+    /**
+     * Writes read error to console.
+     *
+     * @return void
+     */
+    private function writeGenerationDirectoryReadError()
+    {
+        $output = new Console\Output\ConsoleOutput();
+        $output->writeln(
+            '<error>'
+            . 'Command line user does not have read and write permissions on ' . Io::DEFAULT_DIRECTORY . ' directory. '
+            . 'Please address this issue before using Magento command line.'
+            . '</error>'
+        );
+    }
+
     /**
      * Retrieves vendor commands.
      *
diff --git a/setup/src/Magento/Setup/Console/CompilerPreparation.php b/setup/src/Magento/Setup/Console/CompilerPreparation.php
index 391e9d64c737fe40d553a9d9a09b5c572b1bd586..b42a2814d631851b5b1ef7b480875065a91ef147 100644
--- a/setup/src/Magento/Setup/Console/CompilerPreparation.php
+++ b/setup/src/Magento/Setup/Console/CompilerPreparation.php
@@ -8,21 +8,29 @@ namespace Magento\Setup\Console;
 use Magento\Framework\App\Bootstrap;
 use Magento\Framework\App\Filesystem\DirectoryList;
 use Magento\Framework\Console\GenerationDirectoryAccess;
+use Magento\Framework\Exception\FileSystemException;
 use Magento\Framework\Filesystem\Driver\File;
 use Magento\Framework\Phrase;
 use Magento\Setup\Console\Command\DiCompileCommand;
 use Magento\Setup\Mvc\Bootstrap\InitParamListener;
 use Symfony\Component\Console\Input\ArgvInput;
+use Zend\ServiceManager\ServiceManager;
 
 class CompilerPreparation
 {
-    /** @var \Zend\ServiceManager\ServiceManager */
+    /**
+     * @var ServiceManager
+     */
     private $serviceManager;
 
-    /** @var ArgvInput */
+    /**
+     * @var ArgvInput
+     */
     private $input;
 
-    /** @var File */
+    /**
+     * @var File
+     */
     private $filesystemDriver;
 
     /**
@@ -31,24 +39,24 @@ class CompilerPreparation
     private $generationDirectoryAccess;
 
     /**
-     * @param \Zend\ServiceManager\ServiceManager $serviceManager
+     * @param ServiceManager $serviceManager
      * @param ArgvInput $input
      * @param File $filesystemDriver
      */
     public function __construct(
-        \Zend\ServiceManager\ServiceManager $serviceManager,
-        \Symfony\Component\Console\Input\ArgvInput $input,
-        \Magento\Framework\Filesystem\Driver\File $filesystemDriver
+        ServiceManager $serviceManager,
+        ArgvInput $input,
+        File $filesystemDriver
     ) {
-        $this->serviceManager   = $serviceManager;
-        $this->input            = $input;
+        $this->serviceManager = $serviceManager;
+        $this->input = $input;
         $this->filesystemDriver = $filesystemDriver;
     }
 
     /**
-     * Determine whether a CLI command is for compilation, and if so, clear the directory
+     * Determine whether a CLI command is for compilation, and if so, clear the directory.
      *
-     * @throws \Magento\Framework\Exception\FileSystemException
+     * @throws FileSystemException if generation directory is read-only
      * @return void
      */
     public function handleCompilerEnvironment()
@@ -69,7 +77,7 @@ class CompilerPreparation
         $compileDirList[] = $directoryList->getPath(DirectoryList::DI);
 
         if (!$this->getGenerationDirectoryAccess()->check()) {
-            throw new \Magento\Framework\Exception\FileSystemException(
+            throw new FileSystemException(
                 new Phrase('Generation directory can not be written.')
             );
         }