Skip to content

Commit 016037a

Browse files
Copilotrmarinho
andcommitted
Fix ArgumentException warning when JDK path is not a directory
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>
1 parent db0e9d2 commit 016037a

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ public static IEnumerable<JdkInfo> GetSupportedJdkInfos (Action<TraceLevel, stri
358358
internal static JdkInfo? TryGetJdkInfo (string path, Action<TraceLevel, string> logger, string locator)
359359
{
360360
JdkInfo? jdk = null;
361+
if (!Directory.Exists (path)) {
362+
logger (TraceLevel.Verbose, $"Skipping JDK path `{path}` from `{locator}`: path does not exist or is not a directory.");
363+
return jdk;
364+
}
361365
try {
362366
jdk = new JdkInfo (path, locator);
363367
}

tests/Xamarin.Android.Tools.AndroidSdk-Tests/JdkInfoTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,37 @@ public void GetKnownSystemJdkInfos_DiscoversWindowsUserJdk (string folderName)
309309
}
310310
}
311311

312+
[Test]
313+
public void GetKnownSystemJdkInfos_JiJavaHome_NonDirectoryPath_NoWarning ()
314+
{
315+
// Reproduce https://github.com/dotnet/android-tools/issues/XXX:
316+
// If JI_JAVA_HOME points to a path that exists but is not a directory,
317+
// no Warning-level message should be logged.
318+
var file = Path.GetTempFileName ();
319+
var previous = Environment.GetEnvironmentVariable ("JI_JAVA_HOME", EnvironmentVariableTarget.Process);
320+
try {
321+
// file exists but is not a directory
322+
Environment.SetEnvironmentVariable ("JI_JAVA_HOME", file, EnvironmentVariableTarget.Process);
323+
324+
var warnings = new List<string> ();
325+
Action<TraceLevel, string> logger = (level, message) => {
326+
if (level == TraceLevel.Warning)
327+
warnings.Add (message);
328+
};
329+
330+
JdkInfo.GetKnownSystemJdkInfos (logger).ToList ();
331+
332+
// There should be no warning about the non-directory JI_JAVA_HOME path
333+
Assert.IsFalse (warnings.Any (w => w.Contains (file)),
334+
$"Unexpected warning logged for non-directory JI_JAVA_HOME path: {string.Join ("; ", warnings)}");
335+
}
336+
finally {
337+
Environment.SetEnvironmentVariable ("JI_JAVA_HOME", previous, EnvironmentVariableTarget.Process);
338+
if (File.Exists (file))
339+
File.Delete (file);
340+
}
341+
}
342+
312343
[Test]
313344
[TestCase ("microsoft-21.jdk", false)]
314345
[TestCase ("jdk-21", true)]

0 commit comments

Comments
 (0)