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

spotless:on / off is not ignoring unsupported formats #1887

Closed
matsev opened this issue Nov 16, 2023 · 2 comments
Closed

spotless:on / off is not ignoring unsupported formats #1887

matsev opened this issue Nov 16, 2023 · 2 comments
Labels

Comments

@matsev
Copy link

matsev commented Nov 16, 2023

palantir-java-format does not currently all Java 21 additions (see palantir/palantir-java-format#952). In an attempt to work around this problem, I tried to use spotless:off and spotless:on. The example below compiles using JDK 21 as it is supported by JEP 441 : Pattern Matching for switch:

class Example {
    // spotless:off
    static String test(CharSequence charSequence) {
        return switch (charSequence) {
            case String s when s.isEmpty() -> "empty string";
            case String s -> "non-empty string";
            default -> "not a String";
        };
    }
   // spotless:on
}

Since the code is currently not supported by palantir-java-format, I tried to mute the problem by adding the // spotless:off and // spotless:on comments to disable evaluation of the code as showed above.

Expected result

The source code between // spotless:off and // spotless:on should be ignored by spotless and the evaluation should succeed.

Actual result

The spotless evaluation fails in the same way as if the comments were not added, i.e.

mvn spotless:apply
[...]
Unable to format file [path to Example.java]: com.palantir.javaformat.java.FormatterException: error: expected token: 'when'; generated - instead -> [Help 1]

References

Maven config:

                <plugin>
                    <groupId>com.diffplug.spotless</groupId>
                    <artifactId>spotless-maven-plugin</artifactId>
                    <version>2.40.0</version>
                    <configuration>
                        <java>
                            <palantirJavaFormat>
                                <version>2.38.0</version>
                            </palantirJavaFormat>
                           <toggleOffOn/>
                        </java>
                    </configuration>
                </plugin>

Maven:

mvn -v
Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)
Java version: 21, vendor: Amazon.com Inc.
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "13.6.1", arch: "x86_64", family: "mac"
@matsev matsev changed the title spotless:on / off is not working for unsupported formats spotless:on / off is not ignoring unsupported formats Nov 16, 2023
@nedtwigg
Copy link
Member

This is the expected behavior. For why, you can look at the spotless:off PR, details below

... It implements this under the hood with PipeStepPair, which has an In step and an Out step. The In step is the very first step which gets applied, Out is the last step, and all the other formatting steps go in between them. The In step finds matching off/on pairs, and stores the text between them. The intermediate steps like google-java-format or whatever do their work, and at the end the Out step finds the off/on pairs again, which may have moved. Then it replaces those pairs with whatever was stored by the In step.

@matsev
Copy link
Author

matsev commented Nov 16, 2023

Ok, that's unfortunate. Thanks for the quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants