79522565

Date: 2025-03-20 10:17:46
Score: 1
Natty:
Report link

So in the hope that this might help others - it turns out the first problem was that I was unit testing my source generator and didn't add the expected preprocessor symbols (If I had read the code I copied from better I would have understood that);

var syntaxTree = CSharpSyntaxTree.ParseText(source);

I added the symbols in the unit test like this;

syntaxTree = syntaxTree.WithRootAndOptions(syntaxTree.GetRoot(), (new CSharpParseOptions(LanguageVersion.Latest)).WithPreprocessorSymbols("UNITY_WEBGL"));

Then I could at least debug the source generator properly and understand what was going on. Some wrangling later and my code to find the preprocessor symbols that works is;

var generateForUnityCheck = context.CompilationProvider.Select((compilation, _) =>
{
    if (compilation is CSharpCompilation cSharpCompilation)
    {
        var preprocessorSymbols = cSharpCompilation.SyntaxTrees
                .Select(st => st.Options)
                .OfType<CSharpParseOptions>()
                .SelectMany(po => po.PreprocessorSymbolNames)
                .Distinct()
                .ToList();
                
        return preprocessorSymbols.Any(s => s is "UNITY_WEBGL" or "UNITY_6");
    }

    return false;
});

Thanks @BugFinder - I didn't find anything quite helpful in that issue or the referenced PRs - but it did lead me down paths that helped to get output to debug what was going on

Thanks @derHugo - you are right about the lack of UNITY define (it was really just an example; mangled a bit to put in SO); for my use case (I'm not building a public library!) I only really needed UNITY_WEBGL but I have extended a bit to at least work in the editor. (still messing with the right thing to do here). As to UniTask and Awaitables - I had heard of UniTaskand have seen the plethora of negativity towards Awaitable in the forums but want to take as few dependencies as possible until I have to; Awaitables are working well enough for me at the moment

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @BugFinder
  • User mentioned (0): @derHugo
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Stephen Starkie