maui icon indicating copy to clipboard operation
maui copied to clipboard

FilePicker on MacOS does not always return the file picked

Open ozster123 opened this issue 3 years ago • 20 comments

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

ozster123 avatar Nov 03 '22 14:11 ozster123

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.

ghost avatar Nov 03 '22 19:11 ghost

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.

ozster123 avatar Nov 04 '22 11:11 ozster123

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.

ghost avatar Nov 04 '22 15:11 ghost

Thanks for adding the repro!

jfversluis avatar Nov 04 '22 15:11 jfversluis

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.

Zetthard avatar Nov 18 '22 18:11 Zetthard

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

datvm avatar Nov 24 '22 05:11 datvm

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.

stephenbonar avatar Jan 02 '23 05:01 stephenbonar

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

gcadmes-extron avatar Feb 10 '23 04:02 gcadmes-extron

I have hit the same issue. Quite unexpected.

MartyIX avatar Feb 10 '23 09:02 MartyIX

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

  • https://github.com/dotnet/maui/blob/e31e947a247d6edf2fc108218f12d46ff82b3272/src/Essentials/samples/Samples/ViewModel/FilePickerViewModel.cs#L136-L174
  • https://github.com/dotnet/maui/blob/e31e947a247d6edf2fc108218f12d46ff82b3272/src/Essentials/samples/Samples/View/FilePickerPage.xaml

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

MartyIX avatar Feb 10 '23 12:02 MartyIX

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.

gcadmes-extron avatar Feb 10 '23 12:02 gcadmes-extron

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.

MartyIX avatar Feb 10 '23 12:02 MartyIX

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.

datvm avatar Feb 10 '23 17:02 datvm

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

MartyIX avatar Feb 10 '23 22:02 MartyIX

@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?

datvm avatar Feb 11 '23 01:02 datvm

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 avatar Feb 11 '23 09:02 MartyIX

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.

MartyIX avatar Mar 10 '23 01:03 MartyIX

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 !

cdavidyoung avatar Mar 11 '23 12:03 cdavidyoung

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

MartyIX avatar Mar 11 '23 12:03 MartyIX

Ventura 13.2.1

cdavidyoung avatar Mar 11 '23 13:03 cdavidyoung

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

cdavidyoung avatar Mar 12 '23 23:03 cdavidyoung

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?

mattleibow avatar Mar 17 '23 13:03 mattleibow

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.

cdavidyoung avatar Mar 17 '23 16:03 cdavidyoung

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 avatar Mar 17 '23 16:03 MartyIX

@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?

MartyIX avatar Mar 17 '23 16:03 MartyIX

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

cdavidyoung avatar Mar 17 '23 16:03 cdavidyoung

@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?

mattleibow avatar Mar 17 '23 19:03 mattleibow

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.

cdavidyoung avatar Mar 17 '23 21:03 cdavidyoung

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.

mattleibow avatar Mar 19 '23 13:03 mattleibow

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.

MartyIX avatar Mar 21 '23 20:03 MartyIX