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

run formatter in fork - run on java 16+ without --add-exports flags #140

Merged
merged 21 commits into from
Mar 14, 2022

Conversation

danielnorberg
Copy link
Contributor

Remove the need to add a .mvn/jvm.config with --add-exports flags when using Java 16+

This is achieved by running the formatter in a java subprocess with the necessary --add-exports specified. The forking logic was adapted from https://github.com/spotify/flo/blob/master/flo-runner%2Fsrc%2Fmain%2Fjava%2Fcom%2Fspotify%2Fflo%2Fcontext%2FForkingExecutor.java. We could also consider putting the forking logic in a separate library for easier reuse. Presumably we might encounter the same problem again.

Alternative to #139

@klaraward
Copy link
Contributor

In the light of #141, do you want to make any changes to this @danielnorberg ?

@klaraward
Copy link
Contributor

Also, suggest we add a config to enable forking, setting it to false by default (and possibly to true for JDK 16+ ?)

@danielnorberg
Copy link
Contributor Author

Yeah, I think the way this sets up the classpath when forking will not actually work when running as a maven plugin.

@danielnorberg danielnorberg marked this pull request as draft March 8, 2022 11:43
@danielnorberg
Copy link
Contributor Author

I think I've now managed to resolve the classpath problem, but would want integration tests that run the plugin as a real maven plugin.

@danielnorberg
Copy link
Contributor Author

Fwiw I've tested a snapshot build of this PR locally and it seems to work as expected.

@danielnorberg
Copy link
Contributor Author

danielnorberg commented Mar 9, 2022

fwiw the integration tests pass locally with corretto 11 & 17 (x86) when rebasing this on #145

final ProcessBuilder processBuilder =
new ProcessBuilder(java.toString(), "-cp", classPathArg).directory(workdir.toFile());

// Propagate -Xmx and -D.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not any -XX etc?

Copy link
Contributor Author

@danielnorberg danielnorberg Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. That’s a remnant from the flo use case. If this was invoking google-java-format as a cli then I’d be naively leaning towards not propagating any flags at all.

But maybe it’d be useful to propagate all flags? Not sure 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propagating Xmx as-is might even be problematic if it’s high.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what surefire etc does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The surefire docs are confusing on this point but having stepped through the surefire execution of forks with a debugger and having inspected the precise command line and serialized property files etc I feel confident in saying that surefire does not automatically propagate any properties, not even -Xmx etc. One has to explicitly allow-list specific properties to propagate.

Maybe we should consider conservatively not propagating properties here unless we see a specific need for it in google-java-format.

@danielnorberg
Copy link
Contributor Author

Perhaps we should also test this on windows. Does GitHub actions support that?

@klaraward
Copy link
Contributor

Perhaps we should also test this on windows. Does GitHub actions support that?

Have no idea, but hopefully? https://blog.devgenius.io/write-your-github-actions-workflow-for-build-windows-application-94e5a989f477

@danielnorberg
Copy link
Contributor Author

danielnorberg commented Mar 10, 2022

Re windows, here goes nothing #146

Daniel Norberg and others added 14 commits March 11, 2022 12:47
@danielnorberg
Copy link
Contributor Author

It blows my mind a bit that this just works on windows on the first try. Java is amazing.

@danielnorberg danielnorberg marked this pull request as ready for review March 11, 2022 17:44
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

Successfully merging this pull request may close these issues.

2 participants