composer/composer · error · InvalidArgumentException

--strict-psr-autoloader mode only works with optimized autol

Error message

--strict-psr-autoloader mode only works with optimized autoloader, use --optimize-autoloader or --classmap-authoritative if you want a strict return value.

What it means

Thrown by InstallCommand::execute when `--strict-psr-autoloader` is set but neither `--optimize-autoloader` (nor config `optimize-autoloader`) nor `--classmap-authoritative` (nor config `classmap-authoritative`) is enabled. Strict PSR-4 checking only emits warnings while building the classmap, which requires one of those classmap-generating modes; running it without optimization would be a no-op, so Composer refuses.

Source

Thrown at src/Composer/Command/InstallCommand.php:125

        if (!$composer->getLocker()->isLocked() && !HttpDownloader::isCurlEnabled()) {
            $io->writeError('<warning>Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.</warning>');
        }

        $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'install', $input, $output);
        $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);

        $install = Installer::create($io, $composer);

        $config = $composer->getConfig();
        [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input);

        $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
        $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
        $apcuPrefix = $input->getOption('apcu-autoloader-prefix');
        $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader');

        if ($input->getOption('strict-psr-autoloader') && !$optimize && !$authoritative) {
            throw new \InvalidArgumentException('--strict-psr-autoloader mode only works with optimized autoloader, use --optimize-autoloader or --classmap-authoritative if you want a strict return value.');
        }

        $composer->getInstallationManager()->setOutputProgress(!$input->getOption('no-progress'));

        $install
            ->setDryRun($input->getOption('dry-run'))
            ->setDownloadOnly($input->getOption('download-only'))
            ->setVerbose($input->getOption('verbose'))
            ->setPreferSource($preferSource)
            ->setPreferDist($preferDist)
            ->setDevMode(!$input->getOption('no-dev'))
            ->setDumpAutoloader(!$input->getOption('no-autoloader'))
            ->setOptimizeAutoloader($optimize)
            ->setClassMapAuthoritative($authoritative)
            ->setStrictPsrAutoloader($input->getOption('strict-psr-autoloader'))
            ->setApcuAutoloader($apcu, $apcuPrefix)
            ->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input))
            ->setPolicyConfig($this->createPolicyConfig($composer->getConfig(), $input))

View on GitHub (pinned to 6ffc117740)

Solutions

  1. Add `--optimize-autoloader` (or `--classmap-authoritative`) alongside `--strict-psr-autoloader`.
  2. Set `config.optimize-autoloader` or `config.classmap-authoritative` in composer.json so the flag is implied.
  3. In dev, run a separate `composer dump-autoload --optimize --strict-psr` if you only want the check occasionally.

Example fix

// before
$ composer install --strict-psr-autoloader
// after
$ composer install --strict-psr-autoloader --optimize-autoloader
Defensive patterns

Strategy: validation

Validate before calling

// before invoking install, ensure strict implies optimize:
$strict = true;  // from your flags
$optimize = true || (bool) ($config['optimize-autoloader'] ?? false)
           || (bool) ($config['classmap-authoritative'] ?? false);
if ($strict && !$optimize) {
    throw new \LogicException('Add --optimize-autoloader when using --strict-psr-autoloader');
}

Type guard

/** @param array<string,mixed> $flags */
function strictPsrOk(array $flags): bool {
    $strict = $flags['strict-psr-autoloader'] ?? false;
    $opt = ($flags['optimize-autoloader'] ?? false) || ($flags['classmap-authoritative'] ?? false);
    return !$strict || $opt;
}

Try / catch

try {
    $app->run(new StringInput('install --strict-psr-autoloader'));
} catch (\InvalidArgumentException $e) {
    // append --optimize-autoloader and retry
}

Prevention

When it happens

Trigger: Running `composer install --strict-psr-autoloader` (or `composer update` likewise) without also enabling optimization. Also triggered in CI configs that copy `--strict-psr-autoloader` from a guide but drop the optimize flag for speed.

Common situations: Production-style flags used in dev where optimization is intentionally off; config drift after upgrading Composer; misconfigured Docker build step.

Related errors


AI-assisted analysis of composer/composer@6ffc117740 (2026-08-07). Data as JSON: /api/errors/8dcd5c3a025967b8. Report an issue: GitHub.