Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is it possible to use AdditionalFiles with a Source Generator in Unity 2022.1?

Discussion in 'Editor & General Support' started by thepunkoff, May 13, 2022.

  1. thepunkoff

    thepunkoff

    Joined:
    Apr 29, 2019
    Posts:
    4
    I was able to configure a source generator, that just generates .cs files on it's own, but I need to generate them based on a JSON file.

    There is a tag called 'AdditionalFiles', that can be used in csproj to make a JSON-file visible to a source generator. But Unity generates it's csproj... I tried to add AdditionalFiles there on my own, but it seems not to work. Although my Rider IDE generates them without a problem, the compiler in Unity says that it can't find the classes and namespaces I'm generating. So I'm assuming it doesn't find the JSON, because it needs one.

    Unity docs on source generators are terribly shallow, so I'm asking for help here! Also there's a mistake in docs about what .net standard version your SG should target - with Unity 2022.1 it should be 2.1, not 2.0. there's also a question about System.Text.Json - the docs say the Generator should be using version 6.0.0-preview - but there are many of them. Is any of them fine?

    Please, help, it's hard to Google this thing :) If any Devs are here, please tell if AdditionalFiles is supported, or I should generate cs files in another way, at least for now.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Anything you do compile-wise in Visual Studios is irrelevant.

    If you run some tool in VS to generate code, that might work, assuming the output is actually written to disk in a location that Unity can see it. If generated code goes somewhere transient, Unity certainly won't see it.

    As far as the AdditionalFiles tag, who is that contract between? Is that a Visual Studio contract with the code generator? It doesn't sound like anything Unity would even have visibility into.

    You can specify by filetype to Unity if you want extra types to be included in the csproj that Unity verifies... that is under Edit -> ProjectSettings -> Editor. Is that how you're doing it? It should already include json files, IIRC.
     
  3. thepunkoff

    thepunkoff

    Joined:
    Apr 29, 2019
    Posts:
    4
    Source Generators tutorials say one should specify files to be visible for the generator via AdditionalFiles tag. It works in VS and Rider. I don't actually know right now who is responsible for parsing the .csproj and making this magic. I'd guess, it just turns into some flags to a CS compiler, that supports SG. But if so, am I able to pass my own flags to the compiler used by Unity?

    Thanks for your answer, btw!
     
  4. thepunkoff

    thepunkoff

    Joined:
    Apr 29, 2019
    Posts:
    4
    Well, I did my investigation. It's indeed possible to use AdditionalFiles. You should pass a flag to a compiler used by unity - "/additionalfiles: file list". File path is relative to the project root. You can do it in the Edit > Project Settings > Player, and look for "additional compiler arguments". So I passed "/additionalfiles: Assets/myfile.json", and was able to confirm, that it's not throwing any errors (e.g. non existing files make it throw FileNotFoundException during compilation), and I was able to generate a simple file with a static method that prints the json path, taken from `context.AnalyzerOptions.AdditionalFiles`, and call it from the Unity script.

    However it stops working if I try to parse my JSON with System.Text.Json in the generator source. It just doesn't compile. I tried all 6.0.0-preview versions, that the docs recommend, but none seems to be working. Well, this is a question for another topic.