composer/composer · error · RuntimeException
File "%s" cannot be found in the current directory
Error message
File "%s" cannot be found in the current directory
What it means
Thrown in BaseConfigCommand::initialize() when the resolved config file path does not exist on disk (and --global was not used, which would have created it). The sprintf fills in the resolved path so the user can see where Composer looked. This is a RuntimeException at initialization.
Source
Thrown at src/Composer/Command/BaseConfigCommand.php:79
($configFile === 'composer.json' || $configFile === './composer.json')
&& !file_exists($configFile)
&& realpath(Platform::getCwd()) === realpath($this->config->get('home'))
) {
file_put_contents($configFile, "{\n}\n");
}
$this->configFile = new JsonFile($configFile, null, $io);
$this->configSource = new JsonConfigSource($this->configFile);
// Initialize the global file if it's not there, ignoring any warnings or notices
if ($input->getOption('global') && !$this->configFile->exists()) {
touch($this->configFile->getPath());
$this->configFile->write(['config' => new \ArrayObject]);
Silencer::call('chmod', $this->configFile->getPath(), 0600);
}
if (!$this->configFile->exists()) {
throw new \RuntimeException(sprintf('File "%s" cannot be found in the current directory', $configFile));
}
}
/**
* Get the local composer.json, global config.json, or the file passed by the user
*/
protected function getComposerConfigFile(InputInterface $input, Config $config): string
{
return $input->getOption('global')
? ($config->get('home') . '/config.json')
: ($input->getOption('file') ?? Factory::getComposerFile())
;
}
/**
* Get the local auth.json or global auth.json, or if the user passed in a file to use,
* the corresponding auth.json
*/View on GitHub (pinned to 6ffc117740)
Solutions
- Verify the file path exists (ls the directory) and correct the --file value or cd to the project root.
- Run `composer init` first to create a composer.json in the current project.
- If targeting global config, use --global so Composer creates it automatically.
Example fix
// before composer config --file mispelled.json extra.foo bar // after composer config --file composer.json extra.foo bar
Defensive patterns
Strategy: validation
Validate before calling
$file = $input->getOption('file') ?? 'composer.json';
if (!file_exists($file)) {
fwrite(STDERR, "Config file not found: $file — run `composer init` first\n");
exit(1);
} Type guard
function configTargetExists(string $f): bool { return is_file($f) && is_readable($f); } Prevention
- Check file existence/readability before invoking a config command.
- Run `composer init` in new projects to guarantee a composer.json.
When it happens
Trigger: Running `composer config --file does-not-exist.json ...`, or running a config command in a directory that has no composer.json and no explicit --file, where the default composer.json is absent.
Common situations: Wrong working directory (no composer.json present); a typo in the --file path; the file was deleted or never created; running config before `composer init`; paths with spaces or missing on case-sensitive filesystems.
Related errors
- --file and --global can not be combined
- Unable to read '.$json->getPath().' contents.
- Unable to write new '.$json->getPath().' contents.
- You can not combine a setting value with --unset
- There is no '.$matches[1].' repository defined
AI-assisted analysis of composer/composer@6ffc117740 (2026-08-07).
Data as JSON: /api/errors/9441d84355a561ce.
Report an issue: GitHub.