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

Exclusions resolve wrong base path on multimodule projects #801

Open
Philzen opened this issue Jun 16, 2024 · 1 comment
Open

Exclusions resolve wrong base path on multimodule projects #801

Philzen opened this issue Jun 16, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Philzen
Copy link

Philzen commented Jun 16, 2024

What version of OpenRewrite are you using?

I am using rewrite-maven-plugin 5.33.0

How are you running OpenRewrite?

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>5.33.0</version>
    <configuration>
        <detail>true</detail>
        <activeRecipes>
            <recipe>org.philzen.oss.testng.MigrateToJunit5</recipe>
        </activeRecipes>
        <exclusions>
            <exclude>${project.basedir}/bin/**</exclude>
            <exclude>${project.basedir}/docs/**</exclude>
            <exclude>${project.basedir}/modules/openapi-generator-gradle-plugin/**</exclude>
            <exclude>${project.basedir}/modules/openapi-generator-maven-plugin/**</exclude>
            <exclude>${project.basedir}/samples/**</exclude>
            <exclude>${project.basedir}/website/**</exclude>
        </exclusions>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.philzen.oss</groupId>
            <artifactId>rewrite-recipe</artifactId>
            <version>1.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>

Problem details and root

I was constantly running into

[ERROR] Java heap space -> [Help 1] 
java.lang.OutOfMemoryError: Java heap space

when executing mvn rewrite:run on a rather large repo (this one).

So i thought i could mitigate that by limiting the amount of files being parsed, as i was seeing "[WARNING] There were problems parsing …" on lots of files that are completely irrelevant to the migration (i.e. yaml files), by using <excludes> in the rewrite-maven-plugin config.

All of the following did not work, there was no effect whatsoever on the warnings and the fatal OutOfMemoryError:

  • <exclude>*/samples/**</exclude> (just as shown on the reference page)
  • <exclude>${project.basedir}/samples/**</exclude>
  • <exclude>./samples/**</exclude>
  • <exclude>samples/**</exclude>

Running again with -X revealed what i believe to be the issue here:

[DEBUG]   (f) exclusions = [
~/prog/oss/openapi-generator/modules/openapi-generator/bin/**, 
~/prog/oss/openapi-generator/modules/openapi-generator/docs/**, 
~/prog/oss/openapi-generator/modules/openapi-generator/modules/openapi-generator-gradle-plugin/**,
~/prog/oss/openapi-generator/modules/openapi-generator/modules/openapi-generator-maven-plugin/**, 
~/prog/oss/openapi-generator/modules/openapi-generator/samples/**, 
~/prog/oss/openapi-generator/modules/openapi-generator/website/**]

(reformatted and actual home dir replaced with ~ for legibility here)

The pom.xml plugin config posted above lies in the root dir (~/prog/oss/openapi-generator) but all the excludes are resolved to the context of the module currently being processed, hence they are not being applied as expected – the samples folder in the main directory will still be parsed and the run fails with OutOfMemoryException.

Workaround

**/samples/** works (obviously)

But this workaround may not be feasible for everybody, i.e. another recipe user may need to exclude ${project.basedir}/samples but not ${project.basedir}/modules/some-module/samples.

@Philzen Philzen added the bug Something isn't working label Jun 16, 2024
@timtebeek
Copy link
Contributor

Thanks for the detailed report! It seems we convert the configured paths to matchers on these lines, which then might need to be revised.

ResourceParser rp = new ResourceParser(baseDir, logger, exclusions, plainTextMasks, sizeThresholdMb, pathsToOtherMavenProjects(mavenProject),
javaParserBuilder.clone(), kotlinParserBuilder.clone(), ctx);
sourceFiles = Stream.concat(sourceFiles, processMainSources(mavenProject, javaParserBuilder.clone(), kotlinParserBuilder.clone(), rp, projectProvenance, alreadyParsed, ctx));
sourceFiles = Stream.concat(sourceFiles, processTestSources(mavenProject, javaParserBuilder.clone(), kotlinParserBuilder.clone(), rp, projectProvenance, alreadyParsed, ctx));
Collection<PathMatcher> exclusionMatchers = exclusions.stream()
.map(pattern -> baseDir.getFileSystem().getPathMatcher("glob:" + pattern))
.collect(toList());
sourceFiles = sourceFiles.map(sourceFile -> {
if (sourceFile instanceof J.CompilationUnit) {
for (PathMatcher excluded : exclusionMatchers) {
if (excluded.matches(sourceFile.getSourcePath())) {
return null;
}
}
}
return sourceFile;
}).filter(Objects::nonNull);

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
Status: Backlog
Development

No branches or pull requests

2 participants