Lỗi bị out options text editor trong visual năm 2024
One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop. Show
Debugger extensionsVS Code has built-in debugging support for the Node.js runtime and can debug JavaScript, TypeScript, or any other language that gets transpiled to JavaScript. For debugging other languages and runtimes (including PHP, Ruby, Go, C#, Python, C++, PowerShell and many others), look for Below are several popular extensions which include debugging support: Tip: The extensions shown above are dynamically queried. Select an extension tile above to read the description and reviews to decide which extension is best for you. Start debuggingThe following documentation is based on the built-in Node.js debugger, but most of the concepts and features are applicable to other debuggers as well. It is helpful to first create a sample Node.js application before reading about debugging. You can follow the Node.js walkthrough to install Node.js and create a simple "Hello World" JavaScript application (
0). Once you have a simple application set up, this page will take you through VS Code debugging features. Run and Debug viewTo bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut ⇧⌘D (Windows, Linux Ctrl+Shift+D). The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings. If running and debugging is not yet configured (no
1 has been created), VS Code shows the Run start view. The top-level Run menu has the most common run and debug commands: Launch configurationsTo run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details. VS Code keeps debugging configuration information in a
1 file located in a
3 folder in your workspace (project root folder) or in your or . To create a
1 file, click the create a launch.json file link in the Run start view. VS Code will try to automatically detect your debug environment, but if this fails, you will have to choose it manually: Here is the launch configuration generated for Node.js debugging:
If you go back to the File Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)), you'll see that VS Code has created a
3 folder and added the
1 file to your workspace. Note: You can debug a simple application even if you don't have a folder open in VS Code, but it is not possible to manage launch configurations and set up advanced debugging. The VS Code Status Bar is purple if you do not have a folder open. Note that the attributes available in launch configurations vary from debugger to debugger. You can use IntelliSense suggestions (⌃Space (Windows, Linux Ctrl+Space)) to find out which attributes exist for a specific debugger. Hover help is also available for all attributes. Do not assume that an attribute that is available for one debugger automatically works for other debuggers too. If you see red squiggles in your launch configuration, hover over them to learn what the problem is and try to fix them before launching a debug session. Review all automatically generated values and make sure that they make sense for your project and debugging environment. Launch versus attach configurationsIn VS Code, there are two core debugging modes, Launch and Attach, which handle two different workflows and segments of developers. Depending on your workflow, it can be confusing to know what type of configuration is appropriate for your project. If you come from a browser Developer Tools background, you might not be used to "launching from your tool," since your browser instance is already open. When you open DevTools, you are simply attaching DevTools to your open browser tab. On the other hand, if you come from a server or desktop background, it's quite normal to have your editor launch your process for you, and your editor automatically attaches its debugger to the newly launched process. The best way to explain the difference between launch and attach is to think of a launch configuration as a recipe for how to start your app in debug mode before VS Code attaches to it, while an attach configuration is a recipe for how to connect VS Code's debugger to an app or process that's already running. VS Code debuggers typically support launching a program in debug mode or attaching to an already running program in debug mode. Depending on the request (
7 or
8), different attributes are required, and VS Code's
1 validation and suggestions should help with that. Add a new configurationTo add a new configuration to an existing
1, use one of the following techniques:
VS Code also supports compound launch configurations for starting multiple configurations at the same time; for more details, please read this . In order to start a debug session, first select the configuration named Launch Program using the Configuration dropdown in the Run and Debug view. Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) by filtering on Debug: Select and Start Debugging or typing
1 and selecting the configuration you want to debug. As soon as a debugging session starts, the DEBUG CONSOLE panel is displayed and shows debugging output, and the Status Bar changes color (orange for default color themes): In addition, the debug status appears in the Status Bar showing the active debug configuration. By selecting the debug status, a user can change the active launch configuration and start debugging without needing to open the Run and Debug view. Debug actionsOnce a debug session starts, the Debug toolbar will appear on the top of the editor. Action Explanation Continue / Pause F5Continue: Resume normal program/script execution (up to the next breakpoint). Pause: Inspect code executing at the current line and debug line-by-line. Step Over F10Execute the next method as a single command without inspecting or following its component steps. Step Into F11Enter the next method to follow its execution line-by-line. Step Out ⇧F11 (Windows, Linux Shift+F11)When inside a method or subroutine, return to the earlier execution context by completing remaining lines of the current method as though it were a single command. Restart ⇧⌘F5 (Windows, Linux Ctrl+Shift+F5)Terminate the current program execution and start debugging again using the current run configuration. Stop ⇧F5 (Windows, Linux Shift+F5)Terminate the current program execution. Tip: Use the setting Run modeIn addition to debugging a program, VS Code supports running the program. The Debug: Run (Start Without Debugging) action is triggered with ⌃F5 (Windows, Linux Ctrl+F5) and uses the currently selected launch configuration. Many of the launch configuration attributes are supported in 'Run' mode. VS Code maintains a debug session while the program is running, and pressing the Stop button terminates the program. Tip: The Run action is always available, but not all debugger extensions support 'Run'. In this case, 'Run' will be the same as 'Debug'. BreakpointsBreakpoints can be toggled by clicking on the editor margin or using F9 on the current line. Finer breakpoint control (enable/disable/reapply) can be done in the Run and Debug view's BREAKPOINTS section.
If the debugger supports breaking on different kinds of errors or exceptions, those will also be available in the BREAKPOINTS view. The Reapply All Breakpoints command sets all breakpoints again to their original location. This is helpful if your debug environment is "lazy" and "misplaces" breakpoints in source code that has not yet been executed. Optionally, breakpoints can be shown in the editor's overview ruler by enabling the setting
7: LogpointsA Logpoint is a variant of a breakpoint that does not "break" into the debugger but instead logs a message to the console. Logpoints are especially useful for injecting logging while debugging production servers that cannot be paused or stopped. A Logpoint is represented by a "diamond" shaped icon. Log messages are plain text but can include expressions to be evaluated within curly braces ('{}'). Just like regular breakpoints, Logpoints can be enabled or disabled and can also be controlled by a condition and/or hit count. Note: Logpoints are supported by VS Code's built-in Node.js debugger, but can be implemented by other debug extensions. The Python and Java extensions, for example, support Logpoints. Data inspectionVariables can be inspected in the VARIABLES section of the Run and Debug view or by hovering over their source in the editor. Variable values and expression evaluation are relative to the selected stack frame in the CALL STACK section. Variable values can be modified with the Set Value action from the variable's context menu. Additionally, you can use the Copy Value action to copy the variable's value, or Copy as Expression action to copy an expression to access the variable. Variables and expressions can also be evaluated and watched in the Run and Debug view's WATCH section. Variable names and values can be filtered by typing while the focus is on the VARIABLES section. Launch.json attributesThere are many
1 attributes to help support different debuggers and debugging scenarios. As mentioned above, you can use IntelliSense (⌃Space (Windows, Linux Ctrl+Space)) to see the list of available attributes once you have specified a value for the
9 attribute. The following attributes are mandatory for every launch configuration:
Here are some optional attributes available to all launch configurations:
Many debuggers support some of the following attributes:
Variable substitutionVS Code makes commonly used paths and other values available as variables and supports variable substitution inside strings in
1. This means that you do not have to use absolute paths in debug configurations. For example,
4 gives the root path of a workspace folder,
5 the file open in the active editor, and
6 the environment variable 'Name'. You can see a full list of predefined variables in the Variables Reference or by invoking IntelliSense inside the
1 string attributes.
Platform-specific properties
8 supports defining values (for example, arguments to be passed to the program) that depend on the operating system where the debugger is running. To do so, put a platform-specific literal into the
1 file and specify the corresponding properties inside that literal. Below is an example that passes
0 to the program differently on Windows:
Valid operating properties are
1 for Windows,
2 for Linux, and
3 for macOS. Properties defined in an operating system specific scope override properties defined in the global scope. Please note that the
9 property cannot be placed inside a platform-specific section, because
9 indirectly determines the platform in remote debugging scenarios, and that would result in a cyclic dependency. In the example below, debugging the program always stops on entry except on macOS:
Global launch configurationVS Code supports adding a
6 object inside your User settings. This
6 configuration will then be shared across your workspaces. For example:
Advanced breakpoint topicsConditional breakpointsA powerful VS Code debugging feature is the ability to set conditions based on expressions, hit counts, or a combination of both.
You can add a condition and/or hit count when creating a source breakpoint (with the Add Conditional Breakpoint action) or when modifying an existing one (with the Edit Condition action). In both cases, an inline text box with a dropdown menu opens where you can enter expressions: Condition and hit count editing support is also supported for function and exception breakpoints. You can initiate condition editing from the context menu or the new inline Edit Condition action. An example of condition editing in the BREAKPOINTS view: If a debugger does not support conditional breakpoints, the Add Conditional Breakpoint and Edit Condition actions will be missing. Inline breakpointsInline breakpoints will only be hit when the execution reaches the column associated with the inline breakpoint. This is particularly useful when debugging minified code which contains multiple statements in a single line. An inline breakpoint can be set using ⇧F9 (Windows, Linux Shift+F9) or through the context menu during a debug session. Inline breakpoints are shown inline in the editor. Inline breakpoints can also have conditions. Editing multiple breakpoints on a line is possible through the context menu in the editor's left margin. Function breakpointsInstead of placing breakpoints directly in source code, a debugger can support creating breakpoints by specifying a function name. This is useful in situations where source is not available but a function name is known. A function breakpoint is created by pressing the + button in the BREAKPOINTS section header and entering the function name. Function breakpoints are shown with a red triangle in the BREAKPOINTS section. Data breakpointsIf a debugger supports data breakpoints, they can be set from the context menu in the VARIABLES view. The Break on Value Change/Read/Access commands will add a data breakpoint that is hit when the value of the underlying variable changes/is read/is accessed. Data breakpoints are shown with a red hexagon in the BREAKPOINTS section. Debug Console REPLExpressions can be evaluated with the Debug Console REPL (Read-Eval-Print Loop) feature. To open the Debug Console, use the Debug Console action at the top of the Debug pane or use the View: Debug Console command (⇧⌘Y (Windows, Linux Ctrl+Shift+Y)). Expressions are evaluated after you press Enter and the Debug Console REPL shows suggestions as you type. If you need to enter multiple lines, use Shift+Enter between the lines and then send all lines for evaluation with Enter. Debug Console input uses the mode of the active editor, which means that the Debug Console input supports syntax coloring, indentation, auto closing of quotes, and other language features. Note: You must be in a running debug session to use the Debug Console REPL. Redirect input/output to/from the debug targetRedirecting input/output is debugger/runtime specific, so VS Code does not have a built-in solution that works for all debuggers. Here are two approaches you might want to consider:
Here's an example
1 configuration:
This approach requires that the "<" syntax is passed through the debugger extension and ends up unmodified in the Integrated Terminal. Multi-target debuggingFor complex scenarios involving more than one process (for example, a client and a server), VS Code supports multi-target debugging. Using multi-target debugging is simple: after you've started a first debug session, you can just launch another session. As soon as a second session is up and running, the VS Code UI switches to multi-target mode: Compound launch configurationsAn alternative way to start multiple debug sessions is by using a compound launch configuration. A compound launch configuration lists the names of two or more launch configurations that should be launched in parallel. Optionally a
3 can be specified that is run before the individual debug sessions are started. The boolean flag
1 controls whether manually terminating one session will stop all of the compound sessions.
Compound launch configurations are displayed in the launch configuration dropdown menu. Remote debuggingVS Code does not itself support remote debugging: this is a feature of the debug extension you are using, and you should consult the extension's page in the Marketplace for support and details. There is, however, one exception: the Node.js debugger included in VS Code supports remote debugging. See the topic to learn how to configure this. Automatically open a URI when debugging a server programDeveloping a web program typically requires opening a specific URL in a web browser in order to hit the server code in the debugger. VS Code has a built-in feature "serverReadyAction" to automate this task. Here is an example of a simple Node.js Express application:
This application first installs a "Hello World" handler for the "/" URL and then starts to listen for HTTP connections on port 3000. The port is announced in the Debug Console, and typically, the developer would now type
2 into their browser application. The serverReadyAction feature makes it possible to add a structured property
0 to any launch config and select an "action" to be performed:
Here the
4 property describes the regular expression for matching the program's output string that announces the port. The pattern for the port number is put into parenthesis so that it is available as a regular expression capture group. In this example, we are extracting only the port number, but it is also possible to extract a full URI. The
5 property describes how the port number is turned into a URI. The first
6 is substituted by the first capture group of the matching pattern. The resulting URI is then opened outside of VS Code ("externally") with the standard application configured for the URI's scheme. Trigger Debugging via Edge or ChromeAlternatively, the
7 can be set to
8 or
9. In this mode, a `Debuggers`0 property can be added that is passed to the Chrome or Edge debug session. To simplify things a bit, most properties are optional and we use the following fallback values:
Triggering an Arbitrary Launch ConfigIn some cases, you may need to configure additional options for the browser debug session--or use a different debugger entirely. You can do this by setting
7 to `Debuggers`5 with a
7 property set to the name of the launch configuration to start when the
4 is matched. The named launch configuration must be in the same file or folder as the one with the
0. Here the serverReadyAction feature in action: Next stepsTo learn about VS Code's Node.js debugging support, take a look at:
To see tutorials on the basics of Node.js debugging, check out these videos:
To learn about debugging support for other programming languages via VS Code extensions:
To learn about VS Code's task running support, go to:
To write your own debugger extension, visit:
Common questionsWhat are the supported debugging scenarios?Debugging of Node.js-based applications is supported on Linux, macOS, and Windows out of the box with VS Code. Many other scenarios are supported by VS Code extensions available in the Marketplace. I do not see any launch configurations in the Run and Debug view dropdown. What is wrong?The most common problem is that you did not set up
1 or there is a syntax error in that file. Alternatively, you might need to open a folder, since no-folder debugging does not support launch configurations. |