❔ Roslyn analyzer test project can't compile `where T : unmanaged` type constraint
I've made a roslyn analyzer for some marshaling stuff. In my test project I try to analyze this source code:
but when I try to compile this code I get:
error CS0518: Predefined type 'System.Runtime.CompilerServices.IsUnmanagedAttribute' is not defined or importedWhy the error? I know there are similar issues with
IsExternalInit
, but my test project runs on .net 6.0 and the test code above is compiled with the latest C# version. AFAIK, the unmanaged
constrain came with C# 7.3 / .net core 2.0.16 Replies
I've worked around it by injecting my own flag type into the compilation:
Hold on, where exactly are you getting the error? In the analyzer project or in the test project?
the test project
or rather, during execution of the tests
each test is building some source code that is then analyzed by the analyzer
and when building that source code during test execution, that build fails
Can you post the code for the tests?
Sounds like you might not be adding the proper assembly references?
That's what I'm suspecting as well, I had trouble with those in the past
This file is used to generate the compilation
Should be largely copied from a sample project
Yeah, don't do that
🙂
Use the testing framework
For example, https://github.com/jmarolf/analyzer-start/blob/main/tests/TypesShouldBeUpperCaseTests.cs
since when does the testing framework exist?
Quite a while
Hmm, I've started using the analyzers basically when they were first introduced, and copy & pasted the testing for new analyzers from then
probably shouldn't have done that
It's been around for 3 years at this point
And in various incarnations before that
🤷 maybe I've already missed it back then. Anyways thanks for the help
This is sooo much better. Now I can delete this cursed helper.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.