Search Unity

Feature Request Expose .sln and .csproj generation

Discussion in 'Code Editors & IDEs' started by liortal, Aug 26, 2022.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Unity automatically generates .sln and .csproj files for the code files inside the project.
    This is OK if you're just using the editor, but limits the ability to interoperate and use external tools to process your code.

    On top of that, since Unity auto-generates and updates those files, it is generally not recommended to check in those files into source control.

    Consider an example - SonarQube is a static analysis tool. It has support for scanning .NET code, and has nothing to do with Unity. In order to run a scan, one must first build and process a .sln file.

    To achieve this, currently, I must launch Unity and call some editor method that refreshes the .sln files, then close the editor and continue with the process.

    While this works, it forces me to have Unity installed on every machine that could run this scan.
    There are other countless examples for such .NET tools that rely on a .sln or .csproj file.

    It would be great if the process of building the .sln and .csproj files was somehow exposed (as some 3rd party tool, DLL or any other option) that is not directly dependent on Unity (the editor) installed on the target machine.

    Is this something that was ever considered?
    Is it in your plans? How difficult would it be to achieve this ?
     
    van800 likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,876
    I wanted to show you this button ... but then I realized, that's another nice feature you only get when using Rider. :D
    upload_2022-8-26_10-37-46.png

    But it may actually suffice to have the Rider Editor package installed, switch to Rider, click the button, then change back to Visual Studio. Wait, what, why am I saying this? I mean, really, the next step is to go to the JetBrains website and purchase Rider for you and your team. :p

    Anyway, you could look into batch mode, it could do the trick if you want to automate this process with a simple batch file: https://docs.unity3d.com/Manual/EditorCommandLineArguments.html
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,876
    I just looked up in source code for "Regenerate project files".
    Looks like the Rider package has a class for this which you could leverage and adapt to your needs.

    Code (CSharp):
    1. namespace Packages.Rider.Editor.ProjectGeneration
    2. {
    3.            internal class ProjectGeneration : IGenerator
    Essentially this is a csproj/sln generator from template snippets.

    But you'd only need to extract that class from the Rider package and call its "Sync" method to generate the project files.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    My suggestion was to extract the project generation logic from Unity into a standalone tool.

    I don't want to use batchmode as it means i gotta have Unity installed, as mentioned in my original post.

    As far as i know, Rider's generation is built upon the built-in project generation provided by Unity, so it cannot be used on its own
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,876
    Right, I missed that. Would be difficult though to generate projects with a tool that's not running within Unity itself. You'd have to manually scan all the files, asmdefs and all that.

    And what for? The benefit of not having to install Unity on a system is just not enough IMO. Even for automated builds you need to have Unity installed anyway. And that's usually also the machine you'd be running such automated tools as well, right?
     
  6. gonzalo_unity764

    gonzalo_unity764

    Joined:
    Mar 14, 2022
    Posts:
    1
    this would be great! we also added Sonarqube to our engineering process and I was really sad when I found out it didn't work without the csproj. And of course we don't want to track that in git
     
    liortal likes this.
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    you basically have to launch Unity to get it syncing the project and solution files, then quit it and run whatever you need with those files now in place.

    It's a hassle and i would rather do it externally and not from inside Unity.
     
  8. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Hi guys! So yes, current workflow does not work well together with well known tooling.
    And even if, in the current state, it would get exposed, it would not help as we today do not use MsBuild to compile with, meaning its not compiling the sln and csproj's, so adding additinal csproj's will not do anything of meaning.
    Csproj's today are only for the IDE experience.

    I do have some happy news though, we are currently working on a MsBuild workflow, this will give;
    - Ability to maintain your own csproj's, change settings through the IDE that will be reflection in the compilation.
    - Ability to extend your builds using standardized MsBuild workflow with Tasks, additional .props/.targets changes. MsBuild has many most likely know have great documentation and are highly extendable to add additional steps.
    - Be able to get NuGet packages.
    - Compile trough the IDE. Reuse IDE compilations/restores from the IDE and Unity, so no more double compilations.
    - Update csproj's from Unity trough an Inspector. Though you can do all the same from your favorite IDE.

    We will still support Asmdef's, but they will be generating Csproj's that will be what we will compile.
    This is mainly for the Package Developers sake, so they can ship Asmdef's, and that will be supported across versions.

    The csproj's generated from Asmdef's will be read-only. so for those if you want to change References it still need to be trough Unity Asmdef Inspector, or modifying, but you will get options to convert all Asmdef's to csproj's that you have.

    Its something that will take some time, so I can't promise when this will be out for you folks. But im super excited about this, its the right thing to do, and im sure we can make it a great experience.
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I have a working workflow where i launch Unity, sync the .sln and .csproj files on our build machine, then quit Unity and run an MSBuild based flow to build the .sln and then use that to scan our codebase using SonarQube.

    I understand what you're saying about not being identical to the compilation that Unity uses in the editor and when packaging the player, but for these purposes it works for us in this case.

    Judging by your answer that the full transition to this workflow will take time - do you think that building our own logic for creating .sln + .csproj files might be far fetched? how complicated can it get ?

    I assume we can map the files inside Assets and decide whether they belong to any special folders (Editor and the likes), check .asmefs and then create files per assembly with references to the matching script files.
     
  10. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    So im guessing you want to generate csproj's and sln without Unity to save time to execute Sonar, instead of doing imports and all that.
    The rules of the globbing is not hard to do.
    Defines, you could just save in some files for the sake for Sonar.
    Unity Package Manager do not have any CLI sadly, so its not possible today. Its one of the things we want to do with the MsBuild flow, so we can restore Unity Packages. So you would need to commit all your packages if you dont want to somehow kick that off.
     
  11. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Im thinking our loud here, but maybe we could test out some of the building blocks we are doing with MsBuild.
    As we would need to do alot of the things you want, generating csproj's, sln, globbing, all that.
    It would give us some more confidence it the work.
    I havent discussed this with my team yet, but I think it would be valuable for you and us. Would that be interesting for you?
     
    goncalo-vasconcelos and bdovaz like this.
  12. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    YEP! sign me up
     
  13. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Did you get a chance to check this out?
     
  14. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    @liortal nothing yet. I dont expect it soon'ish. We still need to find out how to fix Packages.
    But as soon as I have something ill write here :)
     
  15. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
  16. KristoferBoman

    KristoferBoman

    Joined:
    Feb 16, 2016
    Posts:
    61
    Also interested in this. Any updates?
     
  17. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
  18. HaraldNielsen

    HaraldNielsen

    Unity Technologies

    Joined:
    Jun 8, 2016
    Posts:
    139
    Hi @liortal! Sorry for the lack of updates.

    Sadly we can't get past the constraint that we need packages resolved first. So that means that Unity needs to run at least once before to populate the Library folder.
    If that is not done, anything that depends on packages will fail to compile, im not sure what would happen with static analytics for such a case?

    Would a library, to help with the rules of Editor/Predefined Assemblies and project references, still help if Unity needs to run first to resolve packages?
     
    Last edited: Jan 29, 2024