next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Erratic debugging/breakpoint behavior with turbopack (and Cursor?)

Open bernatfortet opened this issue 7 months ago • 29 comments

Link to the code that reproduces this issue

https://github.com/bernatfortet/reproduction-app

To Reproduce

In the reproduction app I've changed:

  1. The script dev to use turbopack: "dev": "next dev --turbopack"
  2. a .vscode/launch.json file and add what the Guides: Debugging page mentions to use for debugging with next.js
  3. In src/app/page.tsx add a console log right below the Home function:
export default function Home() {
  console.log(`Add brakepoint in this line`)
  ...
  1. Add a breakpoint to the console log Image

  2. Refresh OR add a letter to the log and save.

Current vs. Expected behavior

Expected behavior: The breakpoint stops on the console log consistently on every refresh.

Behavior 1:

  • Breakpoint works well, I click continue and it's fine.
  • But if I change the text in the console it trigger behavior 2

Behavior 2:

  • After reloading
  • Breakpoint works well
  • But when I "Continue" executing the code Image
  • I'm sent to some cryptic part of the code Image

Behavior 3: Image

  • After the breakpoint has been set, refreshing the page doesn't trigger anything.
  • (is this because of caching?

Provide environment information

Node.js v23.3.0

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 23.3.0
  npm: 10.9.0
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 15.4.0-canary.43 // Latest available version is detected (15.4.0-canary.43).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

  • Something tells me it's something related to this: https://github.com/microsoft/vscode-js-debug/pull/2223/files
  • Or this: https://github.com/vercel/next.js/pull/76559/files#diff-bd5430ee7c51dc892a67b3f2829d1f5b6d223f0fd48b82322cfd45baf9f5e945
  • other context here: https://github.com/vercel/next.js/issues/73214#issuecomment-2883178209
  • this happens on canary as well ^15.4.0-canary.43
  • Cursor Version: 0.50.5
  • VSCode Version: 1.96.2

bernatfortet avatar May 20 '25 22:05 bernatfortet

bump?

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

bernatfortet avatar Jun 02 '25 18:06 bernatfortet

I guess I'm the only one dealing with this and it's a skill issue? That being said the repro above is as simple as it gets, so I assume everyone is getting it!

bernatfortet avatar Jun 27 '25 21:06 bernatfortet

Unfortunately I'm unable to reproduce the described behavior based on the steps. It consistently hits the console.log even as I edit it. Maybe it's been improved by https://github.com/microsoft/vscode-js-debug/pull/2223 though. I've asked the Cursor team if that has landed in Cursor already or not since the code isn't available.

timneutkens avatar Jul 01 '25 14:07 timneutkens

I am able to reproduce this in VSCode, but I'm not sure yet if it's Turbopack's, Next.js's or VSCode's fault.

mischnic avatar Jul 02 '25 18:07 mischnic

The fix for microsoft/vscode-js-debug#2223 was included in VS Code v1.101. However, Cursor v1.2.4 (latest at the time of writing) is still based on v1.99, so that fix isn't included yet.

If you are using VS Code, you can check your version with code -v in the terminal, and update if it’s below v1.101. My automatic update actually hadn't triggered either, so I had to manually update it. That solved it for me.

Andre9979 avatar Jul 22 '25 00:07 Andre9979

@Andre9979 any thoughts on what to do when using Cursor?

bernatfortet avatar Jul 22 '25 04:07 bernatfortet

@bernatfortet Can you try it by launching the following full-stack debug config with Firefox? Make sure you have the "Debugger for Firefox" extension in Cursor installed.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: Launch Client Debugger (Firefox)",
      "request": "launch",
      "type": "firefox",
      "reAttach": true,
      "url": "http://localhost:3000",
      "skipFiles": ["**/node_modules/**"],
      "pathMappings": [
        {
          "url": "turbopack:///[project]",
          "path": "${workspaceFolder}"
        }
      ]
    },
    {
      "name": "Next.js: Launch Server",
      "type": "node-terminal",
      "request": "launch",
      "command": "pnpm dev"
    },
    {
      "name": "Next.js: Attach Debugger to Server",
      "request": "attach",
      "port": 9230,
      "type": "node",
      "skipFiles": ["<node_internals>/**", "**/node_modules/**", "**.next**"],
      "sourceMapPathOverrides": {
        "turbopack:///[project]/*": "${workspaceFolder}/*"
      }
    },
  ],
  "compounds": [
    {
      "name": "Next.js : Launch Fullstack Debugger",
      "configurations": [
        "Next.js: Launch Client Debugger (Firefox)",
        "Next.js: Launch Server",
        "Next.js: Attach Debugger to Server"
      ],
      "stopAll": true
    }
  ]
}

