{"record":{"id":"39be473437918104","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":"validation","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/6ffc1177404d0c50119c22dde6564a380f4a82c9/src/Composer/Json/JsonFile.php#L136-L172","documentation":"Thrown by JsonFile::write() when the target directory does not exist AND @mkdir($dir, 0777, true) failed (the @ suppresses the warning; the false return triggers the exception). The recursive mkdir could not create the path — usually a permission denial or a parent that is not writable.","triggerScenarios":"Calling write() where dirname($path) is missing and mkdir(..., true) returns false: no write permission on the parent, a read-only filesystem, or a parent path component is a file.","commonSituations":"vendor/ directory not writable (composer running as wrong user); read-only mount; a parent path component occupied by a file; running inside a container with a read-only bind mount; selinux denying directory creation.","solutions":["Ensure the parent of the target directory exists and is writable by the PHP process.","Fix ownership/permissions: `chown -R <user> <parent>` and `chmod -R u+w <parent>`.","Check the filesystem is mounted read-write (`mount | grep <path>`).","Remove any file occupying a parent path component that should be a directory."],"exampleFix":"// before\n$jsonFile->write($data);\n// vendor/composer not creatable (read-only mount)\n\n// after\nchmod -R u+w vendor\n# or fix the container mount to be read-write\n$jsonFile->write($data);","handlingStrategy":"validation","validationCode":"$dir = dirname($path);\nif (!is_dir($dir)) {\n    if (!@mkdir($dir, 0777, true)) {\n        if (!is_writable(dirname($dir))) {\n            throw new \\UnexpectedValueException(\"Cannot create $dir: parent not writable\");\n        }\n        throw new \\UnexpectedValueException(\"Cannot create directory $dir\");\n    }\n}","typeGuard":"function isDirCreatable(string $dir): bool {\n    if (is_dir($dir)) return true;\n    $parent = $dir;\n    while (!is_dir($parent = dirname($parent))) {\n        if (file_exists($parent)) return false;\n    }\n    return is_writable($parent);\n}","tryCatchPattern":"try {\n    $jsonFile->write($data);\n} catch (\\UnexpectedValueException $e) {\n    if (str_contains($e->getMessage(), 'could not be created')) {\n        // fix permissions/ownership/mount, then retry\n    }\n    throw $e;\n}","preventionTips":["Run composer with write access to the project tree.","Ensure the filesystem is mounted read-write.","Pre-create vendor/ and other writable dirs in containers with correct ownership."],"tags":["json","filesystem","write","permissions","mkdir"],"analyzedSha":"6ffc1177404d0c50119c22dde6564a380f4a82c9","analyzedAt":"2026-08-07T00:01:08.491Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}