Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

IDE0065 breaks compilation when moving using statements wrapped in #if/#endif blocks from inside to outside a namespace. The #if directive (leading trivia on the using) moves correctly, but the #endif (leading trivia on the first member) stays behind, orphaning it inside the namespace.

Example:

// Before
namespace MyNamespace {
#if !FOO
    using System.Runtime.CompilerServices;
#endif
    class Program { }
}

// Current (broken)
#if !FOO
using System.Runtime.CompilerServices;
namespace MyNamespace {
#endif  // ← Orphaned, won't compile
    class Program { }
}

// Fixed
#if !FOO
using System.Runtime.CompilerServices;
#endif
namespace MyNamespace {
    class Program { }
}

Changes

Core Fix

  • Added MovePreprocessorDirectivesWithUsings to detect #if directives on usings and relocate the corresponding #endif from the first member's leading trivia to the last using's trailing trivia
  • Modified RemoveUsingsFromNamespace to invoke this method before removing usings from the container

Test Coverage

  • Single using in #if/#endif block
  • Multiple usings in same #if/#endif block
  • Namespace-level #if/#endif wrapping (already handled correctly, test added for regression)

Fixes #31249

Original prompt

This section details on the original issue you should resolve

<issue_title>IDE0065 messes up if/endifs around namespaces</issue_title>
<issue_description>Version Used:
3.5.0-beta2-19570-07+4bd316035f64c9776d5420af71df8ba38e8e23e4

Steps to Reproduce:
Create a project with this code:

using System;

namespace MyNamespace
{
#if !FOO
    using System.Runtime.CompilerServices;
#endif

    class Program
    {
        static void Main() { }
    }
}

Then apply IDE0065 when it fires on the using System.Runtime.CompilerServices; directive.

Expected Behavior:
Refactors to:

using System;
#if !FOO
using System.Runtime.CompilerServices;
#endif

namespace MyNamespace
{
    class Program
    {
        static void Main() { }
    }
}

or doesn't offer the refactoring.

Actual Behavior:
Refactors to:

using System;
#if !FOO
using System.Runtime.CompilerServices;

namespace MyNamespace
{
#endif

    class Program
    {
        static void Main() { }
    }
}

which is broken and won't compile if that compilation constant is set.</issue_description>

<agent_instructions>@copilot see if this is simple. If 'move misplaced' using is moving a single using (or a set of usings) that are entirely surrounded with direct trivia, see about moving the end directive as well.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@stephentoub I just went to file this issue after getting hit by it, and discovered I already did over two years ago :) @Evangelink There is another related scenario.

Steps to reproduce:

#if NET6_0

namespace ConsoleApp
{
    using System;

    internal class Class1
    {
        public void M()
        {
            Console.WriteLine("");
        }
    }
}

#endif

Actual Behavior:

Applying the fix gives

using System;

#if NET6_0

namespace ConsoleApp
{
    internal class Class1
    {
        public void M()
        {
            Console.WriteLine("");
        }
    }
}

#endif

It's less severe because the code still compiles but we end up with warnings about unused using when compiling in different target framework (which can cause build break because of TreatWarningsAsErrors).

Expected Behavior:

Applying the fix gives

#if NET6_0

using System;

namespace ConsoleApp
{
    internal class Class1
    {
        public void M()
        {
            Console.WriteLine("");
        }
    }
}

#endif
```</body></comment_new>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix IDE0065 refactor issue around namespaces Fix IDE0065 corrupting code when moving usings with preprocessor directives Nov 14, 2025
Copilot AI requested a review from CyrusNajmabadi November 14, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IDE0065 messes up if/endifs around namespaces ExtractMethodCodeRefactoringProvider encountered an error and has been disabled.

2 participants