Category Archives: Visual Code

Visual Code with vscontainers

Note: It appears in Visual Studio Code 1.73.1 that the process for how to open a container has changed. I’ve added the changes to the post

Visual Code supports vscontainers. Basically a way of running Visual Code, connecting to a docker image and working within that container.

So for example we could run up a Go container with all the Go tooling within it, use Visual Code’s terminal to interact directly with the container and it also support volumes, so allows us to store data directly on our host machine.

If you’re using Windows you’ll need the Docker Desktop and WSL2 installed.

All we need to do is…

Let’s say we wanted to work with a dotnet container. Create a folder to act as your code folder and then create a .vscontainer .devcontainer folder and within that a vscontainer.json devcontainer.json file. Add the following

{
  "image": "mcr.microsoft.com/vscode/devcontainers/dotnet",
  "forwardPorts": [3000]
}

Now use the View | Command Palette (or Ctrl+Shift+P) and run the command >Remote-Containers: Reopen in Container Dev Containers: Reopen in Container. This will restart VS Code and when it’s finished downloading the container image will be ready for you to start interacting with – if you haven’t already got the terminal window open then try opening one. In my case I have a bash terminal. You should now be within the container so just try and run the dotnet CLI command and you should see it’s help/commands.

Obviously if your system already has dotnet installed that probably won’t surprise you or interest you that much, but there’s a bunch of alternative containers, for example, how about a TypeScript Node container

{
  "image": "mcr.microsoft.com/vscode/devcontainers/typescript-node:0-12",
  "extensions": ["dbaeumer.vscode-eslint"],
  "forwardPorts": [3000]
}

To see a list of Microsoft defined containers, take a look at microsoft-vscode-devcontainers.

Here’s another one to try – so I have Swift set up on my Linux server but not on my Windows Box, so using the following will give me a Swift environment

{
  "image": "swift",
  "forwardPorts": [3000]
}

Now from the terminal (as already stated) you’re within the container but you’ll want to store your code on the host machine – well magically works out of the box (so to speak) and I am able to create a folder in the terminal and see it in the host folder I created with .vscontainer within it.

References

Developing inside a Container

Editorconfig

When we’re collaborating with other developers, whether you’re writing JavaScript, C~ or whatever, it’s useful (some may say essential) to create a coding style that all developers adhere to. After all, one of the aims of collaboration is that you shouldn’t really be able to tell who wrote what code just by the code style.

With web development it’s not unusual to enforce code style etc. with eslint & prettier but this is often shown as a warning when things are working against the style or in some cases, these changes are only applied upon committing the source code. It’s be much better if we could have the editor automatically work the same, for things like indent styles (for example).

This is where the .editorconfig comes in. A file you should commit with your source code and is supported by Visual Code, Visual Studio, JetBrains IDE’s etc.

I’ll be honest, the main reason I use the .editorconfig is to configure my editors to use the same indent_size. Here’s a basic .editorconfig file

root=true

[*]
indent_size = 2
end_of_line = lf

Visual Code has an extension available named EditorConfig which can be used to generate a bare bones .editorconfig for you. Visual Studio has a file template, just add a new file to your project using the Add | New Item… context menu and search for editorconfig. Selecting the editorconfig (.NET) will populate the .editorconfig with lots of options set for various .NET languages.

If you have an .editorconfig file in the root of your project it will be applied to all sub-folders unless overridden by another .editorconfig in one or more of these folders.

If you want to override some, but not all options within the root .editorconfig then just put those additions/changes in the file and ensure

root = true

exists in the root level file.

As you’d expect the .editorconfig is read top to bottom, hence if you have a duplicate key later on in the file this will override any previous value.

References

EditorConfig
Create portable, custom editor settings with EditorConfig

Visual Code debugging in Chrome

I’m using Visual Code for my React development and I’m using Chrome as my browser – I want to debug my React code in Chrome using Visual Code, so let’ see how it’s done.

First off, check out Introducing Chrome Debugging for VS Code, secondly VS Code – Debugger for Chrome.

Or, if you prefer, here’s the steps

  • Ctrl+Shift+X within Visual Code to load the Extensions list
  • Search for Debugger for Chrome and install it (if not already installed)

You’re going to need a launch.json file in the .vscode folder, here’s a minimal example

{
    // 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": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}"
        },
        {
            "type": "chrome",
            "request": "attach",
            "name": "Attach to Chrome",
            "port": 9222,
            "webRoot": "${workspaceFolder}"
        }
   ]
}

In the above we have two configurations, both for Chrome, one launches Chrome with the supplied URL, the second attaches to an existing instance of Chrome.

If you now choose the option Run | Start Debugging, Chrome will launch with your URL loaded. You can place breakpoints in your code (before or after you start debugging) and you’re application will break at these breakpoints as you’d expect and you can inspect variables etc. So basically a full debugging experience.

Default commands within debug mode are as follows

  • F5 Continue
  • F10 Step Over
  • F11 Step Into
  • Shift+F11 Step Out
  • Ctrl+Shift+F5 Restart
  • Shift+F5 Stop

Setting up a Haskel development environment on Windows 10

I wanted to try out Haskell on Windows 10 and figured I’d probably use Visual Code for writing code, here’s the steps to get everything installed and configured.

The simplest way to get everything installed seems to be using chocolatey, see Getting Started.

  1. Install chocolatey
  2. Run Powershell in admin mode and then run choco install haskell-dev
  3. Finally, from Powershell run refreshenv

If all of these steps worked, then just run ghc from the Powershell or a command prompt and you should see some output from ghc.

Let’s now set-up Visual Code (I’ll assume you’ve already got Visual Code installed). There’s a few plugins/extensions for Visual Code for use with Haskell. From Visual Code extensions option…

  • Add Haskell syntax highlighter
  • Also add Haskell-linter
  • To configure the linter, from Visual Code select, File | Preferences | Settings, switch to the code view and add the following
    "haskell.hlint.executablePath": "C:\\ProgramData\\chocolatey\\lib\\ghc\\tools\\ghc-8.10.1\\bin\\ghc.exe"
    

    Chocolatey installs the files into C:\ProgramData\chocolatey\lib, however if you’ve installed ghc by some other means of within another folder then replace the folder with your location.

Finally, let’s write a Haskell “Hello World”. Within Visual Code create the file HelloWorld.hs and add the following code

main = putStrLn "Hello, world!"

Open the Visual Code integrated terminal or open your preferred terminal/shell and from the folder that you saved your HelloWorld.hs file, run

ghc HelloWorld.hs

If all goes to plan you’ll compile the Haskell file to a HelloWorld.exe and you’re done for now.

Debugging Rust in Visual Code

I’m using Visual Code a lot for Rust development, so it’d be good to be able to debug a Rust application within it. Simply change the settings.json file, i.e.

File | Preferences | Settings

from the settings.json and now add

"debug.allowBreakpointsEverywhere": true,

Now add the C/C++ Microsoft extension if you’ve not already added it to Visual Code.

Next up, select

Debug | Add Configuration...

and the option

C++ (Windows)

Here’s my resultant launch.json file

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/target/debug/test.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false
    }
  ]
}

and that’s it, now add a break point to your application, select Debug | Start Debugging (F5) and you’re off and running in the debugger.