简体   繁体   English

断点无法通过 Windows 10 和 WSL2 上的 Visual Studio Code 在 Chrome 中调试 React 应用程序

[英]Breakpoints not working debugging React app in Chrome through Visual Studio Code on Windows 10 and WSL2

After this year's MSBuild conference and the announcement of Terminal 1.x, winget and other extras, I wanted to give Windows 10 another test run ahead of needing to purchase a new laptop (Surface Book 3 or MacBook Pro...that is the question).在今年的 MSBuild 会议和 Terminal 1.x、winget 和其他附加功能的发布之后,我想在需要购买新笔记本电脑(Surface Book 3 或 MacBook Pro ...... )。

The Issue问题

Breakpoints aren't working when debugging web apps in Chrome on Windows 10 using WSL2 and Visual Studio Code.使用 WSL2 和 Visual Studio Code 在 Windows 10 上的 Chrome 中调试 web 应用程序时,断点不起作用。 When running a debug session the message Breakpoint set but not yet bound is shown.运行调试 session 时,会显示消息Breakpoint set but not yet bound

The exact same app works flawlessly when debugging on MacOS.在 MacOS 上调试时,完全相同的应用程序可以完美运行。

My Setup我的设置

MacBook Pro running the latest version of MacOS with Windows 10 Pro installed under BootCamp. MacBook Pro 运行最新版本的 MacOS,并在 BootCamp 下安装了 Windows 10 Pro。

Windows 10 has WSL2 running Ubuntu 20.04. Windows 10 具有运行 Ubuntu 20.04 的 WSL2。 Terminal 1.x is installed and used to access the Linux commandline.终端 1.x 已安装并用于访问 Linux 命令行。

Visual Studio Code is the latest 1.45.1 stable release and includes the WSL remote development extension (0.44.2) on Windows 10. VSCode is launched from within WSL2 by running code. Visual Studio Code 是最新的 1.45.1 稳定版本,包括 Windows 10 上的 WSL 远程开发扩展 (0.44.2)。VSCode 通过运行code. within the project directory.在项目目录中。

Debugger for Chrome extension is 4.12.8 Chrome 扩展的调试器是 4.12.8

The Application应用程序

The application is a default Create React App with only a small change to assign breakpoints.该应用程序是一个默认的 Create React 应用程序,仅对分配断点进行了少量更改。

I start by running:我首先运行:

npx create-react-app sandbox

I then simplified src/App.js and added a few arbitrary variables and assignments to use as breakpoint tests.然后我简化了src/App.js并添加了一些任意变量和赋值作为断点测试。

The App.js file contents. App.js文件内容。

import React from 'react';
import './styles/main.css';

function App() {
  const test = true;
  let temp = 9;
  temp = 10;
  return (
    <div>
      <h1>Breakpoint test</h1>
      <p>Did it work?</p>
    </div>
  );
}

export default App;

I place a breakpoint on the const and let creation lines as well as the reassignment of temp .我在const上放置了一个断点, let创建行以及temp的重新分配。

My launch.json contents as recommended by the Create React App editor setup documentation .我的launch.json内容由Create React App editor setup documentation推荐。

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Chrome",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/src",
      "sourceMapPathOverrides": {
        "webpack:///src/*": "${webRoot}/*"
      }
    }
  ]
}

Win10 - What happens when running a Debug session? Win10 - 运行调试 session 时会发生什么?

I run the Create React App using npm run start and when I run the Launch Chrome debug config it automatically opens Chrome as expected.我使用npm run start运行 Create React App,当我运行 Launch Chrome 调试配置时,它会按预期自动打开 Chrome。

Unfortunately, the breakpoints are ignored and inside Visual Studio Code the breakpoints are shown as unfilled circles.不幸的是,断点被忽略,在 Visual Studio Code 中,断点显示为未填充的圆圈。 The message given is Breakpoint set but not yet bound .给出的消息是Breakpoint set but not yet bound

