Search code examples
c#.netmacossublimetext3sublimetext

How to Run C# on Sublime Text 3 on macOS?


I'm trying to run C# on macOS, using Sublime Text 3 on Automatic mode under:

> Tools > Build System > Automatic

and have built a system:

{
    "cmd": ["csc $file && $file_base_name"],
    "shell": true,
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "source.cs",
    "quiet": true
}

in Sublime Text 3/Packages/Users/CSharp.sublime-build based on this article.

I've tested with CMD+B:

using System;

class re{
  public static void Main(){
    Console.WriteLine("Hello world");
  }
}

which returns:

/bin/sh: csc: command not found

and I'm not sure how to fix that, looked at some related posts such as this, yet can't figure it out. How do I fix that?


Other Info

$ which dotnet
/usr/local/share/dotnet/dotnet

$ dotnet --version
2.2.401

$ ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/
2.2.6

Solution

  • If you're using dotnet core (as it appears from your question), you need to use dotnet to execute code and not csc; that's the compiler for .NET (the nomenclature and weird namings of these things gives me a bit of a headache).

    As a gross oversimplification, dotnet core uses a command named dotnet to do compilations and runs of your project, while .NET uses things like msbuild to run builds and uses csc as the underlying compiler. To use those commands you would need to install something like Mono instead (or an official MacOS version of .NET 4 or what have you, assuming such a thing exists).

    The build system that I personally use for this sort of thing is:

    {
        "working_dir": "${folder:${project_path:${file_path}}}",
        "selector": "source.cs",
    
        "variants":
        [
            {
                "name": "Build",
                "shell_cmd": "dotnet build",
                "word_wrap": false,
                "quiet": true,
            },
            {
                "name": "Clean",
                "shell_cmd": "dotnet clean"
            },
            {
                "name": "Run",
                "shell_cmd": "dotnet run"
            },
            {
                "name": "Run Interactive",
                "cmd": ["dotnet", "run"],
                "target": "terminus_open",
                "auto_close": false,
            }
        ],
    }
    

    This sets up a build system with several variants, allowing you to choose to build clean or run the program. If you don't choose a variant when you build, the command will fail because the top level build doesn't contain any command to build anything.

    In this case that's because of the last variant that uses the Terminus package to allow for running interactive programs. Terminus only supports cmd and I prefer to use shell_cmd, so the top level build needs to have no implicit command.

    An alternative to this would be to either ditch the Terminus variant or convert all of the others to use cmd instead. In that case you could move the command for the most common item (say the Run variant) to the top level instead, so that running the program is the default.

    As a last note, for the build sysytem to work the presumption is that you're working in a folder where you've use the appropriate dotnet command to create an empty project. I'm not aware offhand if there's a way to execute single ad-hoc C# files using dotnet outside of that context.