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

fix(requirements check): use regex to get java version from javac output #1220

Merged
merged 3 commits into from May 9, 2021

Conversation

DavidStrausz
Copy link
Contributor

@DavidStrausz DavidStrausz commented Apr 26, 2021

Platforms affected

Android

Motivation and Context

Fixes getting the java version from the javac -version output on macOS devices when custom _JAVA_OPTIONS are set. A detailed description of the issue can be found im my comment here.

Fixes #1203

Description

Use a regex to get just the version string from the javac output. Fixes #1130.

Previously semver.coerce() would get a wrong version from a string like the following:

Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271

With the change in this PR, the following regex is used to get the version string (1.8.0) from the output:

/javac\s+([\d.]+)/i

I also tried removing _JAVA_OPTIONS from the version check by adding env: {}, extendEnv: false to the options of the javac command like suggested here:

execa('javac', ['-version'], { all: true, env: {}, extendEnv: false })

This changed the output from:

Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271

to:

javac 12.0.2

which subsequently let the requirements check fail again (no idea where 12.0.2 comes from). So I just went with the regex solution.

Testing

  • Add android platform with fix on macOS, run cordova build android, check if requirements check succeeds
  • Add android platform with fix on Windows, run cordova build android, check if requirements check succeeds
  • Add unit test for javac -version output with _JAVA_OPTIONS, run unit tests

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

@breautek
Copy link
Contributor

I also tried removing _JAVA_OPTIONS from the version check by adding env: {}, extendEnv: false to the options of the javac command like suggested here:

Yah I don't think we can remove the environment variables, as that may contain details like JAVA_HOME used to point to the direction where Cordova should look for a path... otherwise it will find the first java variable in your path and call it a day which may not be what we want. ;)

@breautek
Copy link
Contributor

breautek commented May 9, 2021

Will merge by tomorrow night if there are no objections.

Copy link
Contributor

@PieterVanPoyer PieterVanPoyer left a comment

Choose a reason for hiding this comment

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

Looks good. I did not test it.

@breautek
Copy link
Contributor

breautek commented May 9, 2021

Thanks for the review @PieterVanPoyer and thank you for your contribution @DavidStrausz

@breautek breautek merged commit a458043 into apache:master May 9, 2021
@breautek breautek mentioned this pull request May 9, 2021
5 tasks
raphinesse added a commit that referenced this pull request Jun 23, 2021
This basically fixes up the changes from #1220.

* test(env/java): replace test that duplicates implementation
* test(env/java): stub _ensure to focus on unit under test
* test(env/java): add test for invalid output
* refactor(env/java): keep try block small
* refactor(env/java): shorten excessive comment
wedgberto pushed a commit to wedgberto/cordova-android that referenced this pull request May 17, 2022
…put (apache#1220)

* fix(requirements check): use regex to get java version from javac output

* fix(lint): format code

* fix(node 10): remove optional chaining from version check
wedgberto pushed a commit to wedgberto/cordova-android that referenced this pull request May 17, 2022
This basically fixes up the changes from apache#1220.

* test(env/java): replace test that duplicates implementation
* test(env/java): stub _ensure to focus on unit under test
* test(env/java): add test for invalid output
* refactor(env/java): keep try block small
* refactor(env/java): shorten excessive comment
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.

java checks in check_reqs.js is incompatible with "javac -version" output
3 participants