openrewrite / rewrite-maven-plugin

OpenRewrite's Maven plugin.
https://openrewrite.github.io/rewrite-maven-plugin/plugin-info.html
Apache License 2.0
138 stars 71 forks source link

Recipe org.openrewrite.text.FindAndReplace ignores files that have "build" or "out" directories in its path #850

Open ErhardSiegl opened 3 weeks ago

ErhardSiegl commented 3 weeks ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

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

Run the following script:

#!/bin/sh

# set -x

for f in src src/build src/build/dir src/out src/dir; do
    mkdir $f  # ignore errors
    echo hello > $f/hello.txt
done

cat <<'EOF' > pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>at.gepardec.openrewrite</groupId>
  <artifactId>findreplace</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.39.1</version>
        <configuration>
          <activeRecipes>
            <recipe>at.gepardec.hello</recipe>
          </activeRecipes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
EOF

cat <<'EOF' > rewrite.yml
---
type: specs.openrewrite.org/v1beta/recipe
name: at.gepardec.hello
displayName: Test FindAndReplace
recipeList:
  - org.openrewrite.text.FindAndReplace:
      find: hello
      replace: welcome
      filePattern: '**/hello.txt'
EOF

mvn org.openrewrite.maven:rewrite-maven-plugin:dryRun

What did you see instead?

I expect that changes would be made in the 5 directories src, src/build, src/build/dir, src/out, src/dir. Instead you see in the output that only files in src and src/dir are changed, files that don't have out or build in their pathname.

[INFO] Running recipe(s)...
[WARNING] These recipes would make changes to src/dir/hello.txt:
[WARNING]     org.openrewrite.text.FindAndReplace: {find=hello, replace=welcome, filePattern=**/hello.txt}
[WARNING] These recipes would make changes to src/hello.txt:
[WARNING]     org.openrewrite.text.FindAndReplace: {find=hello, replace=welcome, filePattern=**/hello.txt}
[WARNING] Patch file available:
[WARNING]     /Users/esiegl/Documents/Develop/OpenRewrite/Tmp2/target/rewrite/rewrite.patch
[WARNING] Estimate time saved: 10m
[WARNING] Run 'mvn rewrite:run' to apply the recipes.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Are you interested in contributing a fix to OpenRewrite?

Yes

ErhardSiegl commented 3 weeks ago

In https://rewriteoss.slack.com/archives/C01A843MWG5/p1724938610370409?thread_ts=1724935505.161649&cid=C01A843MWG5 Shannon Pamperl suggests that the problem might be related to

https://github.com/openrewrite/rewrite-maven-plugin/blob/1655a40e3a97c6666c9fa5f91a151da9c6265192/src%2Fmain%2Fjava%2Forg%2Fopenrewrite%2Fmaven%2FResourceParser.java#L43