Thanks to visit codestin.com
Credit goes to github.com

Skip to content

pick-kotlin-version.py: tolerate warnings #19865

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged

Conversation

cklin
Copy link
Contributor

@cklin cklin commented Jun 24, 2025

This PR changes pick-kotlin-version.py to use re.search() instead of re.match(), so that it can better cope with warning messages.

Running pick-kotlin-version.py with JRE 24, without this PR:

Traceback (most recent call last):
  File "codeql/java/kotlin-extractor/pick-kotlin-version.py", line 31, in <module>
    raise Exception(f'Cannot detect version of kotlinc (got {res.stderr})')
Exception: Cannot detect version of kotlinc (got WARNING: A restricted method in java.lang.System has been called
WARNING: java.lang.System::load has been called by org.fusesource.jansi.internal.JansiLoader in an unnamed module
WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
WARNING: Restricted methods will be blocked in a future release unless native access is enabled

info: kotlinc-jvm 2.2.0 (JRE 24.0.1)
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by com.intellij.util.containers.Unsafe
WARNING: Please consider reporting this to the maintainers of class com.intellij.util.containers.Unsafe
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
)

Running pick-kotlin-version.py with JRE 24, with this PR:

2.2.0

This commit changes pick-kotlin-version.py to use re.search() instead of
re.match(), so that it can better cope with warning messages.
@cklin cklin marked this pull request as ready for review June 24, 2025 15:21
@Copilot Copilot AI review requested due to automatic review settings June 24, 2025 15:21
@cklin cklin requested a review from a team as a code owner June 24, 2025 15:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the Kotlin version detection script to use re.search() instead of re.match(), allowing it to find the version string even when warning lines precede it.

  • Switch from re.match() to re.search() in the version regex
  • Simplify the regex by removing leading/trailing wildcard matches
  • Preserve existing exception handling for parsing failures
Comments suppressed due to low confidence (1)

java/kotlin-extractor/pick-kotlin-version.py:29

  • Add a unit test for pick-kotlin-version.py that simulates kotlinc -version output containing warning lines before the version string, to ensure the regex continues to match correctly.
m = re.search(r' kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) ', res.stderr)

@@ -26,7 +26,7 @@ def version_tuple(v):
res = subprocess.run([kotlinc, "-version"], text=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
if res.returncode != 0:
raise Exception(f"kotlinc -version failed: {res.stderr}")
m = re.match(r'.* kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) .*', res.stderr)
m = re.search(r' kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) ', res.stderr)
Copy link
Preview

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

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

Improve the regex to use word boundaries and flexible whitespace, e.g. r"\bkotlinc-jvm\s+([0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z][A-Za-z0-9]*)?)\b", to make it more robust against punctuation or line breaks.

Suggested change
m = re.search(r' kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) ', res.stderr)
m = re.search(r"\bkotlinc-jvm\s+([0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z][A-Za-z0-9]*)?)\b", res.stderr)

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@igfoo igfoo left a comment

Choose a reason for hiding this comment

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

I'm not sure if these warnings indicate that something else will break, but from the Kotlin side it looks fine.

@cklin cklin merged commit 9a064de into main Jun 24, 2025
12 checks passed
@cklin cklin deleted the cklin/pick-kotlin-version branch June 24, 2025 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants