dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

FilePicker on MacOS does not always return the file picked #11088

Closed ozster123 closed 1 year ago

ozster123 commented 2 years ago

Description

While using FilePicker on MacOS it will regularly return null for the FileResult when a file is chosen. Code below. After a few selects it works. No exceptions or crashes. Any ideas?

Version 17.4 Preview (17.4 build 2366) Runtime .NET 6.0.9 (64-bit) Architecture: X64

        [RelayCommand]
    private async void SelectFile()
    {
        FileResult result = null;
            try
            {
                FilePickerFileType filePickerFileType = new FilePickerFileType(
                    new Dictionary<DevicePlatform, IEnumerable<string>>{
                    { DevicePlatform.MacCatalyst, new [] { "json" } },
                    { DevicePlatform.macOS, new [] { "json"} },
                    { DevicePlatform.WinUI, new [] { ".json" } }
                    });
                PickOptions pickOptions = new PickOptions
                {
                    PickerTitle = "Choose an engine file.",
                    FileTypes = filePickerFileType
                };

                result = await FilePicker.Default.PickAsync(pickOptions);
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0}", e.Message));
            }
            if (null != result)
        {
<Button Text="Select..." Command="{Binding SelectFileCommand}"/>

Steps to Reproduce

Create a new .net Maui App Add a button. Add a relay command for the button. Use code above to select a file.

Link to public reproduction project repository

n/a

Version with bug

7.0 Release Candidate 2

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

macOS 12, 13

Did you find any workaround?

No

Relevant log output

+[CATransaction synchronize] called within transaction
ghost commented 2 years ago

Hi @ozster123. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ozster123 commented 2 years ago

I have created a minimal example https://github.com/ozster123/FilePickerTest.

I switched back to Visual Studio Community 2022 for Mac Version 17.3.8 (build 5)

Runtime .NET 6.0.5 (64-bit) Architecture: X64

This shows the problem usually first or second select.

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

jfversluis commented 2 years ago

Thanks for adding the repro!

Zetthard commented 2 years ago

I have the same issue as #11462, FilePicker.Default.PickAsync always returns null and is not being awaited by caller. MacOS 13 x64; VS for Mac 2022 17.4 .net SDK 7.0.100; App target platform .net 6; build for MacCatalyst

I tried to use Task.Result instead of await, but it stalls the app and never brings up the file dialog at all.

datvm commented 2 years ago

Yep, I encountered it as well (see the closed duplicate issue #11574 ). Calling FilePicker.PickMultipleAsync on Mac shows the Open dialog. However before user has anything to do with the dialog, the code jumps to the next instruction right away (my code has await).

stephenbonar commented 1 year ago

I have a similar issue, although in my case the method doesn't even return null. If I set a breakpoint on a call such as:

var result = await FilePicker.Default.PickAsync();

The code never reaches the next line where I evaluate the result for null. The OS file picker does launch, but after selecting the file, the method has already terminated. No exceptions are generated, and I can see threads starting and stopping in the Application Output. But that's it. If I re-run the method, it works successfully the second time.

gcadmes-extron commented 1 year ago

Any status updates for this item? Currently the FilePicker on macOS is unusable.

MartyIX commented 1 year ago

I have hit the same issue. Quite unexpected.

MartyIX commented 1 year ago

Testing with the latest commit of the main branch, it looks to me that it works on macOS in Essentials samples. See:

It would be great if somebody could verify it as well.

gcadmes-extron commented 1 year ago

It seems if I remove all breakpoints and let it run, sometimes the issue doesn't happen. But most of the time, the FilePicker is unusable in its current state.

MartyIX commented 1 year ago

For me it's more like "almost never picks the file and occasionally it does" so time is important. I guess the code does not wait for something.

datvm commented 1 year ago

I wrote this package to "fix" these problems in the mean time: datvm/LukeMauiFilePicker. Basically I re-implement them on platforms that the official APIs are having trouble with. Also support a Save Picker.

MartyIX commented 1 year ago

@datvm So maybe you can fix the MAUI's macOS implementation for everyone to benefit? :)))

datvm commented 1 year ago

@MartyIX sure that's a great suggestion, I guess I could try. I have never contributed to MS's repo before though, so I will need to get myself familiar with it. Though I wonder if we do get a PR, how long until they release such a fix?

MartyIX commented 1 year ago

I have never contributed to MS's repo before though, so I will need to get myself familiar with it.

It's just creating a PR really.

Though I wonder if we do get a PR, how long until they release such a fix?

IMO there is no definitive answer to that. I would be hopeful that it would take a month. However, once the fix is merged, one can use it thanks to https://github.com/dotnet/maui/blob/main/.github/DEVELOPMENT.md#testing-branch-against-your-project (I use it quite often for macOS but that's just me).

MartyIX commented 1 year ago

On my macOS Ventura, I can see that this is the culprit:

https://github.com/dotnet/maui/blob/ae20455793142ae0034bcfdf8f810ca66aec782a/src/Essentials/src/FilePicker/FilePicker.ios.cs#L41-L45

(Deleting the code seems to fix the issue)

The two delegates compete with each other and if this delegate wins, then no file is picked.

cdavidyoung commented 1 year ago

I am having a similar problem. The FilePicker was working fine on MacOS (as well as the other platforms) but it suddenly stopped working on just this one platform. Hopefully https://github.com/dotnet/maui/pull/13814 will fix this. Thanks @MartyIX !

MartyIX commented 1 year ago

@cdavidyoung What is your macOS version please? I wonder if the issue manifests only on the newest macOS releases or whether it affects even older ones.

cdavidyoung commented 1 year ago

Ventura 13.2.1

cdavidyoung commented 1 year ago

I tried it on Monterey 12.6.3 on a 2014 Intel Core i5 Mac Mini as well and FilePicker hangs there as well.

mattleibow commented 1 year ago

I just tried on Monterey 12.6.3 and it seems to work.

Screenshot 2023-03-17 at 3 51 14 PM

I copied the snippet of code from this issue and no hangs and gets the result.

What happens if you create a blank app and put the code in some button click?

cdavidyoung commented 1 year ago

I have already tried it on the standard Maui app. When I clicked the button it was able to pick correctly.

As I mentioned before this used to work with my app as well. I think it must be some sort of race condition that makes it stop working in some cases.

MartyIX commented 1 year ago

As I mentioned before this used to work with my app as well. I think it must be some sort of race condition that makes it stop working in some cases.

I have already described the race here.

MartyIX commented 1 year ago

@mattleibow

I just tried on Monterey 12.6.3 and it seems to work.

Is it possible that the bug manifests only on macOS Ventura?

cdavidyoung commented 1 year ago

I have tried my app on both Ventura and Monterey. The FilePicker hangs in both cases.

mattleibow commented 1 year ago

@cdavidyoung can you attach a sample of this happening? And also the results of dotnet --info and dotnet workload --info? I am testing this on my mac, but I am assuming something else is different with the SDK and/or something in my test app.

When you say "hang", you mean the await never returns or the app freezes?

cdavidyoung commented 1 year ago

Hang means in my app it does not return from the call to FilePicker.

I can't provide a sample of this happening because it works fine in the standard Maui app. I wish I could. It only happens in my app.

mattleibow commented 1 year ago

Do you have any third party things that pop up dialogs? If you move this code to say a blank page in your app does it happen? Or if you swap out the main page in the app for a blank one does it happen?

It clearly is affecting things, but it seems that something else may be breaking us. We can see about the PR, but that may hide other issues.

MartyIX commented 1 year ago

Not a question for me but ...

Do you have any third party things that pop up dialogs?

I don't use any 3rd party things. I have a pure MAUI application and on mac arm64 Ventura, the issue is very easy to reproduce as the bug manifests almost always (say 4 times out of 5 attempts). So that might be an easy thing to try.

cdavidyoung commented 1 year ago

As another data point, I just recreated my project from scratch starting with the standard Maui app. I then copied and merged all my project files. This fixed the FilePicker problem that I was having with the Mac version. It runs perfectly on my Mac except that there is some kind of provisioning problem when I try to upload the release version to the App Store.

So, if you are having a problem with the FilePicker recreating your project from scratch may fix it. CacheAll is the name of the app if you want to check it out for Android (Galaxy Store), iOS, and Windows.

bartimusprimed commented 1 year ago

Well after like an hour of me thinking I am stupid and doing something wrong, I can confirm I have this issue as well. Running the file picker twice will result it in running the second time. Seems like the first delegate is hanging as mentioned by @MartyIX

LukeFranky commented 1 year ago

Same thing is happening on iOS 16.1.1 & VS 2022 17.6.1. Sometimes selecting a file works, sometimes it doesn't (returns null).

Super frustrating.

brightertools commented 1 year ago

I also have this issue on MacOS, it works in dev, but not when deployed via TestFlight.. works fine on windows and after deployed via the Windows store.

Is there a definitive fix for this?

ghost commented 1 year ago

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!