I tried the full-stack debugger configuration from the Next.js docs, but ran into an issue: the debugger was also breaking inside source map files, which isn’t the expected behavior.

This was resolved by using a compound full-stack debugging setup instead (as shown above).

However, another issue occurred: when using Chrome or Edge for client-side debugging, the debugger would break on every line of JSX returned by a component. This also isn’t expected behavior, and it threw the following errors:

Could not read source map for file:///.../node_modules/.pnpm/next%4015.4.0-canary.83_%40babe_edd3cd349b87e217720fdc64e468ac48/node_modules/next/dist/compiled/scheduler-experimental/cjs/scheduler.development.js: ENOENT: no such file or directory, open '...\node_modules\.pnpm\[email protected]_@babe_edd3cd349b87e217720fdc64e468ac48\node_modules\next\dist\compiled\scheduler-experimental\cjs\" + sourceMap))'
Could not read source map for file:///.../_next/static/chunks/turbopack%3A/%5Bturbopack%5D/browser/runtime/dom/dev-backend-dom.ts: Unexpected token ' ', "      " is not valid JSON

I tried using Firefox for client-side debugging and everything worked as expected. You could still try Chrome or Edge in case it’s a windows-specific issue (I’m on windows), but either way, you’ll need to manually override the path mappings in your debug config as shown above (otherwise they won’t resolve correctly due to changes introduced in #76075) These manual overrides should become unnecessary once most vscode based IDEs (like cursor) update their fork to v1.101+, as the path mappings will be handled by default then.

This workaround worked well for me.

Andre9979 avatar Jul 22 '25 16:07 Andre9979

For anyone trying the run middleware debugger with breakpoints heres a config that might help since I have been searching for hours for a solution and none of them worked. My project is a monorepo using turborepo running with dev turbo. I am also using Cursor. For the below json, change the command to what will run your Next.js app. Heres the launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: Debug with Auto-Attach",
      "type": "node-terminal",
      "command": "pnpm dev --filter web",
      "request": "launch",
      "skipFiles": ["<node_internals>/**", "**/node_modules/**", "**.next**"],
      "env": {
        "NODE_OPTIONS": "--inspect=0.0.0.0:9229"
      },
      "sourceMapPathOverrides": {
        "turbopack:///[project]/*": "${workspaceFolder}/*"
      }
    }
  ]
}

Node.js v22.14.0 Next.js v15.4.1 Machine: mac m4 pro

hammadmaqsood24 avatar Jul 28 '25 14:07 hammadmaqsood24

Thanks @Andre9979 for the suggestion.

These manual overrides should become unnecessary once most vscode based IDEs (like cursor) update their fork to v1.101+, as the path mappings will be handled by default then.

I just tried running my project with VSCode (Version: 1.103.0) and I'm still running into the same issue. Below is the launch configuration I'm using in this VSCode version. I wonder if anything needs to change here.

    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "bun run dev --turbo"
    }

I'm also surprised that I'm the only one running into this issue. Maybe not many people use these kind of debugging experience? Or is this an isolated issue for me only?

bernatfortet avatar Aug 13 '25 17:08 bernatfortet

Man... this is so sad. I'm considering moving away from next.js because it's so hard to work if I can't debug properly. It's a killer bug...

I still can't believe others are not getting this and are getting really frustrated.

bernatfortet avatar Aug 27 '25 16:08 bernatfortet

Unfortunately I'm unable to reproduce the described behavior based on the steps. It consistently hits the console.log even as I edit it. Maybe it's been improved by microsoft/vscode-js-debug#2223 though. I've asked the Cursor team if that has landed in Cursor already or not since the code isn't available.

@timneutkens can you tell me which IDE were you usuing when you tried to repro? I'd like to do it on my end as well.

bernatfortet avatar Aug 27 '25 16:08 bernatfortet

@bernatfortet the snippet you posted only launches the dev server, no debugger is attached. Could you share the full launch.json? Also, have you tried the exact config I shared earlier here?

Can you also try running it with node instead of bun? It could be a bun-specific issue.

Andre9979 avatar Aug 27 '25 18:08 Andre9979

@Andre9979 I only want to launch the dev server. I debug the client side via chrome dev tools.

Your config doesn't have --turbo. Did you try with Turbo? I believe that's the main issues. That turbpack messes up the source maps.

Regarding bun, it was not working before when I was using yarn.

