gradle/gradle · error · ResourceException
Could not write %s content to %s.
Error message
Could not write %s content to %s.
What it means
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>".
Source
Thrown at platforms/core-configuration/file-operations/src/main/java/org/gradle/api/internal/resources/StringBackedTextResource.java:67
}
@Override
public String asString() {
return string;
}
@Override
public Reader asReader() {
return new StringReader(string);
}
@Override
public File asFile(String charset) {
File file = tempFileProvider.createTemporaryFile("string", ".txt", "resource");
try {
Files.asCharSink(file, Charset.forName(charset)).write(string);
} catch (IOException e) {
throw new ResourceException("Could not write " + getDisplayName() + " content to " + file + ".", e);
}
return file;
}
@Override
public File asFile() {
return asFile(Charset.defaultCharset().name());
}
@Override
public TaskDependency getBuildDependencies() {
return TaskDependencyInternal.EMPTY;
}
@Override
public Object getInputProperties() {
return string;
}View on GitHub (pinned to 534f27719b)
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.
Example fix
// before
def f = resources.text.fromString(manifestText).asFile()
// after
def f = layout.buildDirectory.file('gen/manifest.xml').get().asFile()
f.parentFile.mkdirs()
f.text = manifestText Defensive patterns
Strategy: try-catch
Validate before calling
def tmp = new File(System.getProperty('java.io.tmpdir'))
if (!tmp.directory || !tmp.canWrite()) throw new GradleException("java.io.tmpdir not writable: $tmp") Try / catch
try {
def f = resources.text.fromString(text).asFile()
} catch (org.gradle.api.resources.ResourceException e) {
def f = layout.buildDirectory.file('gen/out.txt').get().asFile()
f.parentFile.mkdirs()
f.text = text
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not write %s content to %s.
- Could not write %s content to %s.
- Cannot create file for char source %s
- Loading a TextResource from an insecure URI, without explici
- Loading a TextResource from an insecure redirect, without ex
AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22).
Data as JSON: /api/errors/a058d4e057ee92fe.
Report an issue: GitHub.