ts-babel-node icon indicating copy to clipboard operation
ts-babel-node copied to clipboard

Unable to debug (in Webstorm) with ts-babel-node

Open dan-j opened this issue 8 years ago • 2 comments

If I run ts-node I am able to use the Webstorm debugger however with ts-babel-node I'm not.

I'm not sure what the issue is other than it's when registerBabel() is called. I thought it may have been an issue with how source-maps are configured but I've tried commenting the overrideSourceMaps() function but that doesn't help.

In webstorm, I get the following output for ts-babel-node:

/Users/Dan/.nvm/versions/node/v8.2.0/bin/node /Users/Dan/.nvm/versions/node/v8.2.0/lib/node_modules/npm/bin/npm-cli.js run ts-babel-node --scripts-prepend-node-path=auto

> [email protected] ts-babel-node /Users/Dan/Documents/git_repos/ts-babel-node-debugger-issue
> ts-babel-node $NODE_DEBUG_OPTION index.ts

parent directory of here is: /Users/Dan/Documents/git_repos

Process finished with exit code 0

For ts-node, where the debugger works:

/Users/Dan/.nvm/versions/node/v8.2.0/bin/node /Users/Dan/.nvm/versions/node/v8.2.0/lib/node_modules/npm/bin/npm-cli.js run ts-node --scripts-prepend-node-path=auto

> [email protected] ts-node /Users/Dan/Documents/git_repos/ts-babel-node-debugger-issue
> ts-node $NODE_DEBUG_OPTION index.ts

Debugger listening on ws://127.0.0.1:54769/aa160606-b3ee-4c63-9972-9fab24593856
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
parent directory of here is: /Users/Dan/Documents/git_repos
Waiting for the debugger to disconnect...
Debugger listening on ws://127.0.0.1:54769/aa160606-b3ee-4c63-9972-9fab24593856
For help see https://nodejs.org/en/docs/inspector

Process finished with exit code 0

I've created a repo with details to reproduce, https://github.com/dan-j/ts-babel-node-debugger-issue. Note I'm using a forked version of ts-babel-node (which has a pull request open at the moment https://github.com/danielmoore/ts-babel-node/pull/16) but I've confirmed the debugger doesn't work in version 1.1.0 either (just 1.1.0 doesn't work with [email protected] and [email protected])

I've also tried with node v7.10.1, to no avail.

dan-j avatar Jul 21 '17 12:07 dan-j

I've found the problem!

Essentially source maps must be emitted inline for WebStorm to pick it up and act on breakpoints accordingly. Attaching them as comments made everything work, however this only solved the problem when using the require hook (node -r ts-babel-node/register ...) or using require('ts-babel-node/register).

In order to also have this working using the ts-babel-node executable, it needs to require ts-node/dist/bin, not ts-node/dist/_bin.

#! /usr/bin/env node
'use strict';

require('..').registerBabel();
< require('ts-node/dist/_bin');
---
> require('ts-node/dist/bin');

I think (I've only quickly looked into why) this is because bin.js spawns a new process with the correct node arguments, i.e.

node [nodeArgs] _bin.js [tsNodeArgs] [script] [scriptArgs]

Whereas for _bin.js all arguments are treated as ts-node arguments, i.e.

node _bin.js [nonScriptArgs] [script] [scriptArgs]

By using _bin any node arguments, for instance --debug, --inspect etc., are passed as arguments to bin.js so the node process can't attach the debugger.

I've not had much response from issues I've raised here (although I've been full-on for the past 2-3 days and appreciate you also may be busy), so I'm going to continue with my forked version for now. I'll try and write some tests and create a proper PR, I'll make a point of doing it sooner rather than later if I get a response

dan-j avatar Jul 21 '17 16:07 dan-j

Ignore half of that last comment. You can't use bin.js because it spawns a new process which means this module hasn't registered the .ts or .tsx hooks to require.extensions[]. I thought it worked because my code didn't actually require babel so it looked like it worked, but it wasn't actually using this module 😞

There may just have to be a limitation that for debugging, you must use node's -r hook or call require('ts-babel-node/register) yourself.

dan-j avatar Jul 21 '17 18:07 dan-j