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

"Debug: Attach to Node Process" + sourceMapPathOverrides #94300

Closed
borekb opened this issue Apr 2, 2020 · 12 comments
Closed

"Debug: Attach to Node Process" + sourceMapPathOverrides #94300

borekb opened this issue Apr 2, 2020 · 12 comments
Assignees
Labels
debug Debug viewlet, configurations, breakpoints, adapter issues feature-request Request for new features or functionality verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@borekb
Copy link

borekb commented Apr 2, 2020

Debug: Attach to Node Process is an awesome feature – no need to update npm scripts with --inspect and, most of the time, no need to create launch.json configs.

However, I'm in a situation where I'm trying to debug a Node.js project in a subfolder in my workspace. So in a console, I do:

cd subfolder
yarn dev

VSCode can successfully attach to the process but all paths are broken – the debugger sends paths like src/server.ts but VSCode should interpret this as subfolder/src/server.ts.

If I started the process via launch.json (which I generally don't like too much – it duplicates a few things from package.json, not all team members are using VSCode, etc.), this would work:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Example",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--inspect", "node_modules/.bin/next", "dev"],
      "port": 9229,
      "cwd": "${workspaceFolder}/subfolder",
      "sourceMapPathOverrides": {
        "webpack:///./*": "${workspaceRoot}/subfolder/*",
      }
    }
  ]
}

I just learned today about sourceMapPathOverrides and it makes VSCode work, however, I'd really much prefer to be able to do Debug: Attach to Node Process and still somehow tell VSCode to apply a custom mapping.

@weinand weinand assigned roblourens and connor4312 and unassigned weinand and isidorn Apr 2, 2020
@weinand weinand added the debug Debug viewlet, configurations, breakpoints, adapter issues label Apr 2, 2020
@connor4312
Copy link
Member

connor4312 commented Apr 3, 2020

In tomorrow's nightly of our new debugger (when installed, it will register a command Debug: Attach to Node Process (preview)) you'll be able to add defaults in your user or workspace settings, like so:

"debug.javascript.pickAndAttachOptions": {
  "sourceMapPathOverrides": {
    "webpack:///./*": "${workspaceRoot}/subfolder/*",
  }
}

Let us know if you run into any more issues!

(Versifiers: easiest way to test is add this option with trace: true and verify that the debug adapter prints "Verbose logs are written to: ..." in the debug console)

@connor4312 connor4312 added this to the April 2020 milestone Apr 3, 2020
@connor4312 connor4312 added the feature-request Request for new features or functionality label Apr 3, 2020
connor4312 added a commit to microsoft/vscode-js-debug that referenced this issue Apr 3, 2020
@borekb
Copy link
Author

borekb commented Apr 3, 2020

Oh wow, I thought I'm in some edge case and a few hours later, this issue is fixed 😄.

Workspace setting sounds great, and what you suggest in vercel/next.js#10807 (comment), i.e., being able to detect path mappings automatically with the help of lsof, sounds even better. Is there already an open issue for it?

@borekb
Copy link
Author

borekb commented Apr 3, 2020

@connor4312 Actually one point to the solution mentioned above:

Our monorepo has several Next.js projects in subfolders, like this:

- <workspace root>
    - nextjs-app-1
    - nextjs-app-2
    - nextjs-app-3

So having a single path mapping probably will help only partially – in some README, I'll probably need to suggest that the person debugging should update .vscode/settings.json temporarily (making the file modified as far as Git is concerned, which is a bit annoying).

Again, if lsof could solve that, that would be great. Otherwise, I'm not sure that there's much to do about that, maybe some sort of "interactive Attach to Node Process" that would ask for a real folder path via quick pick UI?

@connor4312
Copy link
Member

connor4312 commented Apr 3, 2020

We already use lsof when it's available 🙂 But right now it's only used when there's 0 or >= 2 workspace folders open--if there's exactly one open folder, we use that as the process cwd. As of microsoft/vscode-js-debug#326, for normal launches we set the cwd based on the nearest package.json-containing folder. Maybe we can adjust our attachment algorithm to do the same--if lsof is available, use the nearest package.json-containing folder inside any workspace folder. For Node.js, sourcemap resolution happens relative to the working directory, so that should work here, I think.

microsoft/vscode-js-debug#413, should get to it early next week

@borekb
Copy link
Author

borekb commented Apr 3, 2020

For normal launches we set the cwd based on the nearest package.json-containing folder. Maybe we can adjust our attachment algorithm to do the same.

That sounds great indeed.

If I understand correctly, the attach would detect nextjs-app-1/package.json, set cwd to nextjs-app-1 and all paths (which are currently relative to nextjs-app-1, not our workspace root) would suddenly work.

@connor4312
Copy link
Member

Yep, that's right.

@connor4312 connor4312 added the verification-needed Verification of issue is requested label Apr 27, 2020
@connor4312
Copy link
Member

To verify:

  1. Add the setting "debug.javascript.pickAndAttachOptions": { "trace": true }
  2. Run node --inspect in some terminal
  3. Run the command Attach to Node.js Process (preview) and pick that process
  4. Verify that the location debug log file is printed to the debug console (i.e. that the trace setting applied to attachment)

@JacksonKearl
Copy link
Contributor

image
Followed the steps but not seeing the log?

@connor4312
Copy link
Member

connor4312 commented Apr 30, 2020

@JacksonKearl the log should be in the "debug console" view, rather than the terminal.

@JacksonKearl
Copy link
Contributor

JacksonKearl commented Apr 30, 2020

image

I'm seeing the attach create two debug consoles, Node.js Process and process, and the log is shown in process. Node.js Process is empty. Is this expected?

@connor4312
Copy link
Member

I think that's intended behavior (cc @isidorn) so that you don't lose previous logs if you run a new launch config. (I see the same thing, but with an "Extension Host" entry since that happens to be the last thing I debugged).

@JacksonKearl JacksonKearl added the verified Verification succeeded label Apr 30, 2020
@isidorn
Copy link
Contributor

isidorn commented May 4, 2020

Yes this is designed behavior.

@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
debug Debug viewlet, configurations, breakpoints, adapter issues feature-request Request for new features or functionality verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants