Skip to content

Commit 81ecb64

Browse files
kmccarpshanman190jkschneider
authored
Pass resolvers to JS/Python RPC so PrepareRecipe callbacks can resolve Maven recipes (#6742)
* Fall back to RecipeLoader when ecosystem resolver is missing When the PrepareRecipe handler finds a recipe in the marketplace but listing.prepare() throws because no resolver matches the bundle's ecosystem (e.g., a "maven"-bundled recipe when only a "runtime" resolver is available), the handler now catches the exception and falls through to RecipeLoader. This allows recipes that are already on the classpath to be loaded by class name instead of failing outright. This fixes recipe runs on Node.js repositories where JS recipes use preconditions like usesType() or usesMethod() that call back to the Java side to prepare Java recipes. Without a Maven resolver wired into the RPC endpoint, these callbacks previously failed with "No available resolver for 'maven' ecosystem". * Accept resolvers in JavaScriptRewriteRpc and PythonRewriteRpc Both RPC classes were calling the 2-arg RewriteRpc constructor which passes emptyList() for resolvers. When a JS/Python recipe's preconditions reference Java recipes bundled under the "maven" ecosystem, the PrepareRecipe callback fails with "No available resolver for 'maven' ecosystem". Add a resolvers field to both Builders (defaulting to emptyList() for backward compatibility) and pass them through to the 3-arg super constructor. * Polish * Remove no longer valid test case as well * Add missing RecipeBundleResolver import to PythonRewriteRpc --------- Co-authored-by: Shannon Pamperl <shannon@moderne.io> Co-authored-by: Shannon Pamperl <shanman190@gmail.com> Co-authored-by: Jonathan Schneider <jkschneider@gmail.com>
1 parent 6b5d958 commit 81ecb64

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

rewrite-core/src/test/java/org/openrewrite/rpc/RewriteRpcTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.Test;
2727
import org.openrewrite.*;
2828
import org.openrewrite.config.Environment;
29+
import org.openrewrite.config.OptionDescriptor;
2930
import org.openrewrite.config.RecipeDescriptor;
3031
import org.openrewrite.internal.RecipeLoader;
3132
import org.openrewrite.marketplace.*;

rewrite-javascript/src/main/java/org/openrewrite/javascript/rpc/JavaScriptRewriteRpc.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.Parser;
2323
import org.openrewrite.SourceFile;
2424
import org.openrewrite.internal.StringUtils;
25+
import org.openrewrite.marketplace.RecipeBundleResolver;
2526
import org.openrewrite.marketplace.RecipeMarketplace;
2627
import org.openrewrite.rpc.RewriteRpc;
2728
import org.openrewrite.rpc.RewriteRpcProcess;
@@ -57,8 +58,8 @@ public class JavaScriptRewriteRpc extends RewriteRpc {
5758
private final Map<String, String> commandEnv;
5859
private final RewriteRpcProcess process;
5960

60-
JavaScriptRewriteRpc(RewriteRpcProcess process, RecipeMarketplace marketplace, String command, Map<String, String> commandEnv) {
61-
super(process.getRpcClient(), marketplace);
61+
JavaScriptRewriteRpc(RewriteRpcProcess process, RecipeMarketplace marketplace, List<RecipeBundleResolver> resolvers, String command, Map<String, String> commandEnv) {
62+
super(process.getRpcClient(), marketplace, resolvers);
6263
this.command = command;
6364
this.commandEnv = commandEnv;
6465
this.process = process;
@@ -199,6 +200,7 @@ public static Builder builder() {
199200
@RequiredArgsConstructor
200201
public static class Builder implements Supplier<JavaScriptRewriteRpc> {
201202
private RecipeMarketplace marketplace = new RecipeMarketplace();
203+
private List<RecipeBundleResolver> resolvers = Collections.emptyList();
202204
private final Map<String, String> environment = new HashMap<>();
203205
private Path npxPath = System.getProperty("os.name").toLowerCase().contains("windows") ? Paths.get("npx.cmd") : Paths.get("npx");
204206
private @Nullable Path log;
@@ -218,6 +220,11 @@ public Builder marketplace(RecipeMarketplace marketplace) {
218220
return this;
219221
}
220222

223+
public Builder resolvers(List<RecipeBundleResolver> resolvers) {
224+
this.resolvers = resolvers;
225+
return this;
226+
}
227+
221228
public Builder recipeInstallDir(@Nullable Path recipeInstallDir) {
222229
this.recipeInstallDir = recipeInstallDir;
223230
return this;
@@ -382,7 +389,7 @@ public JavaScriptRewriteRpc get() {
382389
process.start();
383390

384391
try {
385-
return (JavaScriptRewriteRpc) new JavaScriptRewriteRpc(process, marketplace,
392+
return (JavaScriptRewriteRpc) new JavaScriptRewriteRpc(process, marketplace, resolvers,
386393
String.join(" ", cmdArr), process.environment())
387394
.livenessCheck(process::getLivenessCheck)
388395
.timeout(timeout)

rewrite-python/src/main/java/org/openrewrite/python/rpc/PythonRewriteRpc.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.RequiredArgsConstructor;
2020
import org.jspecify.annotations.Nullable;
2121
import org.openrewrite.*;
22+
import org.openrewrite.marketplace.RecipeBundleResolver;
2223
import org.openrewrite.marketplace.RecipeMarketplace;
2324
import org.openrewrite.python.*;
2425
import org.openrewrite.python.marker.PythonResolutionResult;
@@ -63,8 +64,8 @@ public class PythonRewriteRpc extends RewriteRpc {
6364
private final Map<String, String> commandEnv;
6465
private final RewriteRpcProcess process;
6566

66-
PythonRewriteRpc(RewriteRpcProcess process, RecipeMarketplace marketplace, String command, Map<String, String> commandEnv) {
67-
super(process.getRpcClient(), marketplace);
67+
PythonRewriteRpc(RewriteRpcProcess process, RecipeMarketplace marketplace, List<RecipeBundleResolver> resolvers, String command, Map<String, String> commandEnv) {
68+
super(process.getRpcClient(), marketplace, resolvers);
6869
this.command = command;
6970
this.commandEnv = commandEnv;
7071
this.process = process;
@@ -351,6 +352,7 @@ public static Builder builder() {
351352
@RequiredArgsConstructor
352353
public static class Builder implements Supplier<PythonRewriteRpc> {
353354
private RecipeMarketplace marketplace = new RecipeMarketplace();
355+
private List<RecipeBundleResolver> resolvers = Collections.emptyList();
354356
private final Map<String, String> environment = new HashMap<>();
355357
// Default to looking for a venv python, falling back to system python
356358
private Path pythonPath = findDefaultPythonPath();
@@ -396,6 +398,11 @@ public Builder marketplace(RecipeMarketplace marketplace) {
396398
return this;
397399
}
398400

401+
public Builder resolvers(List<RecipeBundleResolver> resolvers) {
402+
this.resolvers = resolvers;
403+
return this;
404+
}
405+
399406
/**
400407
* Path to the Python executable.
401408
*
@@ -565,7 +572,7 @@ public PythonRewriteRpc get() {
565572
process.start();
566573

567574
try {
568-
return (PythonRewriteRpc) new PythonRewriteRpc(process, marketplace,
575+
return (PythonRewriteRpc) new PythonRewriteRpc(process, marketplace, resolvers,
569576
String.join(" ", cmdArr), process.environment())
570577
.livenessCheck(process::getLivenessCheck)
571578
.timeout(timeout)

0 commit comments

Comments
 (0)