Do I really need to change filename in launch.json when I debug a unique javascript file?

Viewed 1062

I'm currently debugging a couple of javascript files. It's very frustrating to update the file path before I can start the debugger in Visual Studio Code.

Isn't it possible somehow to configure the vsc debugger to automatic take the file that the user has open and start debugger automatic to avoid update this launch.json script over and over again?

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program":
        "${workspaceRoot}/server/operations/database/eClassKeywordsImport.js"
      // "${workspaceRoot}/server/operations/NPL/stringAlgo.js"
      // another file ...
      // another file ...
    }
  ]
}
2 Answers

According to the VS Code documentation, there is:

"program": "${file}",

Feeds in the path of the file currently open in the active editor tab.

You have to play around with different options found here https://code.visualstudio.com/docs/editor/variables-reference in my case for example ${file} prints the whole path, not just the file name so a better option was ${relativeFile}

More:

Predefined variables#
The following predefined variables are supported:

${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${fileWorkspaceFolder} - the current opened file's workspace folder
${relativeFile} - the current opened file relative to workspaceFolder
${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
${execPath} - the path to the running VS Code executable
${defaultBuildTask} - the name of the default build task
${pathSeparator} - the character used by the operating system to separate components in file paths
Predefined variables examples#
Supposing that you have the following requirements:

A file located at /home/your-username/your-project/folder/file.ext opened in your editor;
The directory /home/your-username/your-project opened as your root workspace.
So you will have the following values for each variable:

${workspaceFolder} - /home/your-username/your-project
${workspaceFolderBasename} - your-project
${file} - /home/your-username/your-project/folder/file.ext
${fileWorkspaceFolder} - /home/your-username/your-project
${relativeFile} - folder/file.ext
${relativeFileDirname} - folder
${fileBasename} - file.ext
${fileBasenameNoExtension} - file
${fileDirname} - /home/your-username/your-project/folder
${fileExtname} - .ext
${lineNumber} - line number of the cursor
${selectedText} - text selected in your code editor
${execPath} - location of Code.exe
${pathSeparator} - / on macOS or linux, \\ on Windows
Related