Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer projection failure until after plugins run #1762

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Defer projection failure until after plugins run
This changes `SmithyBuildImpl` to continue applying plugins after one fails,
throwing the error (if present) after they have all completed. This is
useful for example when you want to see the serialized output of a model
you know is valid but a plugin is causing the whole build to fail. A new
test case was added to ensure that artifacts produced by valid plugins
are still created despite the build failing.

This was originally implemented in #1416, but was rolled back in #1429
as a precaution since we had an unrelated issue ocurring at the time.
milesziemer committed May 4, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 5e7d4d8762427a152fc26861db753080ce328577
Original file line number Diff line number Diff line change
@@ -303,7 +303,7 @@ private ProjectionResult applyProjection(
ProjectionConfig projection,
ValidatedResult<Model> baseModel,
List<ResolvedPlugin> resolvedPlugins
) {
) throws Throwable {
Model resolvedModel = baseModel.unwrap();
LOGGER.fine(() -> String.format("Creating the `%s` projection", projectionName));

@@ -351,18 +351,33 @@ private ProjectionResult applyProjection(
LOGGER.fine(() -> String.format("No transforms to apply for projection %s", projectionName));
}

// Keep track of the first error created by plugins to fail the build after all plugins have run.
Throwable firstPluginError = null;
ProjectionResult.Builder resultBuilder = ProjectionResult.builder()
.projectionName(projectionName)
.model(projectedModel)
.events(modelResult.getValidationEvents());

for (ResolvedPlugin resolvedPlugin : resolvedPlugins) {
if (pluginFilter.test(resolvedPlugin.id.getArtifactName())) {
applyPlugin(projectionName, projection, baseProjectionDir, resolvedPlugin,
try {
applyPlugin(projectionName, projection, baseProjectionDir, resolvedPlugin,
projectedModel, resolvedModel, modelResult, resultBuilder);
} catch (Throwable e) {
if (firstPluginError == null) {
firstPluginError = e;
} else {
// Only log subsequent errors, since the first one is thrown.
LOGGER.severe(String.format("Plugin `%s` failed: %s", resolvedPlugin.id, e));
}
}
}
}

if (firstPluginError != null) {
throw firstPluginError;
}

return resultBuilder.build();
}

Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -268,6 +269,62 @@ public void failsByDefaultForUnknownPlugins() throws Exception {
assertThat(e.getMessage(), containsString("Unable to find a plugin for `unknown1`"));
}

@Test
public void defersFailureUntilAfterAllPluginsApplied() throws Exception {
SmithyBuildConfig config = SmithyBuildConfig.builder()
.load(Paths.get(getClass().getResource("defers-failure.json").toURI()))
.outputDirectory(outputDirectory.toString())
.build();

RuntimeException canned = new RuntimeException("broken");

// "broken" will run before "test1Serial" because of natural ordering
Map<String, SmithyBuildPlugin> plugins = new HashMap<>();
plugins.put("broken", new SmithyBuildPlugin() {
@Override
public String getName() {
return "broken";
}
@Override
public void execute(PluginContext context) {
throw canned;
}
});
plugins.put("test1Serial", new Test1SerialPlugin());

Function<String, Optional<SmithyBuildPlugin>> factory = SmithyBuildPlugin.createServiceFactory();
Function<String, Optional<SmithyBuildPlugin>> composed = name -> OptionalUtils.or(
Optional.ofNullable(plugins.get(name)), () -> factory.apply(name));

// Because the build will fail, we need a way to access the file manifests
List<FileManifest> manifests = new ArrayList<>();
Function<Path, FileManifest> fileManifestFactory = pluginBaseDir -> {
FileManifest fileManifest = new MockManifest(pluginBaseDir);
manifests.add(fileManifest);
return fileManifest;
};

SmithyBuild builder = new SmithyBuild()
.pluginFactory(composed)
.fileManifestFactory(fileManifestFactory)
.config(config);

SmithyBuildException e = Assertions.assertThrows(SmithyBuildException.class, builder::build);

// "broken" plugin produces the error that causes the build to fail
assertThat(e.getMessage(), containsString("java.lang.RuntimeException: broken"));
assertThat(e.getSuppressed(), equalTo(new Throwable[]{canned}));

List<Path> files = manifests.stream()
.flatMap(fm -> fm.getFiles().stream())
.collect(Collectors.toList());
assertThat(files, containsInAnyOrder(
outputDirectory.resolve("source/sources/manifest"),
outputDirectory.resolve("source/model/model.json"),
outputDirectory.resolve("source/build-info/smithy-build-info.json"),
outputDirectory.resolve("source/test1Serial/hello1Serial")));
}

@Test
public void cannotSetFiltersOrMappersOnSourceProjection() {
Throwable thrown = Assertions.assertThrows(SmithyBuildException.class, () -> {
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "2.0",
"plugins": {
"broken": {},
"test1Serial": {}
}
}