Don't warn when JDK path from env var is not a directory#276
Don't warn when JDK path from env var is not a directory#276
Conversation
731c702 to
07738c1
Compare
|
@copilot This JDK warning fix is ready! ✅ Status: CI passing, mergeable=true This appears to be a clean, focused fix for the JDK path warning issue. Consider:
Action needed: Mark ready for review - this one looks good to go! |
jonathanpeppers
left a comment
There was a problem hiding this comment.
🤖 AI Review Summary
Found 1 issue: placeholder issue link in test comment.
- Documentation: Test comment references
issues/XXXinstead of the actual issue number#154(JdkInfoTests.cs:315)
👍 What's good:
- Clean, minimal fix — the
Directory.Existsguard is the right approach, preventing the misleadingWarningfromJdkInfo..ctor'sArgumentException. - Logging at
TraceLevel.Verboseinstead of silently swallowing is good practice. - Test properly creates a real temp file (not just a non-existent path), which reproduces the exact broken-symlink/file scenario from the original issue.
- Test correctly restores the
JI_JAVA_HOMEenv var and cleans up the temp file in afinallyblock.
Review generated by android-tools-reviewer from review guidelines by @jonathanpeppers.
| [Test] | ||
| public void GetKnownSystemJdkInfos_JiJavaHome_NonDirectoryPath_NoWarning () | ||
| { | ||
| // Reproduce https://github.com/dotnet/android-tools/issues/XXX: |
There was a problem hiding this comment.
🤖 Documentation — This references issues/XXX but should link to the actual issue #154 that this PR fixes.
Rule: Docs describe intent not reality (Postmortem #50)
07738c1 to
016037a
Compare
When JI_JAVA_HOME (or another JDK path source) points to a path that exists but is not a directory, TryGetJdkInfo now checks Directory.Exists before constructing JdkInfo. Non-directory paths are skipped silently (logged at Verbose only) instead of generating a Warning in MSBuild output. Fixes: ArgumentException "Not a directory" spurious warning Co-authored-by: rmarinho <1235097+rmarinho@users.noreply.github.com>
016037a to
7e8f711
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR reduces noisy MSBuild warnings during JDK discovery by skipping paths that exist but aren’t directories (e.g., files or broken symlinks) before attempting to construct JdkInfo, and adds coverage for the regression case.
Changes:
- Add an early
Directory.Existsguard inJdkInfo.TryGetJdkInfo, logging skips atVerboseinstead of emitting aWarningvia exception handling. - Add a new NUnit test to ensure
JI_JAVA_HOMEpointing to a non-directory path does not generate a warning.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs | Adds a directory-existence guard to avoid warning-level logs when a candidate JDK path isn’t a directory. |
| tests/Xamarin.Android.Tools.AndroidSdk-Tests/JdkInfoTests.cs | Adds a regression test covering JI_JAVA_HOME set to a temp file path to ensure no warning is logged for that path. |
| { | ||
| JdkInfo? jdk = null; | ||
| if (!Directory.Exists (path)) { | ||
| logger (TraceLevel.Verbose, $"Skipping JDK path `{path}` from `{locator}`: path does not exist or is not a directory."); |
There was a problem hiding this comment.
The new verbose message wraps locator in backticks, but several call sites already pass locator strings that include backticks (e.g. "/usr/libexec/java_home -X"), which results in doubled quoting like /usr/libexec/java_home -X in logs. Consider removing the extra backticks around {locator} (or ensuring locators are passed without embedded backticks) to keep verbose output readable/consistent.
| logger (TraceLevel.Verbose, $"Skipping JDK path `{path}` from `{locator}`: path does not exist or is not a directory."); | |
| logger (TraceLevel.Verbose, $"Skipping JDK path `{path}` from {locator}: path does not exist or is not a directory."); |
| { | ||
| // Reproduce https://github.com/dotnet/android-tools/issues/154: | ||
| // If JI_JAVA_HOME points to a path that exists but is not a directory, | ||
| // no Warning-level message should be logged. |
There was a problem hiding this comment.
The test comment says “no Warning-level message should be logged”, but the assertion only verifies that no warning contains the temp file path (warnings from other discovery sources could still occur). Consider rewording the comment to match what’s actually asserted (e.g., “no warning should be logged for the non-directory JI_JAVA_HOME path”).
| // no Warning-level message should be logged. | |
| // no Warning-level message should be logged for that non-directory JI_JAVA_HOME path. |
When
JI_JAVA_HOME(or other JDK path sources) points to a path that exists but is not a directory (e.g., a file or broken symlink),TryGetJdkInfowas propagating theArgumentExceptionfromJdkInfo..ctoras aTraceLevel.Warning, producing noisy, misleading output in MSBuild logs.Changes
JdkInfo.TryGetJdkInfo: Add an earlyDirectory.Existsguard. Paths that don't resolve to a directory are now silently skipped — logged only atVerbose— rather than emitting aWarning.GetKnownSystemJdkInfos_JiJavaHome_NonDirectoryPath_NoWarning: setsJI_JAVA_HOMEto a temp file path and asserts noWarningis logged.Original prompt
ArgumentExceptionlooking for JDKs #154💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.