diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f35d9944b4..550afd5c35a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Smithy Changelog +## 1.57.1 (2025-04-21) + +### Bug Fixes + +- Fixed an issue where `FileManifest::writeJson` would return a relative path instead of an absolute one ([#2602](https://github.com/smithy-lang/smithy/pull/2602)) + ## 1.57.0 (2025-04-21) ### Features diff --git a/VERSION b/VERSION index 373aea97570..b4cf7c0db50 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.57.0 +1.57.1 diff --git a/smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java b/smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java index f8311742741..7891739f4fb 100644 --- a/smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java +++ b/smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java @@ -215,8 +215,7 @@ default Path writeFile(String path, InputStream fileContentsInputStream) { */ @SuppressWarnings("unused") default Path writeJson(Path path, Node node) { - writeUsing(path, (writer) -> Node.prettyPrintJsonToWriter(node, writer)); - return path; + return writeUsing(path, (writer) -> Node.prettyPrintJsonToWriter(node, writer)); } /** @@ -236,16 +235,19 @@ default Path writeJson(String path, Node node) { * * @param path Relative path to write to. * @param consumer Node data to write to JSON. + * @return Returns the resolved path. */ - default void writeUsing(Path path, Consumer consumer) { - path = addFile(path); + default Path writeUsing(Path path, Consumer consumer) { + Path resolved = addFile(path); - try (Writer writer = Files.newBufferedWriter(path)) { + try (Writer writer = Files.newBufferedWriter(resolved)) { consumer.accept(writer); writer.write('\n'); } catch (IOException e) { - throw new SmithyBuildException("Unable to create a write to file `" + path + "`: " + e.getMessage(), e); + throw new SmithyBuildException("Unable to create a write to file `" + resolved + "`: " + e.getMessage(), e); } + + return resolved; } /** diff --git a/smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java b/smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java index b4036e3391f..2687eb26a1f 100644 --- a/smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java +++ b/smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java @@ -132,10 +132,10 @@ public Path writeFile(Path path, InputStream fileContentsInputStream) { } @Override - public void writeUsing(Path path, Consumer consumer) { + public Path writeUsing(Path path, Consumer consumer) { Writer writer = new StringWriter(); consumer.accept(writer); - writeFile(path, writer.toString()); + return writeFile(path, writer.toString()); } /** diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/FileManifestTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/FileManifestTest.java index fd7fa6dee45..fbfe7a47c7b 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/FileManifestTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/FileManifestTest.java @@ -73,11 +73,11 @@ public void mergesRelativeWithBasePath() throws IOException { @Test public void writesJsonFiles() throws IOException { FileManifest a = FileManifest.create(outputDirectory); - a.writeJson("foo/file.json", Node.objectNode()); + Path resolved = a.writeJson("foo/file.json", Node.objectNode()); - assertThat(Files.isDirectory(outputDirectory.resolve("foo")), is(true)); - assertThat(Files.isRegularFile(outputDirectory.resolve("foo/file.json")), is(true)); - assertThat(new String(Files.readAllBytes(outputDirectory.resolve("foo/file.json"))), equalTo("{}\n")); + assertThat(resolved, equalTo(outputDirectory.resolve("foo/file.json"))); + assertThat(Files.isRegularFile(resolved), is(true)); + assertThat(new String(Files.readAllBytes(resolved)), equalTo("{}\n")); } @Test @@ -107,4 +107,20 @@ public void writesClassResources() { assertThat(Files.isRegularFile(outputDirectory.resolve("test.txt")), is(true)); } + + @Test + public void writesWithWriter() throws IOException { + FileManifest a = FileManifest.create(outputDirectory); + Path resolved = a.writeUsing(Paths.get("foo/bar.txt"), (w) -> { + try { + w.write("foo"); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + + assertThat(resolved, equalTo(outputDirectory.resolve("foo/bar.txt"))); + assertThat(Files.isRegularFile(resolved), is(true)); + assertThat(new String(Files.readAllBytes(resolved)).trim(), equalTo("foo")); + } }