Skip to content
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

Invalid pattern for dependenciesFromResources in JavaParser #4123

Closed
ckcd opened this issue Apr 7, 2024 · 0 comments · Fixed by #4124
Closed

Invalid pattern for dependenciesFromResources in JavaParser #4123

ckcd opened this issue Apr 7, 2024 · 0 comments · Fixed by #4124
Assignees
Labels
bug Something isn't working

Comments

@ckcd
Copy link
Contributor

ckcd commented Apr 7, 2024

Description of issue

In short: the file.getName() and pattern Pattern.compile("[/\\\\]" + artifactName + "-?.*\\.jar$") is mismatch so that the pattern.matcher will always be false. The file.getName() returns a simple filename without any / but the pattern needs a /.

In this dependenciesFromResources method it list all files in local system (default path is ~/.rewrite/classpath and it doesn't matter for this bug), then try to compute whether these files match the artifactNames given in args.

As we can see the source code is:

Pattern jarPattern = Pattern.compile("[/\\\\]" + artifactName + "-?.*\\.jar$");
File[] extracted = resourceTarget.listFiles();
if (extracted != null) {
    for (File file : extracted) {
        if (jarPattern.matcher(file.getName()).find()) {
            artifacts.add(file.toPath());
            continue nextArtifact;
        }
    }
}

For example:

  • if artifactName = spring-beans-5.* and their are a spring-beans-5.3.27.jar in target path, then we want match for it;
  • if artifactName = spring-beans-4.* and their are a spring-beans-5.3.27.jar in target path, then we want NOT match for it;

But the issue is: for now this pattern will always be NOT match no matter what the artifactName and local file is. The reason is the file.getName() and pattern Pattern.compile("[/\\\\]" + artifactName + "-?.*\\.jar$") is mismatch.

  • file.getName() returns a short name without any /.
  • but the pattern needs a /.

What version of OpenRewrite are you using?

I am using

  • OpenRewrite 8.1.6
  • Maven/Gradle plugin Maven 3.3.9 (irrelevant)
  • rewrite-java 8.1.6

How are you running OpenRewrite?

I am using the sbm-support-rewrite in spring-boot-migrator which will call open-rewrite.

What is the smallest, simplest way to reproduce the problem?

Any codes that using dependenciesFromResources can reproduce this issue. For my case it is:

// want to use /xxx/yyy as target jar folders
File folderFile = Path.of("/xxx/yyy").normalize().toFile();
InMemoryExecutionContext context = new InMemoryExecutionContext();
context.putMessage("org.openrewrite.java.parserClasspathDownloadLocation", folderFile);

// Init javaParser with above context
JavaParser.Builder<? extends JavaParser, ?> javaParserBuilder = JavaParser.fromJavaVersion()
    .logCompilationWarningsAndErrors(false).classpathFromResources(context,
        "spring-beans", "spring-context", "...");

ResourceParser rp = new ResourceParser(baseDir, new Slf4jToMavenLoggerAdapter(LOGGER), Set.of(), getPlainTextMasks(), -1, Set.of(), javaParserBuilder.clone());

// Use ResourceParser to parse source codes

// Run some recipes that will use jars in /xxx/yyy

What did you expect to see?

All artifactNames should be find in local folder and the missingArtifactNames in this line should be empty.

What did you see instead?

When use debug breakpoint I can see all artifactNames mismatch with local jars even they exists in my local. And the missingArtifactNames in this line is NOT empty.

Are you interested in contributing a fix to OpenRewrite?

Would like to if we agree that it's a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant