{"record":{"id":"6b38ce2f9a6c3173","repo":"composer/composer","slug":"dir-does-not-exist-and-could-not-be-created","errorCode":null,"errorMessage":"${dir} does not exist and could not be created.","messagePattern":"(.+?) does not exist and could not be created\\.","errorType":"exception","errorClass":"UnexpectedValueException","httpStatus":null,"severity":"error","filePath":"src/Composer/Json/JsonFile.php","lineNumber":154,"sourceCode":"     * @return void\n     */\n    public function write(array $hash, int $options = JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)\n    {\n        if ($this->path === 'php://memory') {\n            file_put_contents($this->path, static::encode($hash, $options, $this->indent));\n\n            return;\n        }\n\n        $dir = dirname($this->path);\n        if (!is_dir($dir)) {\n            if (file_exists($dir)) {\n                throw new \\UnexpectedValueException(\n                    realpath($dir).' exists and is not a directory.'\n                );\n            }\n            if (!@mkdir($dir, 0777, true)) {\n                throw new \\UnexpectedValueException(\n                    $dir.' does not exist and could not be created.'\n                );\n            }\n        }\n\n        $retries = 3;\n        while ($retries--) {\n            try {\n                $this->filePutContentsIfModified($this->path, static::encode($hash, $options, $this->indent). ($options & JSON_PRETTY_PRINT ? \"\\n\" : ''));\n                break;\n            } catch (\\Exception $e) {\n                if ($retries > 0) {\n                    usleep(500000);\n                    continue;\n                }\n\n                throw $e;\n            }","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/composer/composer/blob/c435d285c9120efdca35696769c72ea9fdcc0466/src/Composer/Json/JsonFile.php#L136-L172","documentation":"JsonFile::write() throws an UnexpectedValueException when the parent directory does not exist and @mkdir($dir, 0777, true) returns false. mkdir fails silently here (error suppressed with @), so the message reports only that creation failed; the suppressed error usually indicates a permissions issue or a path collision.","triggerScenarios":"Calling write() where dirname($path) is missing and cannot be created — e.g. a parent segment lacks write permission, a component of the path is an existing file, or open_basedir restricts the location.","commonSituations":"vendor/ or its parent not writable by the PHP user (common on shared hosts, Docker with wrong UID, CI running as non-owner); running composer as root with a HOME that maps to an unreadable path; open_basedir restriction; a file blocking the directory creation.","solutions":["Fix ownership/permissions on the parent directory so the PHP user can write and create subdirs: `chown -R <user>:<group> <parent>` and `chmod -R u+w <parent>`.","Ensure you are running composer as the directory owner (not root) or with matching UID in Docker.","Remove any file that conflicts with a needed directory segment.","Check open_basedir / disable functions if on a restricted host."],"exampleFix":"# before (docker, wrong UID)\n$ composer install\n> .../vendor does not exist and could not be created.\n\n# after\n$ chown -R $USER:$USER .\n$ chmod -R u+w .\n$ composer install","handlingStrategy":"validation","validationCode":"$dir = dirname($path);\nif (!is_dir($dir)) {\n    if (file_exists($dir)) {\n        throw new \\RuntimeException(\"$dir is a file, cannot create directory\");\n    }\n    if (!@mkdir($dir, 0777, true)) {\n        throw new \\RuntimeException(\"Cannot create $dir: \" . (error_get_last()['message'] ?? 'permission denied'));\n    }\n}","typeGuard":"function dirIsCreatable(string $dir): bool {\n    if (is_dir($dir)) return true;\n    if (file_exists($dir)) return false;\n    return is_writable(dirname($dir));\n}","tryCatchPattern":"try {\n    $jsonFile->write($data);\n} catch (\\UnexpectedValueException $e) {\n    if (str_ends_with($e->getMessage(), ' does not exist and could not be created.')) {\n        // fix ownership/permissions or open_basedir, then retry\n        throw new \\RuntimeException('Cannot create dir for ' . $path . ': ' . $e->getMessage(), 0, $e);\n    }\n    throw $e;\n}","preventionTips":["Run composer as the directory owner; in Docker, match the container UID to the mounted volume owner.","chmod -R u+w the project root (and vendor) so composer can create needed subdirs.","Ensure the path is inside open_basedir on restricted hosts."],"tags":["json","filesystem","permissions","json-file"],"backgroundTag":null,"analyzedSha":"c435d285c9120efdca35696769c72ea9fdcc0466","analyzedAt":"2026-08-07T18:58:23.525Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}