From 0bb58c01639a5e441ae54427c8e6653934906f8e Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 21 Aug 2025 11:22:41 -0700 Subject: [PATCH] Fix accessing main files in transport version resources The git commands which output the known files on the main branch always outputs line feeds and uses forward slashes. This commit adjusts the resources service to always account for that. closes #133234 --- .../transport/TransportVersionDefinition.java | 6 +++-- .../transport/TransportVersionLatest.java | 7 ++++-- .../TransportVersionResourcesService.java | 22 +++++++++---------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionDefinition.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionDefinition.java index 327d2f7d632f5..65f4caeb95206 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionDefinition.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionDefinition.java @@ -9,11 +9,13 @@ package org.elasticsearch.gradle.internal.transport; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; record TransportVersionDefinition(String name, List ids) { - public static TransportVersionDefinition fromString(String filename, String contents) { + public static TransportVersionDefinition fromString(Path file, String contents) { + String filename = file.getFileName().toString(); assert filename.endsWith(".csv"); String name = filename.substring(0, filename.length() - 4); List ids = new ArrayList<>(); @@ -23,7 +25,7 @@ public static TransportVersionDefinition fromString(String filename, String cont try { ids.add(TransportVersionId.fromString(rawId)); } catch (NumberFormatException e) { - throw new IllegalStateException("Failed to parse id " + rawId + " in " + filename, e); + throw new IllegalStateException("Failed to parse id " + rawId + " in " + file, e); } } } diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionLatest.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionLatest.java index 072409bdf9698..5f1073a2f573a 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionLatest.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionLatest.java @@ -9,14 +9,17 @@ package org.elasticsearch.gradle.internal.transport; +import java.nio.file.Path; + record TransportVersionLatest(String branch, String name, TransportVersionId id) { - public static TransportVersionLatest fromString(String filename, String contents) { + public static TransportVersionLatest fromString(Path file, String contents) { + String filename = file.getFileName().toString(); assert filename.endsWith(".csv"); String branch = filename.substring(0, filename.length() - 4); String[] parts = contents.split(","); if (parts.length != 2) { - throw new IllegalStateException("Invalid transport version latest file [" + filename + "]: " + contents); + throw new IllegalStateException("Invalid transport version latest file [" + file + "]: " + contents); } return new TransportVersionLatest(branch, parts[0], TransportVersionId.fromString(parts[1])); diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionResourcesService.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionResourcesService.java index b495bcff6f4b2..27d3ab927f732 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionResourcesService.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionResourcesService.java @@ -16,7 +16,6 @@ import org.gradle.process.ExecResult; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -104,7 +103,7 @@ Map getNamedDefinitions() throws IOException /** Get a named definition from main if it exists there, or null otherwise */ TransportVersionDefinition getNamedDefinitionFromMain(String name) { - String resourcePath = getNamedDefinitionRelativePath(name).toString(); + Path resourcePath = getNamedDefinitionRelativePath(name); return getMainFile(resourcePath, TransportVersionDefinition::fromString); } @@ -130,7 +129,7 @@ Map getUnreferencedDefinitions() throws IOEx /** Get a named definition from main if it exists there, or null otherwise */ TransportVersionDefinition getUnreferencedDefinitionFromMain(String name) { - String resourcePath = getUnreferencedDefinitionRelativePath(name).toString(); + Path resourcePath = getUnreferencedDefinitionRelativePath(name); return getMainFile(resourcePath, TransportVersionDefinition::fromString); } @@ -145,7 +144,7 @@ Map getLatestByReleaseBranch() throws IOExceptio try (var stream = Files.list(transportResourcesDir.resolve(LATEST_DIR))) { for (var latestFile : stream.toList()) { String contents = Files.readString(latestFile, StandardCharsets.UTF_8).strip(); - var latest = TransportVersionLatest.fromString(latestFile.getFileName().toString(), contents); + var latest = TransportVersionLatest.fromString(latestFile, contents); latests.put(latest.name(), latest); } } @@ -154,7 +153,7 @@ Map getLatestByReleaseBranch() throws IOExceptio /** Retrieve the latest transport version for the given release branch on main */ TransportVersionLatest getLatestFromMain(String releaseBranch) { - String resourcePath = getLatestRelativePath(releaseBranch).toString(); + Path resourcePath = getLatestRelativePath(releaseBranch); return getMainFile(resourcePath, TransportVersionLatest::fromString); } @@ -174,7 +173,7 @@ private Set getMainResources() { String output = gitCommand("ls-tree", "--name-only", "-r", "main", "."); HashSet resources = new HashSet<>(); - Collections.addAll(resources, output.split(System.lineSeparator())); + Collections.addAll(resources, output.split("\n")); // git always outputs LF mainResources.set(resources); } } @@ -188,7 +187,7 @@ private Set getChangedResources() { String output = gitCommand("diff", "--name-only", "main", "."); HashSet resources = new HashSet<>(); - Collections.addAll(resources, output.split(System.lineSeparator())); + Collections.addAll(resources, output.split("\n")); // git always outputs LF changedResources.set(resources); } } @@ -196,12 +195,13 @@ private Set getChangedResources() { } // Read a transport version resource from the main branch, or return null if it doesn't exist on main - private T getMainFile(String resourcePath, BiFunction parser) { - if (getMainResources().contains(resourcePath) == false) { + private T getMainFile(Path resourcePath, BiFunction parser) { + String pathString = resourcePath.toString().replace('\\', '/'); // normalize to forward slash that git uses + if (getMainResources().contains(pathString) == false) { return null; } - String content = gitCommand("show", "main:." + File.separator + resourcePath).strip(); + String content = gitCommand("show", "main:./" + pathString).strip(); return parser.apply(resourcePath, content); } @@ -213,7 +213,7 @@ private static Map readDefinitions(Path dir) try (var definitionsStream = Files.list(dir)) { for (var definitionFile : definitionsStream.toList()) { String contents = Files.readString(definitionFile, StandardCharsets.UTF_8).strip(); - var definition = TransportVersionDefinition.fromString(definitionFile.getFileName().toString(), contents); + var definition = TransportVersionDefinition.fromString(definitionFile, contents); definitions.put(definition.name(), definition); } }