Hello World in Visual Studio Code on Linux

This post adds a sample as addition to my post about VS Code previous post

Setup

  1. Install latest mono as described at http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives.

  2. Install Visual Studio Code from https://code.visualstudio.com Just unpack and start.

Simple Scenario (no debug and no IntelliSense)

Code

./program.cs

public static class Programm
{
    public static void Main()
    {
        Console.WriteLine("Hello Mono and VS Code!!!");
    }    
}

Project configuration

For simple applications without debug support you can skip creation of project.json or any other file like this.

Build configuration

./.vscode/tasks.json

In following task configuration we use msc (mono C# compiler) as build tool with our single code file as an argument and msCompile problem matcher.

{
    "version": "0.1.0",
    "command": "mcs",
    "isShellCommand": true,
    "showOutput": "silent",
    "args": ["program.cs"],
    "problemMatcher": "$msCompile"
}

Launch configuration

Just press F5 and VS Code will auto-generate launch.json that we need.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "mono",
            "request": "launch",
            "program": "${workspaceRoot}/program.exe",
            "args": [],
            "cwd": "${workspaceRoot}",
            "runtimeExecutable": null,
            "env": {},
            "externalConsole": false
        },
        {
            "name": "Attach",
            "type": "mono",
            "request": "attach",
            "address": "localhost",
            "port": 5858
        }
    ]
}

And that’s it

  • Ctrl+Shift+B to build
  • F5 to run and see output in debug console

VS Code Debug Console

Complete scenario

If you need to add debugger and IntelliSense support to simple project described above just add project.json

Additional setup

To use project.json we need to install DNX as fas as project.json is part of dnx build system. Run following commands to install DNX for mono:

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
dnvm upgrade -r mono

./project.json

Below is a simple project.json file that includes all *.cs file in all subdirectories and use dnx451 as framework. Taking into account that we have configured DNX to use mono dnx451 means mono in our case.

{
    "configurations": {
        "Debug": {
            "compilationOptions": {
                "define": ["DEBUG", "TRACE"]
            }
        },
        "Release": {
            "compilationOptions": {
                "define": ["RELEASE", "TRACE"],
                "optimize": true
            }
        }
    },
    "frameworks": {
        "dnx451": {
            "frameworkAssemblies": {
                "System": ""
            }
        }
    },
    "dependencies": {
    },
    "compile": "*/**/*.cs" 
}

After that you can navigate in code use IntelliSense but you still not able to debug your program because mcs does not produce *.mdb by default. To fix this problem just –debug to mcs arguments in tasks.json

./taks.json

{
    "version": "0.1.0",
    "command": "mcs",
    "isShellCommand": true,
    "showOutput": "silent",
    "args": ["program.cs","--debug"],
    "problemMatcher": "$msCompile"
}

Now you can work with all functionalities of VS Code. Just press F5 and start debugging!

VS Code Debugger

Complex projects

For complex projects just use your favorite build tool in tasks.json (see [prev post] for more details about tasks.json)

Some useful links

Mono Project

Visual Studio Code

DNX

Project File Description

global.json

Schema for tasks.json

task.json description

Debugging in Visual Studio Code

Version Control in Visual Studio Code

3 Comments

  1. Posted March 13, 2016 at 02:47 | Permalink | Reply

    Simply desire to say your article is as surprising. The
    clarity for your submit is just spectacular and
    i can assume you are knowledgeable on this subject.

    Well along with your permission let me to clutch your RSS
    feed to keep up to date with drawing close post.
    Thank you a million and please continue the rewarding work.

    Like

  2. Andy
    Posted August 1, 2017 at 15:37 | Permalink | Reply

    So much has changed since you wrote this article. I would like to know how to get Intellisense working in VSCode while using Mono. QNX is dead. You go to their site, they want you to “just use .NET Core” — I don’t want to use .NET Core. I want to use Mono.

    So, are we Mono users SOL with Intellisense?

    Like

    • Andy
      Posted August 1, 2017 at 15:38 | Permalink | Reply

      I meant to say DNX, not QNX

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: