{"record":{"id":"a058d4e057ee92fe","repo":"gradle/gradle","slug":"could-not-write-s-content-to-s-a058d4","errorCode":null,"errorMessage":"Could not write %s content to %s.","messagePattern":"Could not write (.+?) content to (.+?)\\.","errorType":"exception","errorClass":"ResourceException","httpStatus":null,"severity":"error","filePath":"platforms/core-configuration/file-operations/src/main/java/org/gradle/api/internal/resources/StringBackedTextResource.java","lineNumber":67,"sourceCode":"    }\n\n    @Override\n    public String asString() {\n        return string;\n    }\n\n    @Override\n    public Reader asReader() {\n        return new StringReader(string);\n    }\n\n    @Override\n    public File asFile(String charset) {\n        File file = tempFileProvider.createTemporaryFile(\"string\", \".txt\", \"resource\");\n        try {\n            Files.asCharSink(file, Charset.forName(charset)).write(string);\n        } catch (IOException e) {\n            throw new ResourceException(\"Could not write \" + getDisplayName() + \" content to \" + file + \".\", e);\n        }\n        return file;\n    }\n\n    @Override\n    public File asFile() {\n        return asFile(Charset.defaultCharset().name());\n    }\n\n    @Override\n    public TaskDependency getBuildDependencies() {\n        return TaskDependencyInternal.EMPTY;\n    }\n\n    @Override\n    public Object getInputProperties() {\n        return string;\n    }","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/core-configuration/file-operations/src/main/java/org/gradle/api/internal/resources/StringBackedTextResource.java#L49-L85","documentation":"StringBackedTextResource (created by resources.text.fromString) has no backing file, so asFile(charset) materializes the string into a temporary file using Files.asCharSink(file, charset).write(string). An IOException during that write — creation failure, unwritable temp dir, disk full — is reported as ResourceException \"Could not write ... content to <file>\".","triggerScenarios":"resources.text.fromString(someString).asFile() or .asFile(charset) while java.io.tmpdir is unwritable, the disk is full, or the freshly created temp file is locked by another process.","commonSituations":"Generating manifests or descriptors from strings and passing them to file-based APIs; hermetic CI containers with read-only /tmp; agents with exhausted disk quotas.","solutions":["Fix the temp directory (writable, space available).","Write the string yourself to a stable project location instead of relying on the temp file.","Consume the resource as .asString()/.asReader() when a File is not strictly required."],"exampleFix":"// before\ndef f = resources.text.fromString(manifestText).asFile()\n\n// after\ndef f = layout.buildDirectory.file('gen/manifest.xml').get().asFile()\nf.parentFile.mkdirs()\nf.text = manifestText","handlingStrategy":"try-catch","validationCode":"def tmp = new File(System.getProperty('java.io.tmpdir'))\nif (!tmp.directory || !tmp.canWrite()) throw new GradleException(\"java.io.tmpdir not writable: $tmp\")","typeGuard":null,"tryCatchPattern":"try {\n    def f = resources.text.fromString(text).asFile()\n} catch (org.gradle.api.resources.ResourceException e) {\n    def f = layout.buildDirectory.file('gen/out.txt').get().asFile()\n    f.parentFile.mkdirs()\n    f.text = text\n}","preventionTips":["Write generated content to build-directory files instead of relying on temp materialization","Use asString() when only the content matters","Alert on low disk space for containerized CI"],"tags":["gradle","text-resource","string","temp-file","io"],"backgroundTag":"temp-file-write-failed","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}