In any case here's my full config. But then again, I only use the debug server-side. It worked fine for a year and then it stopped.

  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "bun run dev --turbo",
    },
    {
      "name": "Next.js: debug client-side",
      "type": "chrome",
      "request": "attach",
      "port": 9222,
      "urlFilter": "https://rs.local/*",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "Next.js: debug full stack",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/.bin/next",
      "runtimeArgs": ["--inspect"],
      "skipFiles": ["<node_internals>/**"],
      "serverReadyAction": {
        "action": "debugWithEdge",
        "killOnServerStop": true,
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "webRoot": "${workspaceFolder}"
      }
    },
    {
      "name": "Trigger.dev: Dev",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "bun",
      "runtimeArgs": ["x", "trigger.dev@latest", "dev"],
      "skipFiles": ["<node_internals>/**"],
      "sourceMaps": true
    }
  ],
  "compounds": [
    {
      "name": "Next.js: Full Debug",
      "configurations": ["Next.js: debug server-side", "Next.js: debug client-side"]
    }
  ]
}

bernatfortet avatar Sep 12 '25 22:09 bernatfortet

Unfortunately I'm unable to reproduce the described behavior based on the steps. It consistently hits the console.log even as I edit it. Maybe it's been improved by microsoft/vscode-js-debug#2223 though. I've asked the Cursor team if that has landed in Cursor already or not since the code isn't available.

@timneutkens were you able to try the issue while using Cursor? I'm not sure what's the Cursor vs VSCode usage, but I think it's become a pretty important/popular tool, right?

bernatfortet avatar Sep 12 '25 23:09 bernatfortet

Just echoing that I am facing the same pain while using cursor and have found this thread :(

evanreilly avatar Sep 13 '25 02:09 evanreilly

Hi @evanreilly do you have the same configuration? Can you add your cursor version? Do you use turbo?

I'd like to see if we can help @timneutkens and others to see if they can help us with this. It's incredibly painful to not be able to debug properly.

bernatfortet avatar Sep 13 '25 20:09 bernatfortet

About Cursor:

Version: 1.5.11 VSCode Version: 1.99.3 Commit: 2f2737de9aa376933d975ae30290447c910fdf40 Date: 2025-09-05T03:48:32.332Z (1 wk ago) Electron: 34.5.8 Chromium: 132.0.6834.210 Node.js: 20.19.1 V8: 13.2.152.41-electron.0 OS: Darwin arm64 24.6.0

I am running an SST nextJS application "dev:debug": "NODE_OPTIONS='--inspect=0.0.0.0:9229' next dev --turbopack -p 3000",

other configs I have tried: "dev:debug": "NODE_OPTIONS='--inspect' next dev -p 3000",

a bunch of launch.json hacks that didn't work at all.

evanreilly avatar Sep 13 '25 20:09 evanreilly

I think we need to just wait for cursor to update to latest VScode :(

evanreilly avatar Sep 13 '25 20:09 evanreilly

@bernatfortet Yes, I use Turbo. You typically add the --turbo flag to your dev script in package.json.

Could you try adding the following to your Next.js: debug client-side configuration and then run your Next.js: Full Debug config?

"skipFiles": [
  "<node_internals>/**",
  "**/node_modules/**",
  "**/.next/**"
]

This might resolve the issue you described under Behaviour 2. There's a lot of external code being pulled in during debugging, and excluding these paths helps prevent stepping too deep into irrelevant internals. That’s usually why you end up in the “cryptic code” you mentioned.

Edit: If it worked for you previously, checking earlier canary versions until you find a working one is an excellent way to pinpoint where the issue was introduced.

Andre9979 avatar Sep 15 '25 21:09 Andre9979

@Andre9979 I tired but unfortunately the skipFiles don't make a difference.

When that started happening I tried to go back into different canary versions. I actually tried quite hard but I got nowhere.

bernatfortet avatar Sep 18 '25 16:09 bernatfortet

Did somebody find a solution to this? I also have this issue with cursor.

Edit by maintainer bot: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!

siimsams avatar Oct 24 '25 07:10 siimsams

@siimsams any chance you could try to create another repo with instructions on how to reproduce? Maybe you have more luck than me in giving Tim a way to reproduce the issue.

bernatfortet avatar Nov 04 '25 14:11 bernatfortet

I have tried the configuration file of @Andre9979. It's work for me but need some modify. I have change the pathMappings for firefox client debugger.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js: Launch Client Debugger (Firefox)",
      "request": "launch",
      "type": "firefox",
      "reAttach": true,
      "url": "http://localhost:3000",
      "skipFiles": [
        "**/node_modules/**"
      ],
      "pathMappings": [
        {
          "url": "file:///",
          "path": "${workspaceFolder}"
        }
      ]
    },
    {
      "name": "Next.js: Launch Server",
      "type": "node-terminal",
      "request": "launch",
      "command": "pnpm dev"
    },
    {
      "name": "Next.js: Attach Debugger to Server",
      "request": "attach",
      "port": 9230,
      "type": "node",
      "skipFiles": [
        "<node_internals>/**",
        "**/node_modules/**",
        "**.next**"
      ],
      "sourceMapPathOverrides": {
        "turbopack:///[project]/*": "${workspaceFolder}/*"
      }
    }
  ],
  "compounds": [
    {
      "name": "Next.js : Launch Fullstack Debugger",
      "configurations": [
        "Next.js: Launch Client Debugger (Firefox)",
        "Next.js: Launch Server",
        "Next.js: Attach Debugger to Server"
      ],
      "stopAll": true
    }
  ]
}

Enviroment

  • vscode 1.105.1 with devcontainer on mbp
  • next 15.5.2
  • Debugger for Firefox extension 2.15.0
  • Firefox 144.0.2 (aarch64)

Other problem

When I set the breakpoints in vscode on client/server components, I still got a tip of (Debugger for Firefox) extension that ask me to add a pathmapping, just ignore.

Image

trganda avatar Nov 12 '25 01:11 trganda

In 16.0.3 we've switched from turbopack:// urls in the source paths of the sourcemap to using relative paths. This should make it so that the sourcemaps are automatically found by the tooling without additional config (i.e. vscode / cursor). Can you give that a try? Thanks!

timneutkens avatar Nov 18 '25 15:11 timneutkens

I've tried to upgrade next on any new version, unfortunately I've tried two times for 16 and when I deploy to vercel it tries to build and gets stuck on "creating optimized build" until timeout (45min) On local vercel build worked just fine. I wanted to create a new issues but I had no way to repro 😞. I've also looked a few times for other people with that issues but no luck.

Super excited to upgrade and get the turbopack issue fixed!

bernatfortet avatar Nov 18 '25 17:11 bernatfortet

In 16.0.3 we've switched from turbopack:// urls in the source paths of the sourcemap to using relative paths. This should make it so that the sourcemaps are automatically found by the tooling without additional config (i.e. vscode / cursor). Can you give that a try? Thanks!

I am using version 16.0.3 and those turbopack://[project] and similar urls are still there. Do I need to change something else to have it to generate relative paths?

My debugger only hits the breakpoints in middleware.ts, anything else is unbound.

solmersa avatar Nov 19 '25 02:11 solmersa

Any update on getting this to work?

I'm running next 15.5.7 in a monorepo, with some shared packages AS WELL as a TS alias to another folder at the same directory as my next js app folder.

Webpack debugging works perfectly. With turbopack enabled, it hits breakpoints, but step over/step into results in garbage - vs code often thinks I'm debugging webassembly.

I'm hitting a point in my javascript "career" where all I want is to debug and mock ES modules and it like, works without days of headache. Starting to look back fondly at C# 👀

MDerman avatar Dec 07 '25 20:12 MDerman

These are my configs which work for me on a M1 mac in a turborepo monorepo. I use cursor.

  • Node v22.16.0
  • Next.js v15.4.8
  • Turbo v2.4.2
  • TS v5.7.3

I hope this helps.

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Attach to API (already running)",
        "type": "node",
        "request": "attach",
        "cwd": "${workspaceFolder}/apps/api",
        "port": 9229,
        "restart": true,
        "skipFiles": ["<node_internals>/**"],
        "sourceMaps": true
      },
      {
        "name": "Next.js: debug server-side",
        "port": 9230,
        "request": "attach",
        "skipFiles": ["<node_internals>/**", "**/node_modules/**", "**.next**"],
        "type": "node",
        "sourceMaps": true,
        "cwd": "${workspaceFolder}/apps/manager",
        "sourceMapPathOverrides": {
           "/turbopack/[project]/*": "${webRoot}/*"
        },
        "autoAttachChildProcesses": false
      },
      {
        "type": "chrome",
        "request": "attach",
        "name": "Next.js: debug client-side",
        "skipFiles": ["<node_internals>/**", "**/node_modules/**", "**.next**"],
        "url": "http://localhost:9230",
        "webRoot": "${workspaceFolder}",
        "sourceMaps": true,
        "sourceMapPathOverrides": {
           "/turbopack/[project]/*": "${webRoot}/*"
        }
      }
    ],
    "compounds": [
      {
        "name": "Next.js: debug full stack",
        "configurations": ["Next.js: debug client-side", "Next.js: debug server-side"],
        "stopAll": true
      }
    ]
  }
  

siimsams avatar Dec 08 '25 11:12 siimsams

/": "${webRoot}/" }, Thanks Siim! I already had the same sourceMapPathOverrides, but I decided to simply launch next and attach debugger separately as you're doing and that worked! No more weird behavious it seems! Thanks man!

MDerman avatar Dec 11 '25 07:12 MDerman