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

feat: Allow passing environment variables in maven launcher invocations #4830

Merged
merged 12 commits into from
Aug 23, 2022

Conversation

I-Al-Istannen
Copy link
Collaborator

@I-Al-Istannen I-Al-Istannen commented Aug 7, 2022

Some maven builds might need environment variables to complete successfully and we should offer an API for this.

As the methods I overloaded are part of the public API, I enclosed the environment variables in an opaque settings object. This should allow us to add more options later without another overload layer per feature. I am not sure if this is a good idea, but it might ease the burden a bit.

Closes: #4817

@I-Al-Istannen
Copy link
Collaborator Author

Okay, I am stumped. I am not sure why the maven invocation fails, but it only seems to fail on windows. I don't have a usable windows install right now, so I'd be grateful if somebody else could help here :)

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

A few observations.

I can try on Windows when I get home this weekend, I have one sitting around.

Comment on lines 51 to 63
@AfterEach
void tearDown() throws IOException {
// Clean up old classpath files to ensure tests actually re-run properly
try (Stream<Path> paths = Files.walk(Path.of("src/test/resources/maven-launcher"))) {
List<Path> filesToDelete = paths
.filter(it -> it.getFileName().toString().equals("spoon.classpath.tmp"))
.collect(Collectors.toList());

for (Path path : filesToDelete) {
Files.deleteIfExists(path);
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better if the tests were executed in a temporary directory instead (e.g. using @TempDir from JUnit5).

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is something of a systematic error we're doing (i.e. just running in the local directory), but perhaps this PR would be a good driving force to actually start running tests appropriately isolated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea, that was a bit lazy… In exchange I have converted all launcher tests (that aren't disabled) to use temporary folders. The diff is a bit more annoying now but I hope still readable enough.


boolean containsSpoonDependency = Arrays
.stream(launcher.getEnvironment().getSourceClasspath())
.anyMatch(it -> it.matches(".*fr.inria.gforge.spoon.spoon-core.10.1.0.spoon-core-10.1.0.jar"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you entirely certain the classpath will end with this dependency? Perhaps the test fails on Windows because it's somewhere else?

Suggested change
.anyMatch(it -> it.matches(".*fr.inria.gforge.spoon.spoon-core.10.1.0.spoon-core-10.1.0.jar"));
.anyMatch(it -> it.matches(".*fr.inria.gforge.spoon.spoon-core.10.1.0.spoon-core-10.1.0.jar.*"));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good spot, but:
It is an array with one entry per dependency, so it should be fine. The classpath printed in the CI is an empty array, so nothing is found. I could observe that happening locally if the maven invocation failed and did not produce any output, but the invocation request does not seem to throw an exception in such cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It probably failed then. We've had trouble with Maven on Windows before. I'll have a peek once I get home then!

* Additional options for maven invocations.
*/
@Internal
public static class MavenOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the reasoning for making this a nested class in SpoonPom? Barring the fact that it's used by SpoonPom, it doesn't seem intrinsically related.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted to keep this an internal implementation detail and the directory SpoonPom resides in didn't feel adequate (it doesn't have anything to do with compilers). Additionally it is quite closely related so I hoped I could get away with it.

If you feel that's too much of a stretch I can extract it with a more verbose name :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

the directory SpoonPom resides in didn't feel adequate

That we can agree on. It would have to go into an internal subpackage, and that'd put it far away from it's currently singular use. I mostly wanted to see if you'd thought it through, which you have, so let's leave it where it is :)

Why does it work without `env.` on linux???
This is not documented as far as I can tell, but it
works-on-my-machine:tm: and just failed on windows. Let's see.
@I-Al-Istannen
Copy link
Collaborator Author

@slarse Okay, I think I solved it. The docs say that only env.xx is valid, but without env. it works as well in practice. I have no idea why, but I either missed it or that is an undocumented feature.

The problem is that maven normalizes all env variables to upper case – but apparently not the occurences in the pom. So env.foo works on linux, but on windows it is matched against FOO, which fails. This is in the name of "reliability", but after wasting a few hours I disagree quite strongly with that…

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

I was unable to get around to testing this over the weekend, other things came up. But I see you worked it out, and LGTM.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Actually, one last thing ... 🤔

src/main/java/spoon/MavenLauncher.java Outdated Show resolved Hide resolved
Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

@slarse slarse merged commit 65ab20b into INRIA:master Aug 23, 2022
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.

How to pass environment Variables to MavenLauncher
2 participants