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

Consider exposing System.Diagnostics.StackTraceHiddenAttribute publicly #29681

Closed
john-h-k opened this issue May 28, 2019 · 10 comments · Fixed by #42632
Closed

Consider exposing System.Diagnostics.StackTraceHiddenAttribute publicly #29681

john-h-k opened this issue May 28, 2019 · 10 comments · Fixed by #42632
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Diagnostics
Milestone

Comments

@john-h-k
Copy link
Contributor

john-h-k commented May 28, 2019

This attribute hides methods and types marked by it in the error message stacktraces. It is used extensively in the CoreLib to e.g. hide implementation details of async methods that are always the same and just add clutter to the error messages.

It appears to be a very useful attribute, particular as System.ThrowHelper is internal, this would be invaluable for user-written throw helper types to keep the best possible exception semantics while still having the advantages of a throw helper. It could be abused, but not really in a "breaking" way, just using it stupidly could make debugging a little harder, so unless there is something I am really missing I cannot see a strong case against making it public.

Proposed API

namespace System.Diagnostics
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Struct, Inherited = false)]
    public sealed class StackTraceHiddenAttribute : Attribute
    {
        public StackTraceHiddenAttribute() { }
    }
}
@danmoseley
Copy link
Member

@tommcdon

@tommcdon
Copy link
Member

cc @noahfalk @sywhang

@MuiBienCarlota
Copy link

This can also be usable with the already public DoesNotReturnAttribute.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the Future milestone Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@tommcdon tommcdon removed the untriaged New issue has not been triaged by the area owner label Apr 6, 2020
@MihaZupan MihaZupan added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 23, 2020
Sergio0694 added a commit to Sergio0694/WindowsCommunityToolkit that referenced this issue Jul 21, 2020
This reverts commit f70ae0c.
Unfortunately the attribute is actually matched by type and not just by name like the others. We should reintroduce this once either dotnet/runtime#29681 is done, or when the runtime switches to a name matching for the internal attribute, so we can just use a local copy here.
@jkotas jkotas added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jul 22, 2020
@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 24, 2020
@terrajobst
Copy link
Member

terrajobst commented Jul 24, 2020

Video

  • Since we can apply it to classes and structs, it seem one should be able to apply it to properties and events as well. Should we add Property, Event, Delegate?
  • We considered Interface (DIM), but that seems ill-defined for non-DIMs. If people wanted them on DIMs, they can apply it to the method
namespace System.Diagnostics
{
    [AttributeUsage(AttributeTargets.Class |
                    AttributeTargets.Method |
                    AttributeTargets.Constructor |
                    AttributeTargets.Struct, Inherited = false)]
    public sealed class StackTraceHiddenAttribute : Attribute
    {
        public StackTraceHiddenAttribute();
    }
}

@PathogenDavid
Copy link
Contributor

PathogenDavid commented Jul 24, 2020

  • Should we add Property, Event, Delegate?

Adding those would require updating consumers of this attribute to recognize it in those places. For example:

if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the method.
return false;
}
Type? declaringType = mb.DeclaringType;
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
if (declaringType != null &&
declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the containing Type of the method.
return false;
}

I'm not sure if it's worth it to add the extra checks here for those scenarios. I can't really imagine why you'd need to hide a get/set/add/remove method from stack traces, and if you need to you could just apply them to the individual methods.

As for delegates, what would that mean? That invocations of the delegate are hidden? Found time to watch the video, I believe the answer is yes.

@john-h-k
Copy link
Contributor Author

I don't think there is really any value in adding to delegate, particularly given it would possibly require additional support from tooling, and for prop/event you can just add to the individual accessor

I would be happy to do the PR for this (don't think it will be a very tricky one 😆 )

@benaadams
Copy link
Member

@stephentoub
Copy link
Member

Does the one in ASP.NET do anything? The stack trace code is searching for the attribute based on type rather than name, using the type from corelib, right?

@benaadams
Copy link
Member

Does the one in ASP.NET do anything?

Just allows methods to be annotated with it; which is then used in ASP.NET's stack trace cleaning https://github.com/dotnet/aspnetcore/blob/e657de4b778bcefbaeeea89883f0975cbcae3107/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs#L254-L257

Which could also be changed to a type check rather than a string comparison (is string comparison to pick up both the runtime's internal type and aspnet's type)

@stephentoub
Copy link
Member

I see, thanks, so same name but used for a different (related) purpose.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.