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

Support JEP 330 "shebang" headers #1214

Closed
vorburger opened this issue Dec 29, 2024 · 6 comments
Closed

Support JEP 330 "shebang" headers #1214

vorburger opened this issue Dec 29, 2024 · 6 comments

Comments

@vorburger
Copy link
Member

vorburger commented Dec 29, 2024

I'm toying (more "for fun") with https://openjdk.org/jeps/330 in enola-dev/enola#971:

I am using https://www.jbang.dev, but this issue doesn't really have anything to do with that (JBang) per se:

For the following content of learnenola.java which (at least as I understand it) is technically "valid standard Java syntax" per JEP 330:

#!/usr/bin/env jbang

import static java.lang.System.out;

public class learnenola {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

google-java-format currently fails like this:

$ ./google-java-format_linux-x86-64 --aosp --lines 1:11 learnenola.java

<stdin>:1:1: error: illegal character: '#'
#!/usr/bin/env jbang
^
<stdin>:1:3: error: class, interface, enum, or record expected
#!/usr/bin/env jbang
  ^
 Command failed: ./google-java-format_linux-x86-64 --aosp --lines 1:13 -
<stdin>:1:1: error: illegal character: '#'
#!/usr/bin/env jbang
^
<stdin>:1:3: error: class, interface, enum, or record expected
#!/usr/bin/env jbang
  ^

See also https://www.jbang.dev/documentation/guide/latest/faq.html for some further background if needed.

PS: I've opened another issue #1215 for something that's a separate problem related to "special" (non-shebang) /// comments.

@vorburger
Copy link
Member Author

vorburger commented Dec 29, 2024

which (at least as I understand it) is technically "valid standard Java syntax" per JEP 330

FYI both Visual Studio Code and IntelliJ IDEA don't really like this syntax either (VSC, which uses JDT, just says "Syntax error on tokens, delete these tokens", and IJ says "Shebang mechanism in .java files is not permitted" (which surprised me given that this jetbrains.com post says it should work; but possibly I just couldn't figure out the right language level support Settings).

But be that (current Editor support) as it may, per JEP 330 it's still valid Java syntax, so (in a perfect world... 🍭 ) google-java-format should not choke on it.

@vorburger
Copy link
Member Author

FTR This is on v1.25.1, which is the 2nd most recent release today; looking at the 1.25.2 Release Notes, I have no reason to believe that this (just) got fixed.

@cushon
Copy link
Collaborator

cushon commented Jan 3, 2025

technically "valid standard Java syntax" per JEP 330

I'm not sure that's right, as I understand it JEP 330 is exclusively a Java launcher (bin/java) feature, and mentions that

It is not a goal to change either the Java Language Specification (JLS) or javac to accommodate shebang file

javac also rejects these files:

$ javac T.java
T.java:1: error: illegal character: '#'
#!/usr/bin/env jbang
^
T.java:1: error: class, interface, enum, or record expected
#!/usr/bin/env jbang
  ^
2 errors

@vorburger
Copy link
Member Author

I'm not sure that's right, as I understand it JEP 330 is exclusively a Java launcher (bin/java) feature, and mentions that

Huh, wait... what - really? So java T.java works, but javac T.java fails - and that's intentional? OMG - what were they thinking! 🤯

@maxandersen just to double check, are we understanding this correctly, or missing anything here? (If we are, then I'll close this issue, but also send you a PR to make this a bit more explicit and clear on the JBang FAQ §6. related to this; because it seems to me that this basically means you would really want to think twice before adopting to use shebang headers... if this is the case, then how can JBang even compile such files?)

@maxandersen
Copy link

@vorburger correct - java's shebang support is IMO fundementally broken. its why jbang uses /// trick in header to be something that IDE's and tools like google-java-format trips won't trip over as #! at start goes against the Java Language Spec.

@vorburger
Copy link
Member Author

@maxandersen thanks for the clarification. I've sent you jbangdev/jbang#1903.

@cushon I'm closing this FYI, as the conversation in this issue have successfully convinced me this is not a good idea.

@vorburger vorburger closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants