Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEXT-517] Breakpoint not getting hit in VSCode with Next.js debugger config #45697

Closed
1 task done
InfiniteLearner opened this issue Feb 8, 2023 · 24 comments · Fixed by #51467
Closed
1 task done

[NEXT-517] Breakpoint not getting hit in VSCode with Next.js debugger config #45697

InfiniteLearner opened this issue Feb 8, 2023 · 24 comments · Fixed by #51467
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@InfiniteLearner
Copy link

InfiniteLearner commented Feb 8, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.14.2
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.1.7-canary.7
      eslint-config-next: 13.1.6
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

https://github.com/InfiniteLearner/debug_error_next_reproduction

To Reproduce

!!!!!!!!!!!!!!!!!!!!! You should be using node v 16.14.2 !!!!!!!!!!!!!!!!!!!!

(Because of other constraints I can not update my node version so this is not an option)

Create a new next.js app
npx create-next-app@latest
Use every default options

Add a new config for debugging in vsCode (.vscode/launch.json):

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Next.js: debug server-side",
        "type": "node-terminal",
        "request": "launch",
        "command": "npm run dev"
      },
      {
        "name": "Next.js: debug client-side",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:3000"
      },
      {
        "name": "Next.js: debug full stack",
        "type": "node-terminal",
        "request": "launch",
        "command": "npm run dev",
        "serverReadyAction": {
          "pattern": "started server on .+, url: (https?://.+)",
          "uriFormat": "%s",
          "action": "debugWithChrome"
        }
      }
    ]
  }

Create a new async function in index.tsx and call it (with an onClick for example):

async function greeting(){
    console.log("Hello");
    console.log("Goodbye");
  }

...

<p onClick={greeting}></p>
...

Add a new breakpoint in the function.
2023-02-08_11h49_26

Run in debug mode (either server-side, client-side or fullstack).

Describe the Bug

On click of the element, the logs will be displayed without being stop.
The breakpoints are not getting hit.

2023-02-08_11h50_08

It should be noted that breakpoints on the returned component are working fine but none in functions.
So if you had a breakpoint on your div (or any other component) it will stop.

Expected Behavior

The breakpoints should get hit and the logs not be displayed.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-517

@InfiniteLearner InfiniteLearner added the bug Issue was opened via the bug report template. label Feb 8, 2023
@naut305

This comment was marked as off-topic.

@jonleung
Copy link

jonleung commented Feb 9, 2023

I'm having the same problem.

My debugger attaches, it just doesn't stop at a breakpoint

  • Operating System:
    • Platform: OSX
    • Arch: x64
    • Version: Ventura
  • Binaries:
    -Node: v18.14.0
  • Relevant packages:
    • next: 13.1.6
    • eslint-config-next: 13.1.6
    • react: 18.2.0
    • react-dom: 18.2.0

I can't get to pause VS Code or just Chrome

I should also note that I'm using TypeScript

@github-actions github-actions bot added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 10, 2023
@balazsorban44
Copy link
Member

Hmm. I cannot reproduce the OP's issue, we will need to look more into this. @InfiniteLearner attaching a reproduction would be very helpful. 🙏

@balazsorban44 balazsorban44 added linear: next Confirmed issue that is tracked by the Next.js team. and removed linear: next Confirmed issue that is tracked by the Next.js team. labels Feb 10, 2023
@balazsorban44 balazsorban44 changed the title Breakpoint not getting hit in VSCode with Next.js debugger config [NEXT-517] Breakpoint not getting hit in VSCode with Next.js debugger config Feb 10, 2023
@jonleung
Copy link

jonleung commented Mar 2, 2023

Here is me reproducing the issue with Next.js v13.2.3 but I also tested 13.2.2, 13.2.1 13.1.6, 12.3.4 and I could not get anything to trigger a breakpoint:

Here is me starting with npx create-next-app@latest today: https://capture.dropbox.com/sSSjb6nxQGN4Yl43

@HelmerBarcos
Copy link

Im also having this problem with all Next.js versions mentioned above.
VScode version 1.76

@HelmerBarcos
Copy link

HelmerBarcos commented Mar 4, 2023

For the next.js version 13.1.6 it will start 2 debuggers after running npm run dev. 1 on port 9229 and another one on port 9230

1️⃣ without dockerized nextjs app

Inside the package.json

    "dev": "NODE_OPTIONS='--inspect' next dev",

Inside the launch.json

    {
      "name": "Attach",
      "address": "127.0.0.1",
      "port": 9230,
      "request": "attach",
      // "localRoot": "${workspaceFolder}/frontend", # just if next app is not inside your root dir
      "skipFiles": ["<node_internals>/**"],
      "type": "node"
    }

If you now attach the vscode debugger, it will recognize breakpoints

2️⃣ with dockerized nextjs app

🚨 You will need to update remoteRoot as well as localRoot if you are working with a monorepo
🚨 You will need to expose the 9230 port by defining it inside the docker-compose.yml file.
You will need to expose the 9230 port by defining it inside the docker-compose.yml file.

