composer/composer · warning · InvalidArgumentException
--prefer-source can not be used together with --prefer-insta
Error message
--prefer-source can not be used together with --prefer-install
What it means
Thrown by BaseCommand::getPreferredInstallOptions() when both --prefer-source and --prefer-install are set on the same invocation. --prefer-install is the unified replacement flag and is mutually exclusive with the legacy --prefer-source.
Source
Thrown at src/Composer/Command/BaseCommand.php:347
case 'source':
$preferSource = true;
break;
case 'dist':
$preferDist = true;
break;
case 'auto':
default:
// noop
break;
}
if (!$input->hasOption('prefer-dist') || !$input->hasOption('prefer-source')) {
return [$preferSource, $preferDist];
}
if ($input->hasOption('prefer-install') && is_string($input->getOption('prefer-install'))) {
if ($input->getOption('prefer-source')) {
throw new \InvalidArgumentException('--prefer-source can not be used together with --prefer-install');
}
if ($input->getOption('prefer-dist')) {
throw new \InvalidArgumentException('--prefer-dist can not be used together with --prefer-install');
}
switch ($input->getOption('prefer-install')) {
case 'dist':
$input->setOption('prefer-dist', true);
break;
case 'source':
$input->setOption('prefer-source', true);
break;
case 'auto':
$preferDist = false;
$preferSource = false;
break;
default:
throw new \UnexpectedValueException('--prefer-install accepts one of "dist", "source" or "auto", got '.$input->getOption('prefer-install'));
}View on GitHub (pinned to 6ffc117740)
Solutions
- Remove --prefer-source and keep --prefer-install=source (the modern equivalent).
- Or drop --prefer-install and keep only --prefer-source (legacy).
- Standardize on --prefer-install across scripts.
Example fix
// before composer require pkg --prefer-source --prefer-install=dist // after composer require pkg --prefer-install=source
Defensive patterns
Strategy: validation
Validate before calling
if ($input->getOption('prefer-install') !== null && $input->getOption('prefer-source')) {
throw new \InvalidArgumentException('Use --prefer-install OR --prefer-source, not both');
} Prevention
- Standardize on --prefer-install across all scripts.
- Audit CI configs for legacy --prefer-source/-dist usage.
- Document the mutually-exclusive nature in team runbooks.
When it happens
Trigger: Running a command (install/update/require) with --prefer-source together with --prefer-install=<value>; getPreferredInstallOptions detects --prefer-install set as a string and --prefer-source also true.
Common situations: Copy-pasting flags from old scripts while adding the newer --prefer-install; CI configs accumulated over time; wrapper scripts passing both.
Related errors
- --prefer-dist can not be used together with --prefer-install
- --abandoned must be one of '.implode(', ', ListPolicyConfig:
- Valid composer.json and composer.lock files are required to
- Composer commands can only work with an '.Application::class
- Could not create a Composer\Composer instance, you must inje
AI-assisted analysis of composer/composer@6ffc117740 (2026-08-07).
Data as JSON: /api/errors/89a225cb6293cd18.
Report an issue: GitHub.