spotless icon indicating copy to clipboard operation
spotless copied to clipboard

indent step not playing nicely with multiline string literals

Open bmarwell opened this issue 2 years ago • 8 comments

If you are submitting a bug, please include the following:

  • [X] summary of problem
  • [X] gradle or maven version
  • [X] spotless version
  • [X] operating system and version
  • [ ] copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
  • [ ] copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace

setup

Maven any 3.8.x

spotless 2.32.0

OS - Mac, Linux, windows

What happened?

Reformats and destroys text blocks

source:

class TestClas {
  @Test
  public void serverFilecheck() {
    assertThat(myFile())
        .as("file [%s] any line contains [limitFieldSize=\"8192\"].", myFile())
        .content(StandardCharsets.UTF_8)
        .contains(
            """
  <httpEndpoint
    id="defaultHttpEndpoint"
    host="localhost"
    httpPort="8080"
    limitFieldSize="8192"
""");
  }
}

reformatted:

class TestClas {
  @Test
  public void serverFilecheck() {
    assertThat(myFile())
        .as("file [%s] any line contains [limitFieldSize=\"8192\"].", myFile())
        .content(StandardCharsets.UTF_8)
        .contains(
            """
<httpEndpoint
id="defaultHttpEndpoint"
host="localhost"
httpPort="8080"
limitFieldSize="8192"
""");
  }
}

Config:

spotless + indent 4 tabs + then indent 2 spaces (recommended workaround by spotless to have palantir with two spaces)

      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>2.32.0</version>
        <inherited>true</inherited>
        <configuration>
          <java>
            <toggleOffOn/>
            <palantirJavaFormat>
              <version>2.28.0</version>
            </palantirJavaFormat>

            <importOrder/>
            <removeUnusedImports/>
            <trimTrailingWhitespace/>
            <endWithNewline/>
            <formatAnnotations/>

            <indent>
              <tabs>true</tabs>
              <spacesPerTab>4</spacesPerTab>
            </indent>
            <indent>
              <spaces>true</spaces>
              <spacesPerTab>2</spacesPerTab>
            </indent
          </java>

          <pom>
            <includes>
              <include>pom.xml</include>
            </includes>

            <sortPom>
              <expandEmptyElements>false</expandEmptyElements>
            </sortPom>
          </pom>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <phase>validate</phase>
          </execution>
        </executions>
      </plugin>

What did you want to happen?

Maybe nudge the text block to the right, but the difference between the closing """ and the text MAY NOT BE ALTERED!

bmarwell avatar Mar 15 '23 12:03 bmarwell

It even reformats the code with // spotless:off 😞

bmarwell avatar Mar 15 '23 12:03 bmarwell

Any workarounds appreciated!

bmarwell avatar Mar 20 '23 07:03 bmarwell

the difference between the closing """ and the text MAY NOT BE ALTERED!

It looks like the "source" and "reformatted" that you copy-pasted above are the same. Is that a mistake? Regardless, I would try commenting out a step until you know exactly which step is causing this issue. I would guess it is palantir. Then I would file an issue in their bugtracker.

It even reformats the code with // spotless:off 😞

For sure you can workaround this bug with spotless:off. Do you have a tag // spotless:off and a matching //spotless:on afterwards? You need both.

nedtwigg avatar Mar 20 '23 19:03 nedtwigg

Hey! 👋🏻

No it's different, see the text block! It modified the Text block. Look closely.

Yes, I tried and it's NOT palantir. Only if I add the indent steps. Oh, and yes, I had both comments in there and it didn't work. Will try to set to a demo project on the train tomorrow.

bmarwell avatar Mar 20 '23 19:03 bmarwell

Ahhh, I see it now. You are correct, this is a problem in our indent step.

BEFORE
  <httpEndpoint
    id="defaultHttpEndpoint"
AFTER
<httpEndpoint
id="defaultHttpEndpoint"

We're turning every 4 spaces into a tab, and then turning each tab into 2 spaces. The indent step just looks for leading spaces, it doesn't (currently) know anything about the syntax of multiline string literals on the various languages it is used for.

So the first line, indented by 2 spaces, which is not a multiple of 4, it makes sense that it gets clobbered. But I would expect the second line to be okay. We picked bad names for these steps, and we should fix that in our next breaking version change

  • https://github.com/diffplug/spotless/issues/794

And regardless of that, spotless:off should definitely be able to fix this. I can see three different things going wrong here

  1. spotles:off should be able to fix this
  2. the second line should be okay
  3. the first line "should" be broken, which shows that we need to rename these indent steps better, and possibly also make them language-aware so they understand multiline string literals.

nedtwigg avatar Mar 20 '23 19:03 nedtwigg

100% agree! For now, the quickest and easiest solution would probably be to get the off working. I'm traveling right now, so I'm terribly sorry I wasn't able to upload a reproducer.

bmarwell avatar Mar 21 '23 08:03 bmarwell

@nedtwigg are we still waiting for https://github.com/palantir/palantir-java-format/issues/930 ?

bmarwell avatar May 27 '24 13:05 bmarwell

@bmarwell thanks for the duplicate tag, any way forward on this topic?

rohan-changejar avatar May 27 '24 15:05 rohan-changejar