Inside the package.json
🚨 Here port 9229 since it will create a 2nd debugger on port 9230. If you assign port 9230 inside the package.json it will probably start 2 dubuggers on 2 ports 9230 and 9231

    "dev": "NODE_OPTIONS='--inspect=0.0.0.0:9229' next dev",

Inside the launch.json

    {
      "name": "Attach",
      "address": "127.0.0.1",
      "port": 9230,
      "request": "attach",
      "localRoot": "${workspaceFolder}/path/to/root", # change this. if the app if in the root dir then ${workspaceFolder} will works. If not then ${workspaceFolder}/subpath does the magic
      "remoteRoot": "/app", # needed. this will be the WORKDIR where the next app is located.
      "skipFiles": ["<node_internals>/**"],
      "type": "node"
    }

Then start the app by defining the command property inside the docker-compose.yml

    command: npm run dev

If you now attach the vscode debugger, it will recognize breakpoints

@gregmartyn
Copy link

--inspect=0.0.0.0:9229 doesn't work anymore because https://github.com/vercel/next.js/blob/canary/packages/next/src/cli/next-dev.ts#L524 assumes it can do debugPort + 1 with debugPort = "0.0.0.0:9229"

@gregmartyn
Copy link

Opened a separate issue for that: #47083

@ukabu
Copy link

ukabu commented Mar 25, 2023

For the next.js version 13.1.6 it will start 2 debuggers after running npm run dev. 1 on port 9229 and another one on port 9230

1️⃣ without dockerized nextjs app

Inside the package.json

    "dev": "NODE_OPTIONS='--inspect' next dev",

Inside the launch.json

    {
      "name": "Attach",
      "address": "127.0.0.1",
      "port": 9230,
      "request": "attach",
      // "localRoot": "${workspaceFolder}/frontend", # just if next app is not inside your root dir
      "skipFiles": ["<node_internals>/**"],
      "type": "node"
    }

If you now attach the vscode debugger, it will recognize breakpoints

Node v19.7.0
Next.js v13.2.4

Tried this, yet breakpoints are still no go.

In a .ts file:

image

> bio-trousse@0.1.0 dev
> NODE_OPTIONS='--inspect' next dev

Debugger listening on ws://127.0.0.1:9229/c488c6b0-d0ad-462a-960b-0d46b92016e0
For help, see: https://nodejs.org/en/docs/inspector
warn  - the --inspect option was detected, the Next.js server should be inspected at port 9230.
Debugger listening on ws://127.0.0.1:9230/520830bc-1763-4201-a714-a034dcb30c1d
For help, see: https://nodejs.org/en/docs/inspector
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /workspaces/bio-trousse/.env.local
warn  - You have enabled experimental feature (appDir) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
Starting inspector on 127.0.0.1:9229 failed: address already in use
event - compiled client and server successfully in 1778 ms (262 modules)
wait  - compiling...
event - compiled client and server successfully in 504 ms (262 modules)

@ukabu
Copy link

ukabu commented Mar 25, 2023

I even tried the vscode auto attach feature which attach to all processes (3) when running npm run dev and breakpoints are not good either.

image

Debugger listening on ws://127.0.0.1:53266/376fb431-e929-47d4-9164-15e2b60c00e6
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

> bio-trousse@0.1.0 dev
> next dev

Debugger listening on ws://127.0.0.1:53307/a2c5dd46-85ea-44b7-9dcb-503d480fb1bf
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Debugger listening on ws://127.0.0.1:53096/0ade082a-860f-4fe7-820f-decf43d8f1db
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /workspaces/bio-trousse/.env.local
warn  - You have enabled experimental feature (appDir) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info  - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback

Debugger listening on ws://127.0.0.1:53243/86f557ca-e973-46cd-ba81-11724d58615d
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
event - compiled client and server successfully in 1882 ms (262 modules)
wait  - compiling...
event - compiled client and server successfully in 585 ms (262 modules)

@ukabu
Copy link

ukabu commented Mar 25, 2023

Ok, scratch that, with auto attach, it did manage to hit a break point after reloading the app in the broswer.

@asafpelegcodes
Copy link

Just wondering how is this not a huge problem for tons of people or why isn't it getting much attention from authors?

@afreemanio
Copy link

afreemanio commented May 9, 2023

For people scratching their heads in disbelief over this regression like I was, you can set the debugger to fire manually by hardcoding the option in the node_module:
next/dist/server/next-server.js
Line 168

class NextNodeServer extends _baseserver.default {
    constructor(options){
        options.isNodeDebugging = true; // Added here
        // Initialize super class
        super(options);
        this._validFilesystemPathSet = null;

Then, as in some of the other threads, you have to set the debugger to attach to the second debug port it tells you.
I'm sure there's a way to set the port in a similar way, and this doesn't address the other related issue of not being able to set the address (0.0.0.0), but this at least allowed me to server debug again.

@NullReferenceEric
Copy link

I'm also experiencing this issue. Tried

  1. modifying next-server.js -no luck
  2. tweaking the launch.json - no luck

VSCode version. 1.78
OS: M1 Mac Ventura 13.0

@alxdsz
Copy link

alxdsz commented May 17, 2023

Just for the record I'm experiencing the exact same issue in IntelliJ, so maybe it's not even connected to the IDE.
It worked a while ago though (not sure what changed over the time)

@juhahe
Copy link

juhahe commented May 22, 2023

Not sure about the OP, but I think later commenters are experiencing this issue with the current version of next.js. The issue is that next starts several node processes and the debug server is started on the wrong node process. That process does not execute the code where the breakpoint is set, so the debugger never pauses the execution. That's why @ukabu had success with "auto attach feature which attach to all processes". I'm not familiar with this feature myself but attaching the debugger to the process that actually executes the code with the breakpoint would seem to work.

@NullReferenceEric
Copy link

Good point. @ukabu can you detail your attach to all processes workflow?

@ukabu
Copy link

ukabu commented May 22, 2023

Just set the Javascript: Auto attach filter to Always in your workspace or user settings.

Then you can start the dev server with npm run dev in a terminal.

image

@albertlieyingadrian
Copy link

Facing the same issue here

@Cow258
Copy link
Contributor

Cow258 commented Jun 18, 2023

It is worth noting that
We can't hit the breakpoint on the first execution
because the file does not exist there yet or was not compiled
In most cases, the breakpoint can only be triggered normally during the second execution

breakpoint has been added before start the debug process
image

after start the debug process, but the breakpoint not yet find the code
image

after start the debug process and execute once, the breakpoint should be triggered on next execution
image

the breakpoint has been triggered
image

@lu-zen
Copy link

lu-zen commented Jun 20, 2023

Manually attaching works here:

- info the --inspect option was detected, the Next.js server for app should be inspected at port 32957.
- info the --inspect option was detected, the Next.js server for pages should be inspected at port 35423.

Using CMD+SHIFT+P then Debug: Attach to Node process and selecting the corresponding process.
Didn't have any luck launching via debugger, it attaches to the wrong proccess.

Ps: node v18.16.0

@kodiakhq kodiakhq bot closed this as completed in #51467 Jun 25, 2023
kodiakhq bot pushed a commit that referenced this issue Jun 25, 2023
## Fixing a bug

Fixes #50489
Fixes #48767
Fixes #45697

## What?
When running `NODE_OPTIONS='--inspect' next dev`
Then go to `http://localhost:3000/`
Will display error message `WebSockets request was expected` like the following screenshot
![image](https://github.com/vercel/next.js/assets/14261588/d2f3e520-7cce-40db-be69-99980804cc51)

Also the debug port for app and page still not follow by user input
When `NODE_OPTIONS='--inspect=8000' next dev` the app debug port still `54151`
![image](https://github.com/vercel/next.js/assets/14261588/e3d25c0e-9d00-4767-94d6-d954776912b2)


## Why?
#50248 added a function `getFreePort()` and it used on debug port and HTTP server port
So conflict happen between debug and HTTP port
Then show up error `WebSockets request was expected`

Here are some references about this error:
https://stackoverflow.com/questions/49766500/websockets-request-was-expected-error-when-using-inspect-brk-option


## How?
1. `getFreePort()` should only use on HTTP server
2. Added `getDebugPort()` for read the port from user input
3. Assign port to each worker
4. Add accurate info log for each debug port, e.g.: `proxy`,`router`,`app`,`page`

When `NODE_OPTIONS='--inspect' next dev` 
![image](https://github.com/vercel/next.js/assets/14261588/2260fdff-13fe-435a-9812-984315c0ea2e)

When `NODE_OPTIONS='--inspect=8000' next dev` 
![image](https://github.com/vercel/next.js/assets/14261588/de6e2f2d-9216-481f-b6db-7c5132e97b6f)

Also fix VSCode debugger
It is worth noting that
We can't hit the breakpoint on the first execution
because the file does not exist there yet or was not compiled
In most cases, the breakpoint can only be triggered normally during the second execution
![image](https://github.com/vercel/next.js/assets/14261588/dccded50-46c5-4b6d-beb7-ff0e5324327a)


Closes NEXT-1179
Closes NEXT-517



Co-authored-by: Shu Ding <3676859+shuding@users.noreply.github.com>
@cesarvarela
Copy link

Just wondering how is this not a huge problem for tons of people or why isn't it getting much attention from authors?

This is really baffling, but the reason is that most devs don't know how to use debuggers and rely mostly on logs which is soo inefficient. It is a lost battle at this point.

@nicu-chiciuc
Copy link

A thing that doesn't seem to be mentioned is that I'm able to stop the execution in a server function by adding a debugger statement.
Waiting for the first execution to pass then waiting for the 2nd one to attach doesn't seem to work for me.

The issue with the debugger (and I assume breakpoints) is that it stops in a file that has the same contents and name as the actual function file, but it's not the same file (?!).

Note that the file with the debugger doesn't show Git diffs nor breadcrumbs.

Screenshot 2023-07-06 at 11 33 07 Screenshot 2023-07-06 at 11 33 01

I'm using Turborepo so that might add other issues into the mix. I think it would be worth having a sample Next.js repo with clear instructions on how to debug the code.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Aug 5, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.