MacOS - What happens when running a Debug session? MacOS - 运行调试 session 时会发生什么?

Chrome opens and control is shifted back to Visual Studio Code with the breakpoint information showing (eg variables data, call stack, etc). Chrome 打开,控制权转移回 Visual Studio Code,并显示断点信息(例如变量数据、调用堆栈等)。

Win10 - Firefox works Win10 - Firefox 工作

Interesting side point but Firefox debugging works.有趣的一点,但 Firefox 调试工作。 When running a Firefox debug session I do have to refresh the initial page load before the breakpoints trigger though.在运行 Firefox 调试 session 时,我确实必须在断点触发之前刷新初始页面加载。

The breakpoint initially showed the error Unverified Breakpoint .断点最初显示错误Unverified Breakpoint Clicking on this prompted a wizard to add a pathMappings to my config.单击它会提示向导将pathMappings添加到我的配置中。

The Firefox launch.json configuration in use on Windows 10 is: Firefox launch.json配置在 Windows 10 上使用是:

    {
      "name": "Launch Firefox",
      "type": "firefox",
      "request": "launch",
      "reAttach": true,
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/src",
      "pathMappings": [
          {
             "url": "http://localhost:3000/home/rando/dev/sandbox/src",
             "path": "${workspaceFolder}/src"
          }
      ]
    }

Note that /home/rando/dev/sandbox/src is the location of the app within WSL2 Ubuntu.请注意, /home/rando/dev/sandbox/src是应用程序在 WSL2 Ubuntu 中的位置。 MacOS Firefox setup is the same but without the pathMappings . MacOS Firefox 设置相同,但没有pathMappings

Conclusion结论

At this stage I can only conclude it is something to do with the path mappings needing to be set differently despiteVisual Studio Code WSL documentation hinting no additional changes are required .在这个阶段,我只能得出结论,尽管Visual Studio Code WSL 文档暗示不需要额外的更改,但路径映射需要以不同方式设置。

Help me, StackOverflow.帮助我,StackOverflow。 You're my only hope.你是我唯一的希望。

I just ran into this and I think I have it working for myself.我刚遇到这个,我想我已经为自己工作了。 Using the .script command of the Debugger for Chrome extension , I saw the below output.使用Debugger for Chrome extension.script命令,我看到了下面的 output。

› http://localhost:3000/static/js/0.chunk.js (/__vscode-remote-uri__/home/user/projects/TachiWeb-React/src/static/js/0.chunk.js)
    - /home/user/projects/TachiWeb-React/node_modules/@babel/runtime-corejs2/core-js/date/now.js (/home/user/projects/TachiWeb-React/node_modules/@babel/runtime-corejs2/core-js/date/now.js)

