Skip to content

Conversation

@GerardSmit
Copy link
Contributor

Before the directives weren't processed, which is causing problems.

For example, I have the following code:

using System;
namespace Test;
public partial class DisposeTest
{
    [CreateSyncVersion]
    public async Task DisposeAsync(object? obj)
    {
#if !SYNC_ONLY
            if (obj is IAsyncDisposable asyncDisposable)
            {
                await asyncDisposable.DisposeAsync();
                return;
            }
#endif

            if (obj is IDisposable disposable)
            {
                disposable.Dispose();
                return;
            }

#if SYNC_ONLY
            if (obj is IAsyncDisposable)
            {
                throw new InvalidOperationException("Cannot dispose an async disposable in sync-only mode.");
            }
#endif
}

In the sync result, the using statements are removed, causing if (obj is IAsyncDisposable) to fail (the namespace isn't being added, because we are not processing the directives).

This PR fixes this. The directives are being processed, causing the namespace to be added.

Diffs (before, after):

//HintName: Test.DisposeTest.DisposeAsync.g.cs
// <auto-generated/>
#nullable enable
namespace Test;
public partial class DisposeTest
{
    public void Dispose(object? obj)
    {

            if (obj is global::System.IDisposable disposable)
            {
                disposable.Dispose();
                return;
            }

-           if (obj is IAsyncDisposable)
+           if (obj is global::System.IAsyncDisposable)
            {
-               throw new InvalidOperationException("Cannot dispose an async disposable in sync-only mode.");
+               throw new global::System.InvalidOperationException("Cannot dispose an async disposable in sync-only mode.");
            }
}
}

@GerardSmit GerardSmit force-pushed the fix/process-directives branch from 64d1860 to 495115f Compare July 22, 2025 10:13
@GerardSmit GerardSmit force-pushed the fix/process-directives branch from 495115f to 14f265d Compare July 22, 2025 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants