import org.scalatest.wordspec._
class SampleSpec extends AnyWordSpec {
"The sample code" should {
"say hello" in {
assert(Sample.greeting.startsWith("h"))
After the sample project is created, the layout looks like the picture below from VS Code.
Note that .bloop, .metlas, and .vscode are generated by VS Code and Metals extension. If we use a version control system like Git, we should add these folders to .gitignore.
(The sample code is also available at https://github.com/shunsvineyard/scala-sample-code.)
VS Code Usage
The following subsections demonstrate some everyday use cases for Scala development using VS Code.
Metals supports Scalafmt for code formatting. To format code, right-click on the Scala file we want to format and select Format Document to format the file as shown in the picture.
By default, Metals formats Scala documents based on the .scalafmt.conf file on the project directory’s root. The path can be modified from the Metals extension configuration. See Configurations to alter the settings.
Run and Debug
There are two ways to run and debug the Scala code.
1. Use Code Lenses
For each main or test class, Metals shows code lenses run | debug for main class and test | test debug for test classes. Click run or test to run or test the Scala program.
2. Use launch.json Configuration
The other way is to define launch.json for VS Code to run or debug a program.
Follow the steps in the picture to create the launch.json configuration file.
After step 3, VS Code asks a few more questions. We can leave them all blank and edit the launch.json file after the JSON file is created. After the configure file is created, we can fill in the configurations and run or debug the program.
Configurations
In addition to the most common use cases, code format, run and debug, there are many other things we can configure Metals to work in the ways we want.
To open the settings, do [File -> Preference -> Settings] to open the settings. The settings have three scopes: User, Remote, and Workspace. Choose whichever fits our needs. And select Extensions -> Metals to modify the settings.
Troubleshooting
Sometimes, the Metals extension may not work correctly. We can check the Metals output to get more information to solve it or, at least, have better error messages to Google.
For example, if the JDK version we installed does not support JDI (this will result in the debugger not working), we will get the following error message when we try to run the debugger.
Couldn't find a debug adapter descriptor for debug type 'scala'
(extension might have failed to activate)
Unfortunately, the error message does not give enough information about the issue. In this case, we can check the Metals output. See the picture below:
It gives us more information about this issue.
Message: Debugging is not supported because bloop server is running on a
JRE /usr/lib/jvm/java-8-openjdk-amd64/jre with no support for Java Debug Interface:
'JDI implementation is not provided by the vendor'.
To enable debugging, install a JDK and restart the bloop server.
So, we know the JDK is the issue and needs to be fixed.
Terminal for sbt Commands
One useful VS Code feature worth mentioning is the integrated terminal. With the integrated terminal, we can run the shell on WSL. For Scala development, the terminal provides a convenient way to run sbt commands within the VS Code.
Using Ctrl+Shift+P to bring up the Command Palette and type terminal. It should show a few terminal options. Choose to Create New Integrated Terminal (In Active Workspace).
This brings up the WSL shell on the project directory.
With this feature, we can use the terminal without external tools such as Windows Terminal or MobaXterm.
Conclusion
This article provides the basic setup and common use cases for using VS Code to write Scala code. Although it demonstrates the configurations on Windows Subsystem for Linux, the VS Code Usage also works on Windows, Linux, and MacOS.
VS Code may not be the most popular editor for Scala programming, but with the great extension support such as Metals, VS Code can be a useful tool for Scala development. Especially for people who work in multiple languages, VS Code could be the one for all your needs.
The post Setting Up VS Code for Scala Development on WSL appeared first on Shun's Vineyard.
License
My name is Shun. I am a software engineer and a Christian. I currently work at a startup company.
My Website: https://formosa1544.com
Email: shun@formosa1544.com