It seems it doesn't append your webroot to the inferred local path.似乎它不是 append 您的 webroot 到推断的本地路径。 But using ${webRoot}/* also doesn't work for some reason.但是由于某种原因,使用${webRoot}/*也不起作用。 Doing so leads to your path repeating twice like the below result.这样做会导致您的路径重复两次,如下面的结果。

/__vscode-remote-uri__/home/user/projects/TachiWeb-React/src/home/user/projects/TachiWeb-React/node_modules/@babel/runtime-corejs2/core-js/date/now.js

But manually writing out "/__vscode-remote-uri__/*" seems to get round the above duplicate path problem.但是手动写出"/__vscode-remote-uri__/*"似乎可以解决上述重复路径问题。

This is my working configuration of launch.json :这是我的launch.json的工作配置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "WSL Chrome",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/src",
      "sourceMapPathOverrides": {
        "/*": "/__vscode-remote-uri__/*"
      }
    }
  ]
}

After upgrading to WSL2 my debugger (only using attach) stopped working.升级到 WSL2 后,我的调试器(仅使用附加)停止工作。

I'm using the "Remote WSL" feature on VS Code connecting to my $wsl/Ubuntu/project-path-here + Edge (the good one) + Latest VS Code + Win 10 machine with all updates (stable)我在 VS Code 上使用“远程 WSL”功能连接到我的$wsl/Ubuntu/project-path-here + Edge(好的)+ 最新的 VS Code + Win 10 机器,所有更新(稳定)

Checking .scripts on vscode debug console reveals that my paths are still weird, not repeating, but still weird.在 vscode 调试控制台上检查.scripts发现我的路径仍然很奇怪,不重复,但仍然很奇怪。

1 - using the "classic" config (that used to work just fine): 1 - 使用“经典”配置(过去工作得很好):

"name": "Attach to Edge",
"type": "edge",
"request": "attach",
"port": 9222,
"url": "http://localhost:4300/*",
"webRoot": "${workspaceFolder}"

results in this:结果是:

webpack:///./src/app/component-one/component-one.component.html (\home\my-username\employer-folder\mono-repo\actual-project\src\app\component-one\component-one.component.html)

2 - using the first answer on this post (also from https://github.com/microsoft/vscode-remote-release/issues/2068 and https://github.com/microsoft/vscode-chrome-debug/issues/899 ): 2 - 使用这篇文章的第一个答案(也来自https://github.com/microsoft/vscode-remote-release/issues/2068https://github.com/microsoft/vscode-chrome-debug/issues/ 899 ):

"name": "Attach to Edge",
"type": "edge",
"request": "attach",
"port": 9222,
"url": "http://localhost:4300/*",
"webRoot": "${workspaceFolder}",
"sourceMapPathOverrides": {
   "/*": "/__vscode-remote-uri__/*"
}

results in this (which looks incorrect with the c: usage):导致这个(与 c: 用法看起来不正确):

webpack:///./src/app/component-one/component-one.component.html (c:\home\my-username\employer-folder\mono-repo\actual-project\webpack:\src\app\component-one\component-one.component.html)

At this point I've tried every single combination in the launch.json, even tried to mix $wsl/Ubuntu to get the full correct path (checked on .scripts ) and the breakpoint is always disabled.在这一点上,我已经尝试了 launch.json 中的每一个组合,甚至尝试混合$wsl/Ubuntu以获得完整正确的路径(在.scripts上检查)并且断点始终被禁用。

Also, when I press the attach it is weirdly fast... it used to take a while (probably checking/using source maps).此外,当我按下附件时,它的速度非常快......过去需要一段时间(可能检查/使用源地图)。

Ended up doing the following (wanted to see if the same would happen):最终做了以下事情(想看看是否会发生同样的事情):

  • Added --start-debugger-server to my Firefox Developer launch config在我的 Firefox 开发人员启动配置中添加了--start-debugger-server

  • Updated launch.json with a new attach config for FF (also installed Debugger for Firefox extension):更新了launch.json ,为 FF 添加了新的附加配置(还安装了 Firefox 扩展的调试器):

"name": "Attach to Firefox",
"type": "firefox",
"request": "attach",
"url": "http://localhost:4300/*",
"webRoot": "${workspaceFolder}",

BINGO, first try... no extra path configs or anything, the way it should be and the way it was before my WSL upgrade (plus no need for port attribute). BINGO,首先尝试...没有额外的路径配置或任何东西,它应该是这样的方式以及它在我的 WSL 升级之前的方式(加上不需要端口属性)。

PS: I'll check on my other pc that has a clean Windows 10 2004 + WSL2 install, I'm thinking this might be an "existing/running wsl1 + upgrade to wsl2" thing. PS:我会检查我的另一台安装了干净的 Windows 10 2004 + WSL2 的电脑,我认为这可能是“现有/正在运行的 wsl1 + 升级到 wsl2”的事情。

The problem I am looking at for most people is a difference on the sourcemap path between the one reflected on Chrome or other browser and the one recognized by vscode.对于大多数人来说,我正在寻找的问题是在 Chrome 或其他浏览器上反映的源映射路径与 vscode 识别的源映射路径之间存在差异。 The configuration that worked for me on Chrome running (npm start on wsl ) was as follows:Chrome运行( wsl上的 npm start )上对我有用的配置如下:

My particular solution:我的特殊解决方案:

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Chrome",
            "request": "launch",
            "type": "pwa-chrome",
            "url": "http://localhost:3000",
            "webRoot": "${workspaceFolder}",
            "sourceMapPathOverrides": {
                "\\mnt\\c\\*": "C:/*"
            }
        }
    ]
}

Question is: How to debug how source paths are translated into Chrome?问题是:如何调试源路径如何转换为 Chrome?

Here is a simple way:这是一个简单的方法:

  1. Start the development server with: npm start启动开发服务器:npm start
  2. Launch Chrome in debug mode(F5) with standard configuration (no need to include sourceMapPathOverrides)使用标准配置以调试模式(F5)启动 Chrome(无需包含 sourceMapPathOverrides)
  3. Go to Chrome Developer console => Sources, Go 到 Chrome 开发者控制台 => 来源,
  4. Find your file (example App.js) and set a breakpoint there.找到您的文件(例如 App.js)并在那里设置断点。

在此处输入图像描述

  1. Refresh the page and go to vscode.刷新页面和go到vscode。

在此处输入图像描述

  1. Set a breakpoint in the same place of the document, in the new file that appeared (/mnt/e/ in my case) and in your file.在文档的同一位置、出现的新文件(在我的情况下为 /mnt/e/)和您的文件中设置断点。

在此处输入图像描述

You'll get the idea that you will have to replace "\mnt\e"... with "E:/" in the sourceMapPathOverrides.您会想到必须在 sourceMapPathOverrides 中将“\mnt\e”...替换为“E:/”。

Specifying the webRoot on launch.json worked.在启动时指定webRoot。json工作。 Something like就像是

"webRoot": "${workspaceFolder}/src/js"

or other folder that your webRoot path might be.或您的 webRoot 路径可能位于的其他文件夹。

https://code.visualstudio.com/docs/nodejs/browser-debugging https://code.visualstudio.com/docs/nodejs/browser-debugging

The problem went away for me the moment I abandoned trying to use Microsoft Edge.当我放弃尝试使用 Microsoft Edge 时,问题就消失了。 Went ahead and installed Chrome, created a launch configuration and it worked.继续安装 Chrome,创建一个启动配置,它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SPFx断点和Visual Studio Code(而不是Chrome)中的调试 - SPFx breakpoints and debugging from Visual Studio Code instead from Chrome 在Chrome问题中逐步调试React代码 - Step debugging through React code in Chrome issue 调试时,Visual Studio Code 不是 npm 启动我的 React JS 应用程序 - Visual Studio Code is not npm start-ing my React JS app when debugging 在 Visual Studio Code 中调试 React 应用程序时自动启动 NPM - Auto-start NPM when debugging React app in Visual Studio Code 调试不工作断点不停止 Visual Studio Mac Javascript React - Debugging not working breakpoint not stopping Visual Studio Mac Javascript React 在 Visual Studio 2019 中调试 React App 不起作用 - Debugging React App in Visual Studio 2019 does not work 用于 Javascript(React)的 visual studio code Intellisense 不工作 - visual studio code Intellisense for Javascript(React) is not working 如何使用 Visual Studio 2017 创建 React 应用程序(无 Visual Studio 代码) - How to create react app with visual studio 2017 (no visual studio code) 使用 Visual Studio Code 进行 React Native 调试不起作用 - React Native debug with Visual Studio Code not working 使用 typescript 反应项目的 Visual Studio Code 调试器不会在断点处停止 - Visual Studio Code debugger on react project with typescript doesn't stop at breakpoints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM