Skip to content

Don't warn when JDK path from env var is not a directory#276

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-argumentexception-issue
Open

Don't warn when JDK path from env var is not a directory#276
Copilot wants to merge 3 commits intomainfrom
copilot/fix-argumentexception-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

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), TryGetJdkInfo was propagating the ArgumentException from JdkInfo..ctor as a TraceLevel.Warning, producing noisy, misleading output in MSBuild logs.

Changes

  • JdkInfo.TryGetJdkInfo: Add an early Directory.Exists guard. Paths that don't resolve to a directory are now silently skipped — logged only at Verbose — rather than emitting a Warning.
// Before: ArgumentException from ctor was caught and logged as Warning
internal static JdkInfo? TryGetJdkInfo(string path, ...)
{
    try { jdk = new JdkInfo(path, locator); }
    catch (Exception e) {
        logger(TraceLevel.Warning, ...);  // ← showed up in MSBuild output
    }
}

// After: non-directory paths are skipped before construction
if (!Directory.Exists(path)) {
    logger(TraceLevel.Verbose, $"Skipping JDK path `{path}`...");
    return null;
}
  • New test GetKnownSystemJdkInfos_JiJavaHome_NonDirectoryPath_NoWarning: sets JI_JAVA_HOME to a temp file path and asserts no Warning is logged.
Original prompt

This section details on the original issue you should resolve

<issue_title>ArgumentException looking for JDKs</issue_title>
<issue_description>This happened in a test here:

23:21:22.063   1:7>/Users/runner/android-toolchain/dotnet/packs/Microsoft.Android.Sdk.Darwin/31.0.200-ci.pr.gh6745.77/tools/Xamarin.Android.Tooling.targets(69,5): warning : An exception occurred while validating the Java SDK installation in '/Users/runner/Library/Android/jdk-11' that was found while searching the paths from '$JI_JAVA_HOME'. Ensure that the Android section of the Visual Studio options has a valid Java SDK directory configured. To use a custom SDK path for a command line build, set the 'JavaSdkDirectory' MSBuild property to the custom path. Exception: Not a directory (Parameter 'homePath') [/Users/runner/work/1/a/TestRelease/02-14_22.59.11/temp/NothingToBindclass-parse/UnnamedProject.csproj]
                     System.ArgumentException: Not a directory (Parameter 'homePath')
                        at Xamarin.Android.Tools.JdkInfo..ctor(String homePath, String locator, Action`2 logger) in /Users/builder/azdo/_work/2/s/xamarin-android/external/xamarin-android-tools/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs:line 57
                        at Xamarin.Android.Tools.JdkInfo..ctor(String homePath, String locator) in /Users/builder/azdo/_work/2/s/xamarin-android/external/xamarin-android-tools/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs:line 97
                        at Xamarin.Android.Tools.JdkInfo.TryGetJdkInfo(String path, Action`2 logger, String locator) in /Users/builder/azdo/_work/2/s/xamarin-android/external/xamarin-android-tools/src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs:line 347 (TaskId:57)

The MSBuild log says $JI_JAVA_HOME is:

JI_JAVA_HOME = /Users/runner/Library/Android/jdk-11
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 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.

Copilot AI changed the title [WIP] Fix ArgumentException during JDK validation Don't warn when JDK path from env var is not a directory Feb 20, 2026
Copilot AI requested a review from rmarinho February 20, 2026 19:29
@rmarinho rmarinho force-pushed the copilot/fix-argumentexception-issue branch from 731c702 to 07738c1 Compare February 24, 2026 19:09
@kayodebristol
Copy link

@copilot This JDK warning fix is ready! ✅

Status: CI passing, mergeable=true
Ready for: Immediate review and merge

This appears to be a clean, focused fix for the JDK path warning issue. Consider:

  1. Converting from draft to ready for review
  2. This looks ready for merge given its focused scope

Action needed: Mark ready for review - this one looks good to go!

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

🤖 AI Review Summary

Found 1 issue: placeholder issue link in test comment.

  • Documentation: Test comment references issues/XXX instead of the actual issue number #154 (JdkInfoTests.cs:315)

👍 What's good:

  • Clean, minimal fix — the Directory.Exists guard is the right approach, preventing the misleading Warning from JdkInfo..ctor's ArgumentException.
  • Logging at TraceLevel.Verbose instead 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_HOME env var and cleans up the temp file in a finally block.

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:
Copy link
Member

Choose a reason for hiding this comment

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

🤖 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)

@rmarinho rmarinho force-pushed the copilot/fix-argumentexception-issue branch from 07738c1 to 016037a Compare March 19, 2026 14:21
Copilot AI and others added 2 commits March 23, 2026 18:47
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>
@rmarinho rmarinho force-pushed the copilot/fix-argumentexception-issue branch from 016037a to 7e8f711 Compare March 23, 2026 18:47
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rmarinho rmarinho marked this pull request as ready for review March 23, 2026 18:53
@rmarinho rmarinho requested a review from simonrozsival as a code owner March 23, 2026 18:53
Copilot AI review requested due to automatic review settings March 23, 2026 18:53
Copy link
Contributor

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 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.Exists guard in JdkInfo.TryGetJdkInfo, logging skips at Verbose instead of emitting a Warning via exception handling.
  • Add a new NUnit test to ensure JI_JAVA_HOME pointing 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.");
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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.");

Copilot uses AI. Check for mistakes.
{
// 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.
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

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”).

Suggested change
// no Warning-level message should be logged.
// no Warning-level message should be logged for that non-directory JI_JAVA_HOME path.

Copilot uses AI. Check for mistakes.
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.

ArgumentException looking for JDKs

5 participants