Debugging a script

Glenn Block edited this page Jul 28, 2016 · 14 revisions

Debugging overview

This document explains by example the steps required to use Visual Studio to debug .csx files that are executed with scriptcs. Hopefully you won't need to debug very often, but if you are in need be sure to follow this example.

You may also like to comment on the feature request at https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/7848117-add-scriptcs-csx-intellisense-and-debugging-in-v

Using :openvs with Visual Studio 2013 and 2015.

scriptcs offers an out of the box solution for debugging and executing scripts with Visual Studio, using the :openvs REPL command. Typing :openvs "[filename]" from within the REPL will create a Visual Studio solution for you including all files and subdirectories in the same folder. It will then launch Visual Studio where you can execute the script script and debug. You can add breakpoints, watches etc.

Below is a screenshot showing :openvs in action.

debugging

Using Visual Studio "15" "Open Folder"

The new Visual Studio "Open Folder" allows you to open a folder containing .csx files and quickly edit them (with full syntax highlighting and IntelliSense), you cannot debug a selected script with F5 out of the box. Luckily, there's a new Visual Studio Extension called AnyScriptCs, which adds debugging support for .csx files to Visual Studio "15"!

Here's how to get started:

  1. Install AnyScriptCs VSIX from Visual Studio Gallery
  2. Once installed, open Visual Studio "15", select File > Open > Folder..., and select a folder containing scriptcs (*.csx) files
  3. Right click on any .csx file, and select Set as Startup Item:
  4. Click the dropdown arrow of the Debug button, and select Customize:

image

A file called launch.json will open. We need to specify the path to scriptcs.exe inside the file like this:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "HelloWorld.csx",
      "project": "HelloWorld.csx",
      "scriptcs_exe": "C:\\path\\to\\scriptcs.exe"
    }
  ]
}

Save the file, and press F5!

image

Manually configuring debugging in Visual Studio 2013 & 2015

Note: This section is kept here for historical purposes, but this approach is no longer needed with the :openvs command

Prerequisites

  1. The following example shows how you can debug the WebApiHost sample. This procedure assumes that you have the .csx, scriptcs_packages.config and scriptcs_packages folder already setup.
  2. You must have Visual Studio 2012 or later installed.

Note: Visual Studio "15" which was announced at Build 2016 introduces a new feature called "Open Folder", allowing you to quickly open a folder containing arbitrary files. Read below about how to enable scriptcs debugging for "Open Folder" in Visual Studio "15".

Steps

  1. Open Visual Studio.
  2. Open the Open Project dialog by navigating to File -> Open -> Project/Solution.
  3. Locate the folder where the scriptcs.exe file is located and open the executable file. Yes, you can open an .exe in VS!
  4. Right-click the scriptcs solution item and click Properties.
  5. Provide values for the following fields:
    • Arguments: server.csx -debug
    • Working directory: the source folder of the app you want to debug, in this the directory where server.csx is located.
  6. Close the Properties window and save the solution.
  7. Add server.csx to the solution by right-clicking the solution and selecting Add Existing Item.
  8. Set a breakpoint in the return "Hello World"; line of the TestController.
  9. Press F5.
  10. Open any browser and navigate to localhost:8080/api/test.

That's it, the breakpoint will be hit. You have all the goodness of VS, such as the Immediate Window, Add Watch to help you debugging.