elastic/elasticsearch · error · GradleException

Cannot generate dependencies json file

Error message

Cannot generate dependencies json file

What it means

GradleException thrown by the Snyk dependency-graph generation task when it cannot write the resolved Gradle dependency graph to its JSON output file. It wraps an IOException from Files.writeString, which uses CREATE + TRUNCATE_EXISTING.

Source

Thrown at build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/snyk/GenerateSnykDependencyGraph.java:86

        projectPath = objectFactory.property(String.class);
        version = objectFactory.property(String.class);
        remoteUrl = objectFactory.property(String.class);
        targetReference = objectFactory.property(String.class);
    }

    @TaskAction
    void resolveGraph() {
        Map<String, Object> payload = generateGradleGraphPayload();
        String jsonOutput = JsonOutput.prettyPrint(JsonOutput.toJson(payload));
        try {
            Files.writeString(
                getOutputFile().getAsFile().get().toPath(),
                jsonOutput,
                StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING
            );
        } catch (IOException e) {
            throw new GradleException("Cannot generate dependencies json file", e);
        }
    }

    private Map<String, Object> generateGradleGraphPayload() {
        Set<ResolvedDependency> firstLevelModuleDependencies = configuration.get()
            .getResolvedConfiguration()
            .getFirstLevelModuleDependencies();
        SnykDependencyGraphBuilder builder = new SnykDependencyGraphBuilder(gradleVersion.get());
        String effectiveProjectPath = projectPath.get();
        builder.walkGraph(
            (effectiveProjectPath.equals(":") ? projectName.get() : effectiveProjectPath),
            version.get(),
            firstLevelModuleDependencies
        );
        return Map.of(
            "meta",
            FIXED_META_DATA,
            "depGraphJSON",

View on GitHub (pinned to db6a809a66)

Solutions

  1. Verify getOutputFile()'s parent directory exists and is writable before the task runs.
  2. Ensure the output file property is configured (not null) in build.gradle.
  3. Check disk space and filesystem permissions on the output location.
  4. Run with --rerun-tasks after clearing any stale/locked output file.

Example fix

// before
Files.writeString(getOutputFile().getAsFile().get().toPath(), jsonOutput, CREATE, TRUNCATE_EXISTING);

// after - guarantee the parent dir exists
File out = getOutputFile().getAsFile().get();
File parent = out.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
    throw new GradleException("Cannot create output directory " + parent);
}
Files.writeString(out.toPath(), jsonOutput, CREATE, TRUNCATE_EXISTING);
Defensive patterns

Strategy: validation

Validate before calling

File out = getOutputFile().getAsFile().get();
File parent = out.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
    throw new GradleException("Cannot create output directory " + parent);
}
if (out.exists() && !out.canWrite()) {
    throw new GradleException("Output file not writable: " + out);
}

Try / catch

try {
    Files.writeString(out.toPath(), jsonOutput, CREATE, TRUNCATE_EXISTING);
} catch (IOException e) {
    throw new GradleException("Cannot generate dependencies json file at " + out, e);
}

Prevention

When it happens

Trigger: getOutputFile() points to a path whose parent directory does not exist, is read-only, is on a full disk, or the process lacks write permission; another process holds an exclusive lock on the file.

Common situations: Output directory was never created via File.mkdirs(); running on a CI agent with a read-only workspace; misconfigured output path property; antivirus or IDE locking the file on Windows.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/8dd4a63007bc6df3. Report an issue: GitHub.