codemaid
codemaid copied to clipboard
`#endif` between method and its Attribute is getting deleted.
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
- Create a class library project.
- Write a method.
- Add
[Obsolete]on top of the method.[DebuggerStepThrough]also gets affected. - Surround the attribute with
#ifand#endif. - 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.
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.
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.
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.
Hi @codecadwallader , thanks for your working, any update on this?
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
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.
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
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.
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?
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.
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!
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)
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