Skip to content

Commit 89b7a6b

Browse files
committed
Skip searching of nonexistent directory in PathMatchingResourcePatternResolver
Prior to this commit, when PathMatchingResourcePatternResolver processed a `file:` pattern (for example, `file:/app-config/**`) for a `rootPath` that did not exist in the filesystem, the resolver attempted to search the directory and logged a WARNING message similar to the following when it failed to do so. Failed to search in directory [/app-config/] for files matching pattern [**]: java.nio.file.NoSuchFileException: /app-config/ To avoid unnecessary attempts to search a nonexistent directory, PathMatchingResourcePatternResolver now skips searching of a nonexistent directory and preemptively logs an INFO message similar to the following. Skipping search for files matching pattern [**]: directory [/app-config] does not exist Closes gh-31111
1 parent 1957033 commit 89b7a6b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource
792792
rootPath = Path.of(rootDirResource.getFile().getAbsolutePath());
793793
}
794794

795+
if (!Files.exists(rootPath)) {
796+
if (logger.isInfoEnabled()) {
797+
logger.info("Skipping search for files matching pattern [%s]: directory [%s] does not exist"
798+
.formatted(subPattern, rootPath.toAbsolutePath()));
799+
}
800+
return result;
801+
}
802+
795803
String rootDir = StringUtils.cleanPath(rootPath.toString());
796804
if (!rootDir.endsWith("/")) {
797805
rootDir += "/";

spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ void classpathStarWithPatternOnFileSystem() {
9090
assertFilenames(pattern, expectedFilenames);
9191
}
9292

93+
@Test // gh-31111
94+
void usingFileProtocolWithWildcardInPatternAndNonexistentRootPath() throws IOException {
95+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
96+
String pattern = String.format("file:%s/example/bogus/**", testResourcesDir);
97+
assertThat(resolver.getResources(pattern)).isEmpty();
98+
// When the log level for the resolver is set to at least INFO, we should see
99+
// a log entry similar to the following.
100+
//
101+
// [main] INFO o.s.c.i.s.PathMatchingResourcePatternResolver -
102+
// Skipping search for files matching pattern [**]: directory
103+
// [/<...>/spring-core/src/test/resources/example/bogus] does not exist
104+
}
105+
93106
@Test
94107
void encodedHashtagInPath() throws IOException {
95108
Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();

0 commit comments

Comments
 (0)