dotnet/android

UnauthorizedAccessExceptions are getting raised when building a blank MAUI app when using .NET 9.0

whodges opened this issue · 37 comments

Description

I just moved to MAUI 9 this morning (specifically 9.0.0-preview.6.24327.7). I was using .NET 8.0 prior to this and everything was fine. I noticed that the first build of solution would work fine, but if I did a rebuild all, the build would fail with an UnauthorizedAccessException. The only way I can get it to go away is to close and reopen VS (I'm using 17.11.0 Preview 3.0, for the record, running in admin mode on Win 10). After that, it builds again, but only once before I start getting the UnauthorizedAccessExceptions.

To investigate, I made a blank .NET MAUI 8 app and tried it - everything builds fine. You can 'Rebuild All' as many times as you want and it'll compete successfully.

Then I made a blank .NET MAUI 9 app and built it - it was fine the first time. Then I tried 'Rebuild All' and, unfortunately, ran into the same UnauthorizedAccessExceptions during the build:

Error (active)	XARDF7019	System.UnauthorizedAccessException: Access to the path 'GoogleGson.dll' is denied.
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound, WIN32_FIND_DATA& data)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
   at Xamarin.Android.Tasks.RemoveDirFixed.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs:line 54	MauiApp2 (net9.0-android)	C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.6.340\tools\Xamarin.Android.Common.targets	2503

and

Error (active)	XALNS7019	System.UnauthorizedAccessException: Access to the path 'D:\Projects\MauiApp2\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp2.dll' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalDelete(String path, Boolean checkHost)
   at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125
   at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344
   at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161
   at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76
   at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25	MauiApp2 (net9.0-android)	C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.6.340\tools\Xamarin.Android.Common.targets	1407

These all seem to be Android-releated. And when I try to deploy the successful build to my Android device, I get this error:

Error		XA0127: Error deploying 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll' using 'xamarin.sync: error: could not open 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll'.'.
Please set the 'EmbedAssembliesIntoApk' MSBuild property to 'true' to disable Fast Deployment in the Visual Studio project property pages, or edit the project file in a text editor.

I never got this error with .NET 8.0. Again, this is a fresh MAUI 9.0 app.

Steps to Reproduce

  1. Create a new, default .NET MAUI 9.0 app (use 9.0.0-preview.6.24327.7).
  2. Rebuild it. It should succeed.
  3. Rebuild it again. It should fail with an UnauthorizedAccessException.

Link to public reproduction project repository

https://github.com/whodges/mauiapp1

Version with bug

9.0.0-preview.1.9973

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.70

Affected platforms

Android

Affected platform versions

Android 31

Did you find any workaround?

Close and restart Visual Studio (but you'll only get one build out of it).

Relevant log output

binlog_success.zip
binlog_fail.zip
binlog_deploy_fail.zip

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

I dug in a bit more, trying different versions. Everything builds fine with 8.0.70, and then this rebuild issue appears with 9.0.0-preview.1.9973.

Yep, np. I editted in two attached two binlogs: binlog_success is from the first rebuild, and binlog_fail is from the subsequent rebuilds.

EDIT: Added a third one, which is when I try and deploy the second build. It's the same UnauthorizedAccessException error, but involving different files.

This issue has been verified using Visual Studio 17.11.0 Preview 3 (9.0.0-preview.1.9973 & 9.0.0-preview.6.24327.7). Cannot repro it.
I followed the steps to reproduce, but the problem did not reoccur on both machines (17.11.0 Preview 3+Net 9 preview 5, 17.11.0 Preview 3 +NET9 preview 6). Are there any other special steps? If so, please let me know.

Ok - I just tried it on a completely different computer that I installed the latest .NET 9 SDK on, along with the latest preview version of VS 2022 (9.0.0-preview.6.24327.7 and 17.11.0 Preview 3.0, respectively), and I got the exact same UnauthorizedAccessException involving 'GoogleGson.dll'.

I just made a github of the .NET 9.0 MAUI app itself, but again, it's just the default app that VS creates for you: https://github.com/whodges/mauiapp1.git. Not sure if that helps.

I'm 100% open to doing anything that would shed more light on this for you all. I was going to chalk this up to something involving my environment, but reproducing it on a separate machine concerns me. I totally get though that this is something that would've been noticed early on your end, so why I seem to be the only one experiencing it is definitely odd.

EDIT: Here's the binlog from the other machine:

binlog.zip

EDIT2: A screenshot just because:

exception

EDIT3: And again, if I switch to .NET 8 (MAUI 8.0.70), everything is fine.

Just tried again with 17.11.0 Preview 4.0. Same issue: the first build works, then 'Rebuild Solution' results in Access to the path 'GoogleGson.dll' is denied. Following it with 'Build Solution' results in Access to the path 'D:\Projects\MauiApp1\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp1.dll is denied.

Note that this only happens with Debug builds. Everything is fine with Release.

I get the same error in a MAUI Project with .NET9 and with Visual Studio 17.11 Preview 2.1. With NET8 everything works without problems

The last error:

Error		XA0127: Error deploying 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll' using 'xamarin.sync: error: could not open 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll'.'.
Please set the 'EmbedAssembliesIntoApk' MSBuild property to 'true' to disable Fast Deployment in the Visual Studio project property pages, or edit the project file in a text editor.

Should be fixed in:

Which should hopefully be in .NET 9 Preview 7.

I tried with the .NET 9 Preview 7 build we are working on, I can't seem to reproduce the UnauthorizedAccessException even with 4 Rebuilds:

image

If you are having this issue, can you use a tool like ProcessExplorer to find out what process is locking the file:

System.UnauthorizedAccessException: Access to the path 'GoogleGson.dll' is denied.

I tried both procexp and tasklist; unfortunately neither reports that any program currently has the file in use. I dug in a bit more though and found that every file in obj\Debug\net9.0-android\android\assets\armeabi-v7a is locked (that is, if you try and delete it via Explorer, you'll get a "the file is open in another program" error, though any unlocking utility will report the file as being unlocked). That program is obvious either VS or related to VS, as closing VS unlocks that directory and its files (and a Rebuild will then work - but just once because then obj\Debug\net9.0-android\android\assets\armeabi-v7a gets locked again).

EDIT: I tried this using 17.11.0 Preview 7.0.

@whodges

Do you have any third party Visual Studio extensions installed (just trying to eliminate causes) for things like code formatting or intellisense.
What kind of Anti Virus do you have installed?
Do you use hot reload?

Those files are clearly being locked by VS but its odd that it does not show up in the procexp 🤔

The only non-MS extensions I have are Multiline Search and Replace (Helixsoft) and Xamarin.Android SDK. AV is just MS Defender. But on the other Win 10 system I tried this on, there's no AV and no extensions installed. Yes I have Hot Reload active on both, but just tried disabling it to no avail; the same error occurs.

I tested this on a DevBox/Virtual Machine, and I noticed the project by default is saved on a Dev Drive.

Let me just check if anything different happens on a regular drive where Windows Defender will do its standard scanning on file writes.

@whodges does anything different happen if you have a Windows Defender exclusion for the folder where the project is saved?

Just tried the exclusion; no luck

Still occurs on version 17.12.0 Preview 1.0

I've got similar issue.

17.12.0 Preview 1.0/.NET9 Preview 7 -> New MAUI project -> Build:

Output:
System.UnauthorizedAccessException: Access to the path is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125
at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344
at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161
at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76
at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25

It works when I start VS as Administrator. But after a few builds and run it appears again.
So it looks like file handler is not closed/some process is still running.

Interesting - it happens for me running VS as admin no matter what after the first build.

I also wonder if it could be some regional settings for whatever weird reason? I’m in Canada.

This will help diagnose these types of issues:

But it looks like I should add UnauthorizedAccessException, which is perhaps thrown by File.Delete()?

Hi!
In my situation, MsBuild.exe holds onto .dll files. If I kill that specific MsBuild instance (identified using Process Explorer), I can build the app again.
Additionally, I’ve noticed that if I wait for a minute or so, the process eventually releases the .dll files, allowing me to build once more.
While there’s no specific reproducible scenario, I’ve observed that certain workflows trigger this issue with locked app .dll files (it's not always the app dll, for example once it was the CommunityToolkit.Mvvm.dll):

Case 1:

  1. Build the app.
  2. Run it.
  3. Close the app in the emulator.
  4. Make a small code change to ensure that the app needs recompilation.
  5. Attempt to build and deploy.

Case 2:

  1. The app is running in the emulator.
  2. Make a small code change to ensure that the app needs recompilation.
  3. Press hot reload.
  4. Stop the app.
  5. Attempt to build and deploy.

Case 3:

  1. The app is running in the emulator.
  2. Press the restart button (Ctrl+Shift+F5).
  3. Stop the app.
  4. Make a small code change to ensure that the app needs recompilation.
  5. Attempt to build and deploy.

Important Note: I’ve tried the mentioned scenarios with a blank/sample app using .NET 8.0 LTS, and it works perfectly. Additionally, deployment to the emulator also seems faster.

TargetFrameworks: net9.0-android;net9.0-ios;net9.0-maccatalyst
VS: 17.12.0 Preview 1.0
Emulator: Pixel 5- API 34 (Android 14.0 - API 34)
dotnet --version : 9.0.100-preview.7.24407.12

Exception:

System.UnauthorizedAccessException: Access to the path '<MY PATH :)>\repos\MauiApp2\MauiApp2\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp2.dll' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125
at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344
at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161
at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76
at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25

@pjeremic can you share a .binlog of either 1) a build that worked (but must be locking a file), and 2) the build that locked the file? https://aka.ms/binlog

I've done the scenarios you've listed above, and they work for me, but .NET 9 RC 1 was released the other day, you might also give it a try.

@jonathanpeppers

Sorry for the late reply.
I’ve updated Visual Studio and .NET, but unfortunately, it hasn’t resolved the issue.

BuildLogs.zip

In your case, the file is: obj\Debug\net9.0-android\android\assets\x86_64\MauiApp2.dll

Seems like only two tasks would touch it, looking at these logs:

  • <LinkAssembliesNoShrink/> would do Files.CopyIfChanged()
  • <FastDeploy/> would open the file for reading, and deploy

There are some changes to how the DirectoryAssemblyResolver is Dispose()d in .NET 9:

runState.resolver = new DirectoryAssemblyResolver (this.CreateTaskLogger (), loadDebugSymbols: ReadSymbols, loadReaderParameters: readerParameters);

This is running for a single architecture, x86_64, but I don't think I see an issue in here offhand. It looks like it would be Dispose()d.

/cc @grendello @dellis1972

My guess is with the IDE closed and building from the command line this is not happening.
There is something in the IDE probably a Design Time build which is locking these files it seems.

The reporting for this issue may be improved due to dotnet/android-tools@ab2165da. (Does it work for directories as well as files?)

Maybe this also needs to check Directory.Exists():

I would also need to test in what ways that could happen. I don't offhand remember if .NET has an easy API that locks a directory (like Directory.OpenHandle()???), but there are definitely Windows p/invoke's that could be used.

If it helps, here are my latest success (1st rebuild) and failure (2nd rebuild) binlogs using the latest MAUI workload and VS 17.12.0 Preview 2.1.

success.binlog.zip
failure.binlog.zip

@whodges can you install Project System Tools extension and capture the design time bin logs as well?
Also have you tried to reproduce from just the command line (i.e no IDE open).

The command line (dotnet build --no-incremental) works perfectly fine on its own. If, however, I have the IDE open, and have used it to build the solution once, both the IDE and the command line will fail with the same UnauthorizedAccessException, suggesting that it is indeed the IDE that's locking the Android files.

Design binlogs attached (not sure if I captured them right but I think I did)

design binlogs.zip

@whodges Thanks for the logs. Unfortunately they do not have the error in them?
Both the android based logs have succeeded. If you get a moment try to repo it with the logger on.

The design time logs or the build logs? It seems like the design time logs get generated when you open the solution rather than when you build it?

The design time logs or the build logs? It seems like the design time logs get generated when you open the solution rather than when you build it?

So what we need is all the logs. So open the IDE and kick of the binlogs collection tool. Open your solution and try to repo the issue. Once you get the UnauthorizedAccessException, then stop the logging and send us all the logs.
We need to get an idea of which design time were running at the time the error occured.

Same here, it can't delete the main project dll. It is blocker by an instance of MsBuild

Workaround for now is to keep killing the lingering msbuild processes.

I wonder if there is a way to set the start arguments for the msbuild process which is causing the issue?

I also encountered almost the same problem, but my current solution is to switch to Windows Machine, stop it and switch back to Android, and it will return to normal. In addition, this problem happens occasionally, even if I don't modify any code. I am currently using Visual Studio 2022 v17.12 preview 2, .net 9 rc2, and the project was created using the "maui-blazor-web" template

Corrected logs attached - sorry for the wait

binlogs.zip