Skip to content

Fix TestRunDirectories crash on Android with relative/empty paths#7787

Merged
Evangelink merged 2 commits intomicrosoft:mainfrom
jonathanpeppers:jonathanpeppers/fix-testrundirectories-android-crash
Apr 24, 2026
Merged

Fix TestRunDirectories crash on Android with relative/empty paths#7787
Evangelink merged 2 commits intomicrosoft:mainfrom
jonathanpeppers:jonathanpeppers/fix-testrundirectories-android-crash

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Member

Continuation of #7772.

On .NET for Android, Assembly.Location returns a relative path like "AndroidTest1.dll" (MonoVM) or an empty string (CoreCLR). Path.GetDirectoryName returns "" for these inputs, and then Directory.CreateDirectory("") throws ArgumentException.

This would fail in Release mode on Android, or if the "fast deployment" feature is disabled.

Guard against empty/relative paths by checking the result of Path.GetDirectoryName and falling back to the standard RootDeploymentDirectory/Out path.

Full stack trace from AZDO build
(https://dev.azure.com/dnceng-public/public/_build/results?buildId=1392743):

INSTRUMENTATION_RESULT: error=System.ArgumentException: The value cannot be an empty string. (Parameter 'path')
 at System.ArgumentException.ThrowNullOrEmptyException(String argument, String paramName)
 at System.ArgumentException.ThrowIfNullOrEmpty(String argument, String paramName)
 at System.IO.Directory.CreateDirectory(String path)
 at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.FileUtility.CreateDirectoryIfNotExists(String directory)
 at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.DeploymentUtilityBase.CreateDeploymentDirectories(IRunContext runContext, String firstTestSource)
 at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestDeployment.Deploy(IEnumerable`1 testCases, IRunContext runContext, IFrameworkHandle frameworkHandle, ITestSourceHandler testSourceHandler, TestRunCancellationToken cancellationToken)
 at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTestsAsync(...)
 at Microsoft.VisualStudio.TestTools.UnitTesting.MSTestBridgedTestFramework.SynchronizedRunTestsAsync(...)
 at Microsoft.Testing.Extensions.VSTestBridge.SynchronizedSingleSessionVSTestBridgedTestFramework.ExecuteRequestAsync(...)
 at Microsoft.Testing.Platform.Builder.TestApplication.RunAsync()
 at DotNetNewAndroidTestMonoVM.TestInstrumentation.<OnStart>b__2_0()
INSTRUMENTATION_CODE: 0

Related: dotnet/android#11195 (random PR I saw this), #7769

/cc @nohwnd

Continuation of microsoft#7772.

On .NET for Android, `Assembly.Location` returns a relative path
like `"AndroidTest1.dll"` (MonoVM) or an empty string (CoreCLR).
`Path.GetDirectoryName` returns `""` for these inputs, and then
`Directory.CreateDirectory("")` throws `ArgumentException`.

This would fail in Release mode on Android, or if the "fast
deployment" feature is disabled.

Guard against empty/relative paths by checking the result of
`Path.GetDirectoryName` and falling back to the standard
`RootDeploymentDirectory/Out` path.

Full stack trace from AZDO build
(https://dev.azure.com/dnceng-public/public/_build/results?buildId=1392743):

    INSTRUMENTATION_RESULT: error=System.ArgumentException: The value cannot be an empty string. (Parameter 'path')
     at System.ArgumentException.ThrowNullOrEmptyException(String argument, String paramName)
     at System.ArgumentException.ThrowIfNullOrEmpty(String argument, String paramName)
     at System.IO.Directory.CreateDirectory(String path)
     at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.FileUtility.CreateDirectoryIfNotExists(String directory)
     at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities.DeploymentUtilityBase.CreateDeploymentDirectories(IRunContext runContext, String firstTestSource)
     at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestDeployment.Deploy(IEnumerable`1 testCases, IRunContext runContext, IFrameworkHandle frameworkHandle, ITestSourceHandler testSourceHandler, TestRunCancellationToken cancellationToken)
     at Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.MSTestExecutor.RunTestsAsync(...)
     at Microsoft.VisualStudio.TestTools.UnitTesting.MSTestBridgedTestFramework.SynchronizedRunTestsAsync(...)
     at Microsoft.Testing.Extensions.VSTestBridge.SynchronizedSingleSessionVSTestBridgedTestFramework.ExecuteRequestAsync(...)
     at Microsoft.Testing.Platform.Builder.TestApplication.RunAsync()
     at DotNetNewAndroidTestMonoVM.TestInstrumentation.<OnStart>b__2_0()
    INSTRUMENTATION_CODE: 0

Related: dotnet/android#11195, microsoft#7769

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 23, 2026 21:44
Copy link
Copy Markdown
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

Fixes an Android-specific crash in MSTest adapter deployment directory creation when Assembly.Location is empty or relative (Android CoreCLR/MonoVM), by ensuring OutDirectory falls back to the standard <RootDeploymentDirectory>\Out path.

Changes:

  • Update TestRunDirectories to ignore empty/relative (no directory component) firstTestSource values when computing OutDirectory.
  • Add unit tests covering firstTestSource == "" and firstTestSource == "MyTests.dll" fallback behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Adapter/MSTestAdapter.PlatformServices/Deployment/TestRunDirectories.cs Adds guarded computation of OutDirectory to avoid Directory.CreateDirectory("") on Android scenarios.
test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Deployment/TestRunDirectoriesTests.cs Adds regression tests validating fallback behavior for empty/relative firstTestSource.

Comment thread src/Adapter/MSTestAdapter.PlatformServices/Deployment/TestRunDirectories.cs Outdated
Copy link
Copy Markdown
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

@jonathanpeppers would you mind helping me setup some E2E test for android so we can have something to catch issues/regressions? I am all good with having it as a follow-up.

Copy link
Copy Markdown
Member

@Evangelink Evangelink 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 moving forward with a merge, please keep raising issues/PR if you face other problems. I renew my request for some E2E test :)

Thank you!

@Evangelink Evangelink merged commit 7de5c0f into microsoft:main Apr 24, 2026
157 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 24, 2026
@jonathanpeppers
Copy link
Copy Markdown
Member Author

would you mind helping me setup some E2E test for android so we can have something to catch issues/regressions? I am all good with having it as a follow-up.

@Evangelink I wouldn't recommend doing it here, just because booting Android emulators are pretty flaky. We do have an end-to-end test in the Android repo where I found this.

Are the packages here pushed to Maestro/darc? The dotnet/android repo could consume builds weekly (maybe not daily), and that would be pretty easy to setup.

@Evangelink
Copy link
Copy Markdown
Member

Yes, look at the test-tools feed (see https://github.com/microsoft/testfx/blob/main/docs/preview.md)

@jonathanpeppers
Copy link
Copy Markdown
Member Author

Ok, I see MSTest 4.3.0-preview.26224.2 on the ".NET Core Tooling Dev" darc channel, "test-tools" NuGet feed. I'll give it a try.

@jonathanpeppers
Copy link
Copy Markdown
Member Author

This will let me dogfood the fix here as well, thanks:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants