codemaid icon indicating copy to clipboard operation
codemaid copied to clipboard

`#endif` between method and its Attribute is getting deleted.

Open MineCake147E opened this issue 3 years ago • 13 comments

Environment

  • Visual Studio version: Community Version 17.1.0 Preview 1.1
  • CodeMaid version: 12.0.300
  • Code language: C#

Description

I wrote this code:

namespace CodeMaidBugRepro
{
    public class Class1
    {
#if DEBUG
        [Obsolete]
#endif
        public uint A(uint b) => b;
    }
}

After running the code cleanup with CodeMaid, the #endif gets removed, making build impossible.

Steps to recreate

  1. Create a class library project.
  2. Write a method.
  3. Add [Obsolete] on top of the method. [DebuggerStepThrough] also gets affected.
  4. Surround the attribute with #if and #endif.
  5. Click on "Clean Up Active Documents" or turn on "Automatic Cleanup On Save" and save the document.

Current behavior

The #endif gets removed.

Expected behavior

The #endif should remain alive.

MineCake147E avatar Nov 22 '21 10:11 MineCake147E

Thanks for reporting the issue. I have been able to reproduce it. This looks to be fitting into a series of issues related to a breaking change in an underlying vsCMAccess API. When we try to set access modifiers using the same API from previous versions it is now causing breaking changes to the code.

As a workaround, if you disable CodeMaid->Options->Cleaning->Insert->Insert explicit access modifier fields on methods that should(tm) avoid the issue for now.

codecadwallader avatar Nov 30 '21 13:11 codecadwallader

I've confirmed this issue happens in both VS2019 and VS2022. I've sent an email to Microsoft to try and get some help digging down into the API.

codecadwallader avatar Dec 09 '21 13:12 codecadwallader

While looking in that area, there is also room for improvement in that CodeElementHelper doesn't recognize some syntax patterns (e.g. method arrow definitions) as well as GetTextToFirstMatch which was recently updated for VS2022 is not respecting the startPoint. These issues are separate from the underlying API issue, but in the example above they would prevent the API from being called if the access modifier was already present.

codecadwallader avatar Dec 09 '21 13:12 codecadwallader

Hi @codecadwallader , thanks for your working, any update on this?

WeihanLi avatar Jan 05 '22 04:01 WeihanLi

Hi @WeihanLi , the latest update is I'm waiting for feedback from Microsoft on this issue if you would like to look/comment over there: https://github.com/dotnet/roslyn/issues/58315

codecadwallader avatar Jan 05 '22 14:01 codecadwallader

I am also getting this issue, but it's removing the entire #if .... #endif block. For example:

[Attribute1]
#if CONDITION // This line to the endif are being removed on auto-format.
[Attribute2]
#endif
public string MyProperty { get; set; }

Using the Visual Studio "Format Document" feature does not remove the block, only when auto-formatting using CodeMaid.

flamewave avatar Jan 13 '22 15:01 flamewave

Hi there

This problem also occurs in the following situation:

#region Attributes [Attribute1] [Attribute2] #endregion public class Class1 { }

The #endregion element will be removed when doing a cleanup

jacobdeboer33 avatar Jan 27 '22 15:01 jacobdeboer33

Thank you for providing the additional situation. It looks like it may be awhile before the SDK is resolved so I am considering disabling the explicit access modifier logic for everyone in the meantime to avoid these unexpected code corruptions.

codecadwallader avatar Feb 06 '22 20:02 codecadwallader

When I have this C# code:

#if USE_NEW_REQUESTS
    [RequestTypeHandler]
#endif

after clean up it looks like this:

#if USE_NEW_REQUESTS
    [RequestTypeHandler]

The workaround As a workaround, if you disable CodeMaid->Options->Cleaning->Insert->Insert explicit access modifier fields on methods that should(tm) avoid the issue for now. does not work for me. Is there any other workaround I could use?

FerdinandStapenhorst avatar Jan 13 '23 07:01 FerdinandStapenhorst

Is the [RequestTypeHandler] decorating a method or another type of element? If for example that is on a property you would want to also disable inserting explicit access modifiers on properties.

codecadwallader avatar Jan 14 '23 18:01 codecadwallader

It decorates a class:

#if USE_NEW_REQUESTS
    [RequestTypeHandler]
#endif
    public class DmViews
    {
        #region Statics
        private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger("_DmHttpServer.DmRequests.Views");
        ...

I disabled it for classes and now it works. Thanks!

FerdinandStapenhorst avatar Jan 15 '23 15:01 FerdinandStapenhorst

It seems the bug deletes comments between attributes and definition as well. e.g. In this example, the comment gets deleted on cleanup:

[TestCase("valX")]
[TestCase("valY")]
// need testcase for valZ
public async task SomeTest(string val)

AppFzx avatar Feb 27 '23 12:02 AppFzx

Hello, we have the same bug/problem with codemaid in our productive environment.

codemaid cleanups document despite it contains pre processor#'s (and therefore disabled) and removes pre processor's in some places like described by others.

  • dont-cleanup-if-pre-prcoessor-present does not work reliable
  • pre-processors get deleted following method attributes

perahoky avatar Apr 05 '24 10:04 perahoky