Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"async void" in a Model class breaks the build #19344

Open
LegacyNsfw opened this issue Jan 28, 2025 · 0 comments
Open

"async void" in a Model class breaks the build #19344

LegacyNsfw opened this issue Jan 28, 2025 · 0 comments
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification

Comments

@LegacyNsfw
Copy link

LegacyNsfw commented Jan 28, 2025

Current behavior

See repro steps.

Expected behavior

A clean build.

How to reproduce it (as minimally and precisely as possible)

See the AsyncVoid branch of this repo:
https://github.com/LegacyNsfw/UnoExample-1/tree/AsyncVoid

Repro steps:

  1. Create a new Uno project with all of the default settings
  2. Get a successful build (just to be sure)
  3. In MainModel.cs, add this: public async void WhateverFunctionName() { await Task.Delay(1); }
  4. DO NOT ADD ANY CODE THAT CALLS THIS NEW METHOD
  5. Build again

Results:

\obj\Debug\net8.0-desktop\Uno.Extensions.Reactive.Generator\Uno.Extensions.Reactive.Generator.FeedsGenerator\UnoExample.Presentation.MainModel.Bindable.g.cs(69,8,69,46): error CS4008: Cannot await 'void'

The same error is repeated for each of the targets (browser, android, ios, etc).

Apparently the code generator added code to call that new method.

Workaround

Using "async Task" is of course preferred to "async void" but please see the notes below.

Works on UWP/WinUI

None

Environment

No response

NuGet package version(s)

No response

Affected platforms

No response

IDE

Visual Studio 2022

IDE version

Version 17.12.3

Relevant plugins

Uno platform extension 1.0

Anything else we need to know?

Yes I am aware that "async void" is not a best practice. But, hear me out...

I ran into this when using a library that requires the application to implement callbacks through an interface that has void methods.

The most natural place to implement those methods is in a Model class, because the callbacks update the UI.

My implementation of those callbacks needs async.

So here I am doing something that is admittedly not a best practice - due to an interface defined by another library.

I don't think it's reasonable for Uno to be so draconian about enforcing "async Task" for methods that are not being invoked by anything at all. These methods were never going to be invoked by generated code. In the application, they were going to be invoked by the library that uses the interface for callbacks, but in the minimal repro repo the method in question is not being called by anything. Except, apparently, by generated code.

There's just no reason for the build to break due to the mere existence of an unused async void method.

Also, as with my previous bug report, the generated file was deleted by the build process, otherwise I'd include it here. Again, I think it's a mistake to delete those files.

@LegacyNsfw LegacyNsfw added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

No branches or pull requests

1 participant