composer/composer · error · \InvalidArgumentException
--strict-psr-autoloader mode only works with optimized…
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
In InstallCommand::execute, `--strict-psr-autoloader` (which makes the command exit with code 6 on PSR-4/PSR-0 mapping errors) is only meaningful when the autoloader is dumped in optimized mode. If neither --optimize-autoloader/-o nor --classmap-authoritative/-a is in effect (and config does not enable them), Composer throws InvalidArgumentException because strict checks would run against a non-optimized autoloader and silently never fire.
Solutions
- Add --optimize-autoloader (or -o): `composer install --strict-psr-autoloader --optimize-autoloader`.
- Or use --classmap-authoritative (or -a), which implicitly enables optimization.
- Or set optimize-autoloader / classmap-authoritative in config so the flag has the prerequisite.
Example fix
// before composer install --strict-psr-autoloader // after composer install --strict-psr-autoloader --optimize-autoloader
Defensive patterns
Strategy: validation
Validate before calling
$opts = ['strict-psr-autoloader','optimize-autoloader','classmap-authoritative'];
if (in_array('strict-psr-autoloader', $argv, true)
&& !in_array('optimize-autoloader', $argv, true)
&& !in_array('classmap-authoritative', $argv, true)
&& !in_array(['-o','-a'], array_slice($argv, -2), true)) {
throw new \InvalidArgumentException('strict-psr-autoloader needs optimization');
} Prevention
- Always pair --strict-psr-autoloader with -o/-a in CI configs.
- Centralize the install flags in a Makefile/script so the combo stays correct.
When it happens
Trigger: Running `composer install --strict-psr-autoloader` (or update/dump-autoload with the flag) without also enabling optimization. The check at InstallCommand.php:124 fails when both $optimize and $authoritative are false.
Common situations: CI pipelines that add `--strict-psr-autoloader` to catch PSR violations but forget the required optimizer flag; copying the flag from docs without -o.
Related errors
- A non-empty PSR-4 prefix must end with a namespace…
- PSR-4 autoloading is incompatible with the target-dir…
- psr-4 namespaces must end with a namespace separator…
- --strict-psr-autoloader mode only works with optimized…
- The src folder name " " is invalid. Please add a relative…
AI-assisted analysis of composer/composer@c435d285c9 (2026-08-07).
Data as JSON: /api/errors/8dcd5c3a025967b8.
Report an issue: GitHub.
Appendix: 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 c435d285c9)