Skip to content

Instantly share code, notes, and snippets.

@erikkinding
Last active January 9, 2024 19:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erikkinding/68e470f808a82c4daf7950669e5bd178 to your computer and use it in GitHub Desktop.
Save erikkinding/68e470f808a82c4daf7950669e5bd178 to your computer and use it in GitHub Desktop.
Debug F# in Visual Studio Code on OSX (and Linux?) targeting dotnet core

I thought I'd share some simple steps you can follow if you wan't to build, run and debug an F# program on OSX using dotnet core 2.0. I guess these steps would also work if you're running Linux, with some minor modifications.

  1. Install dotnet sdk for OSX: https://www.microsoft.com/net/learn/get-started/macos

  2. Install Visual Studio Code for OSX: https://code.visualstudio.com/

  3. Install C# (yes, C#) extension for OSX: https://code.visualstudio.com/docs/languages/csharp

  4. Create a new console application project using dotnet cli: dotnet new console -lang F# -n HelloWorld

  5. Build your project cd HelloWorld, dotnet restore, dotnet build Did it build? Hope so...

  6. Run your project: dotnet run Should output "Hello World from F#!"

  7. Open your project in Visual Studio Code:

    1. Start Visual Studio Code
    2. Select your project directory: File > Open... >
  8. Setup build task:

    1. From the top menu, select Tasks > Run build task...
    2. Click No build task to run found. Configure Tasks...
    3. Click Create tasks.json file from template
    4. Select .NET Core from the options displayed
    5. If you want, you can add a task for nuget packet restore. Example in tasks.json below
  9. Setup lunch.json for debugging:

    1. From the top menu, select Debug > Start Debugging
    2. Select .NET Core from the options displayed
    3. A new file launch.json will be created    1. Point the program setting to your debug output directory. Example in launch.json below
    4. Save and close the file
  10. Debug your console application:

    1. Open Program.fs
    2. Place a new breakpoint in your code. If you didn't touch the file created by the template upon installation, line 7 should be a good example. Place your cursor on line 7 and from menu select Debug > Toggle Breakpoint A red dot should now appear in the margin to the left
    3. From the top menu, select Debug > Start Debugging or press F5
    4. If everything worked out your debugger should now be stopped at your breakpoint. Hit F5 to continue.
    5. You should now see your "Hello World from F#!" output in the Debug Console

To get the most out of F# in Visual Studio Code, install extension Ionide-fsharp. This should give you things like IntelliSense and highlighted errors and stuff like that.

I hope this helped! Take care :)

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/HelloWorld.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/HelloWorld.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "restore",
"command": "dotnet restore",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
},
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
@qbpd
Copy link

qbpd commented Mar 2, 2018

Thank you so very much for sharing this!

@codingonHP
Copy link

Thank you

@warmbreeze
Copy link

Good!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment