Skip to content

Commit 25fcfd5

Browse files
authored
Fix local YAML recipe scanning to be recursive (#7437)
* Add witness test for recursive YAML recipe scanning * Fix local YAML recipe scanning to be recursive
1 parent 857c424 commit 25fcfd5

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/config/ClasspathScanningLoader.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.*;
4040
import java.util.jar.JarEntry;
4141
import java.util.jar.JarFile;
42+
import java.util.stream.Stream;
4243

4344
import static java.util.Collections.emptyList;
4445
import static java.util.Collections.emptyMap;
@@ -415,11 +416,14 @@ private static List<YamlResource> listYamlResourcesFromPath(Path path) {
415416
if (Files.isDirectory(path)) {
416417
Path rewriteDir = path.resolve("META-INF/rewrite");
417418
if (Files.isDirectory(rewriteDir)) {
418-
try (DirectoryStream<Path> stream = Files.newDirectoryStream(rewriteDir, "*.{yml,yaml}")) {
419-
for (Path file : stream) {
420-
resources.add(new YamlResource(file.toUri(), () -> Files.newInputStream(file)));
421-
}
422-
} catch (IOException ignored) {
419+
try (Stream<Path> stream = Files.walk(rewriteDir)) {
420+
stream.filter(Files::isRegularFile)
421+
.filter(p -> {
422+
String name = p.getFileName().toString();
423+
return name.endsWith(".yml") || name.endsWith(".yaml");
424+
})
425+
.forEach(file -> resources.add(new YamlResource(file.toUri(), () -> Files.newInputStream(file))));
426+
} catch (IOException | java.io.UncheckedIOException ignored) {
423427
}
424428
}
425429
} else if (Files.isRegularFile(path)) {

rewrite-core/src/test/java/org/openrewrite/config/ClasspathScanningLoaderTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,42 @@ void scanJarWithSeparateClassesAndResourcesDirs(@TempDir Path tempDir) throws Ex
7272
assertThat(recipe).isNotNull();
7373
assertThat(recipe.getName()).isEqualTo("com.example.TestRecipe");
7474
}
75+
76+
@Test
77+
void scanYamlInSubdirectory(@TempDir Path tempDir) throws Exception {
78+
Path resourcesDir = tempDir.resolve("resources");
79+
Path metaInfSubdir = resourcesDir.resolve("META-INF/rewrite/subdir");
80+
Files.createDirectories(metaInfSubdir);
81+
82+
Files.writeString(metaInfSubdir.resolve("recipe.yml"),
83+
"""
84+
type: specs.openrewrite.org/v1beta/recipe
85+
name: com.example.SubdirRecipe
86+
displayName: Subdir Recipe
87+
description: A test recipe in a subdir.
88+
recipeList:
89+
- org.openrewrite.text.ChangeText:
90+
toText: subdir
91+
""");
92+
93+
List<Path> classpath = List.of(resourcesDir);
94+
URL[] urls = classpath.stream()
95+
.map(p -> {
96+
try {
97+
return p.toUri().toURL();
98+
} catch (Exception e) {
99+
throw new RuntimeException(e);
100+
}
101+
})
102+
.toArray(URL[]::new);
103+
ClassLoader cl = new URLClassLoader(urls, Recipe.class.getClassLoader());
104+
105+
Environment env = Environment.builder()
106+
.scanJar(resourcesDir, classpath, cl)
107+
.build();
108+
109+
Recipe recipe = env.activateRecipes("com.example.SubdirRecipe");
110+
assertThat(recipe).isNotNull();
111+
assertThat(recipe.getName()).isEqualTo("com.example.SubdirRecipe");
112+
}
75113
}

0 commit comments

Comments
 (0)