Skip to content

shaded javac should set Automatic-Module-Name and shade service loader entries #266

Closed

Description

@sormuras

When integrating google-java-format as a library (into an application that lives on the module-path) there are some issues. Especially with the 3rd-party dependencies google-java-format relies on.

One issue, namely the dependency to a certain error-prone/javac release, could be tackled by using javac provided by the current runtime system.

Copied from forax/pro#34 (comment)
For the javac-9-dev-r3297-1-shaded.jar, it would be cool if google-java-format had an option to use javac through the service interface as we do in the compiler plugin instead of using its own version.

Activity

  1. cushon commented on Mar 16, 2018

    @cushon
    Collaborator

    Can you expand on what the issues are? The motivation for the shaded javac is that GJF uses internal javac APIs that change between releases. Supporting arbitrary javac versions doesn't seem that useful, because it will still only work with that latest version. What problems are you seeing with -all-deps.jar in that PR?

  2. sormuras commented on Mar 16, 2018

    @sormuras
    ContributorAuthor

    I'm just in the process of rebasing the PR to gjf 1.5 and current pro master. There are two ways to include gjf into an external application, like pro. a) Use the 3 single jars or b) use the -all-deps.jar.

    a)
    Quote: I didn't managed to make modules via module-fixer out of neither the "-all-deps" distro nor the 3 single jars: google-java-format-1.3.jar, guava-19.0.jar and javac-9-dev-r3297-1-shaded.jar

    Not all problems I faced with the none -all-deps.jar way come to my mind now.
    Forax' pro tool runs entirely on the module-path. iirc, gjf poses no problem. Especially after 1.6 will provide an Automatic-Module-Name. Guava was not behaving well back then, and javac-... had some split-package issues.

    b)
    My solution then was to fallback to the command line and wrap/control the external process within a pro plugin. This worked then and does work now -- but it is not really ... integrated.

    So, the idea is to implement the formatter plugin for pro using approach a). Pro always comes with a current/latest javac version bundled in its jlink-ed runtime image. It would be nice if gfj could use that javac via the tool interface first. Maybe gjf could introduce an option to use the default "javac" tool?

  3. sormuras commented on Mar 19, 2018

    @sormuras
    ContributorAuthor

    Did my explanation clarify the use case?

  4. cushon commented on Mar 19, 2018

    @cushon
    Collaborator

    Having an option to use other javacs is complicated by the way we're doing the shading. It's possible, but I'm not really understanding why it's necessary. It sounds like (a) works with -all-deps now?

  5. sormuras commented on Mar 20, 2018

    @sormuras
    ContributorAuthor

    (a) works and was merged by @forax lately. Resorting to spawn another VM doesn't feel like it is really integrated, though.

  6. cushon commented on Apr 10, 2018

    @cushon
    Collaborator

    Sorry, I'm still not clear on what the underlying issue is here, or why it was necessary to run GJF in a separate process.

    Do you have a self-contained repro that shows the issue you saw using GJF as a library from a modularized application?

  7. sormuras commented on Apr 10, 2018

    @sormuras
    ContributorAuthor

    Do you have a self-contained repro that shows the issue you saw using GJF as a library from a modularized application?

    Should be available as a local branch... if not, I'll prepare one.

  8. sormuras commented on Apr 10, 2018

    @sormuras
    ContributorAuthor

    DIY

    I compiled a minimal demo. Checkout "bach" and run Build03GoogleJavaFormatAsLibrary by invoking jshell build-03-google-java-format-as-library.jsh in the demo/03-google-java-format-as-library folder. JDK-10 is required.

    TL;DR

    javac-shaded-9+181-r4173-1.jar does not work well as an automatic module. It'd be cool, if google-java-format had only deps to javax.tools... instead of org.openjdk.... One could add requires java.compiler; to the application's module descriptor.

    Setup

    • src/application/foo/Foo.java is the application stub using gjf as a libray.
    package foo;
    
    import com.google.googlejavaformat.java.Formatter;
    
    public class Foo {
    
      public static void main(String... args) throws Exception {
        var sourceString = "package foo; enum Bar {A,B,C}";
        var formattedSource = new Formatter().formatSource(sourceString);
        System.out.println(formattedSource);
      }
    }
    
    • src/application/module-info.java reads com.google.googlejavaformat.
    module application {
      requires com.google.googlejavaformat;
    }
    

    Module- and Class-path

    Working on the --module-path like a charm:

    • google-java-format-1.6-20180410.041304-23.jar
    • guava-24.1-jre.jar

    Needs to live on the --class-path:

    • javac-shaded-9+181-r4173-1.jar
      ...because:
    • JPMS can't convert "file name" -> "module name"
    • doesn't provide Automatic-Module-Name in it's MANIFEST.MF
    • rename manually to "errorprone.jar" and put on the module-path yields:
    java.lang.module.FindException: Unable to derive module descriptor for
                  .bach\module-path\errorprone.jar
              Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class
                  com.sun.tools.javac.platform.JDKPlatformProvider not in module
    
  9. cushon commented on Apr 11, 2018

    @cushon
    Collaborator

    Thanks! I understand the problem better now.

    I was able to reproduce the InvalidModuleDescriptorException by creating a GJF jar with an Automatic-Module-Name. The issue there is a META-INF/services didn't get shaded:

    $ unzip -p .bach/class-path/javac-shaded-9+181-r4173-1.jar META-INF/services/com.sun.tools.javac.platform.PlatformProvider
    com.sun.tools.javac.platform.JDKPlatformProvider
    

    Fixing that to refer to the shaded classes allows your repro to work with GJF on the module path.

    I'll include an Automatic-Module-Name and fix the service loading issue in the next error-prone-javac release.

  10. changed the title [-]Use `javac` via `ToolProvider`[/-] [+]shaded javac should set Automatic-Module-Name and shade service loader entries[/+] on Apr 11, 2018
  11. cushon commented on Mar 25, 2020

    @cushon
    Collaborator

    The Automatic-Module-Name part happened in 4d728e7c, and we're no longer using a shaded javac so the service loader stuff should be fixed also: 3c191c1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions