diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index 182630c1434..6586e6c0bd2 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java @@ -754,6 +754,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol protected Set doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException { + Set result = new LinkedHashSet<>(); URI rootDirUri; try { rootDirUri = rootDirResource.getURI(); @@ -762,7 +763,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol if (logger.isWarnEnabled()) { logger.warn("Failed to resolve directory [%s] as URI: %s".formatted(rootDirResource, ex)); } - return Collections.emptySet(); + return result; } Path rootPath = null; @@ -791,6 +792,14 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol rootPath = Path.of(rootDirResource.getFile().getAbsolutePath()); } + if (!Files.exists(rootPath)) { + if (logger.isInfoEnabled()) { + logger.info("Skipping search for files matching pattern [%s]: directory [%s] does not exist" + .formatted(subPattern, rootPath.toAbsolutePath())); + } + return result; + } + String rootDir = StringUtils.cleanPath(rootPath.toString()); if (!rootDir.endsWith("/")) { rootDir += "/"; @@ -806,7 +815,6 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol .formatted(rootPath.toAbsolutePath(), subPattern)); } - Set result = new LinkedHashSet<>(); try (Stream files = Files.walk(rootPath)) { files.filter(isMatchingFile).sorted().map(FileSystemResource::new).forEach(result::add); } diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index 61de1af5dce..b234739a270 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -90,6 +90,19 @@ class PathMatchingResourcePatternResolverTests { assertFilenames(pattern, expectedFilenames); } + @Test // gh-31111 + void usingFileProtocolWithWildcardInPatternAndNonexistentRootPath() throws IOException { + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); + String pattern = String.format("file:%s/example/bogus/**", testResourcesDir); + assertThat(resolver.getResources(pattern)).isEmpty(); + // When the log level for the resolver is set to at least INFO, we should see + // a log entry similar to the following. + // + // [main] INFO o.s.c.i.s.PathMatchingResourcePatternResolver - + // Skipping search for files matching pattern [**]: directory + // [/<...>/spring-core/src/test/resources/example/bogus] does not exist + } + @Test void encodedHashtagInPath() throws IOException { Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();