Skip to content

PathMatchingResourcePatternResolver can no longer resolve files on Windows #29226

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

Closed
eugeniace opened this issue Sep 30, 2022 · 3 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task
Milestone

Comments

@eugeniace
Copy link

eugeniace commented Sep 30, 2022

There is problem with latest changes on the method PathMatchingResourcePatternResolver.doFindPathMatchingFileResources. It fails on Windows with the following error:

Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 2: /C:/TestDir/target/classes/com/example
        at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:765)
        at org.springframework.web.context.support.ServletContextResourcePatternResolver.doFindPathMatchingFileResources(ServletContextResourcePatternResolver.java:91)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:558)
        at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:328)

The leading slash should be removed before calling Path rootPath = fileSystem.getPath(rootDir);
We cannot run any application on SB 3.0.0-SNAPSHOT on Windows because of this problem.
Example of code raising the exception (after removing the leading slash it works fine):

FileSystem fileSystem = FileSystems.getDefault();
fileSystem.getPath("/C:/TestDir");
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 30, 2022
@eugeniace eugeniace changed the title Exception Illegal char <:> at index 2: from PathMatchingResourcePatternResolver.doFindPathMatchingFileResources on Windows Exception Illegal char <:> at index 2: in PathMatchingResourcePatternResolver on Windows Sep 30, 2022
@bclozel bclozel added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 30, 2022
@bclozel bclozel added this to the 6.0.0-RC1 milestone Sep 30, 2022
@bclozel bclozel added type: regression A bug that is also a regression and removed type: bug A general bug labels Sep 30, 2022
@sbrannen
Copy link
Member

sbrannen commented Oct 1, 2022

@sbrannen sbrannen changed the title Exception Illegal char <:> at index 2: in PathMatchingResourcePatternResolver on Windows PathMatchingResourcePatternResolver cannot resolve files on Windows Oct 2, 2022
@sbrannen sbrannen changed the title PathMatchingResourcePatternResolver cannot resolve files on Windows PathMatchingResourcePatternResolver can no longer resolve files on Windows Oct 2, 2022
@eugeniace
Copy link
Author

eugeniace commented Oct 3, 2022

To solve the bug the following code from method protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) :

                URI rootDirUri;
		String rootDir;
		try {
			rootDirUri = rootDirResource.getURI();
                        rootDir = rootDirUri.getPath();
			// If the URI is for a "resource" in the GraalVM native image file system, we have to
			// ensure that the root directory does not end in a slash while simultaneously ensuring
			// that the root directory is not an empty string (since fileSystem.getPath("").resolve(str)
			// throws an ArrayIndexOutOfBoundsException in a native image).
			if ("resource".equals(rootDirUri.getScheme()) && (rootDir.length() > 1) && rootDir.endsWith("/")) {
				rootDir = rootDir.substring(0, rootDir.length() - 1);
			}
		}
		catch (Exception ex) {
			if (logger.isInfoEnabled()) {
				logger.info("Failed to resolve %s in the file system: %s".formatted(rootDirResource, ex));
			}
			return Collections.emptySet();
		}
            	FileSystem fileSystem = getFileSystem(rootDirUri);
		if (fileSystem == null) {
			return Collections.emptySet();
		}
		try {
			Path rootPath = fileSystem.getPath(rootDir);

could be replaced with the following:

Path.of(rootDirResource.getURI()) . It also removes the ending /.

@jhoeller jhoeller assigned jhoeller and sbrannen and unassigned sbrannen Oct 3, 2022
jhoeller added a commit that referenced this issue Oct 3, 2022
In addition to consistent use of cleaned file paths, this revision avoids FileSystem SPI interaction, resolving NIO Path instances from URI (and FileSystemResource from Path).

See gh-29226
@sbrannen sbrannen added type: task A general task and removed type: regression A bug that is also a regression labels Oct 3, 2022
@sbrannen
Copy link
Member

sbrannen commented Oct 3, 2022

Although this was technically a regression, that regression was not released.

In light of that, I have changed the label to task so that this issue does not show up in the release notes.

sbrannen added a commit to sbrannen/spring-framework that referenced this issue Aug 13, 2023
The recent switch to java.nio.file.FileSystem-based classpath scanning
(see spring-projectsgh-29163) resulted in a regression when scanning the classpath in
the Windows filesystem.

This commit addresses this regression by introducing several
workarounds for dealing with `file:` resources on Windows.

See the in-line comments in this commit for details on those
workarounds.

This fix has been tested in the following environments.

- macOS 12.6 / Java 17 Oracle (build 17.0.4.1+1-LTS-2)

- macOS 12.6 / GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06)

- MS Windows 11 / Java 17 Microsoft (build 17.0.4.1+1-LTS)

Closes spring-projectsgh-29226
@sbrannen sbrannen changed the title PathMatchingResourcePatternResolver can no longer resolve files on Windows PathMatchingResourcePatternResolver can no longer resolve files on Windows Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: task A general task
Projects
None yet
Development

No branches or pull requests

5 participants