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

Restarting debugger after file change doesn't work #86

Closed
MagicLegend opened this issue May 6, 2020 · 16 comments
Closed

Restarting debugger after file change doesn't work #86

MagicLegend opened this issue May 6, 2020 · 16 comments

Comments

@MagicLegend
Copy link

Hi,

I have the following configurations:

Package.json script:

"start-watch": "NODE_ENV=development ./node_modules/.bin/tsc-watch --onSuccess \"node --inspect bin/server/src/server.js\" --onFailure \"notify-send -a Node 'Transpilation error!' 'Check the task terminal for results...'\" -p ../tsconfig.dev.json"

Launch.json:

{
    "type": "node",
    "request": "launch",
    "name": "Launch via NPM",
    "runtimeExecutable": "npm",
    "runtimeArgs": [
        "run-script",
        "start-watch"
    ],
    "env": {
        "NODE_ENV": "development"
    },
    "port": 9229,
    "skipFiles": [
        "<node_internals>/**"
    ],
    "outputCapture": "std", // Necessary to be able to see logging in the debug console
    "restart": true,
},
{
    "type": "node",
    "request": "attach",
    "name": "Attach to running node",
    "port": 9229,
    "restart": true,
}

I was hoping that using the "restart": true config as described here in the VSCode docs would work with tsc-watch, but that doesn't appear to be the case. It looks like the watcher rebuilds, but not restart the node server. I can observe in the logs that the watcher started the transpilation; since it outputs the start and finish message.

Am I misconfiguring something here, or is this scenario (restarting and re-attaching the debugger from launch.json) not supported? Note that manually invoking the start-watch and then attaching using the second launch configuration does work across restarts. It's just when I invoke the NPM script via launch.json that it doesn't work.

Thank you for your time.

@gilamran
Copy link
Owner

gilamran commented May 6, 2020

That's very interesting.
I never thought about using tsc-watch in debug session, that's very cool!

I was reading about this, and found that it might be timing out:

Tip: In case of syntax errors, nodemon will not be able to start Node.js successfully until the error has been fixed. In this case, VS Code will continue trying to attach to Node.js but eventually give up (after 10 seconds). To avoid this, you can increase the timeout by adding a timeout attribute with a larger value (in milliseconds).

Can you verify that this works fast enough, or just increase the timeout.

Maybe you can create a very simple repo. to reproduce your issue, so I can see it happens?

Thanks

@MagicLegend
Copy link
Author

MagicLegend commented May 6, 2020

Can you verify that this works fast enough, or just increase the timeout.

I am pretty sure it's fast enough. Transpilation takes less than a second; and is nearly instant. I have updated my testing project to show issues for this issue: https://github.com/MagicLegend/ts-testing-project

Sometimes I also get an error that the 9229 port is already in use (Starting inspector on 127.0.0.1:9229 failed: address already in use); which would indicate to me that the previous node process hasn't exited yet. Could it be that the transpilation is so fast that it tries to restart it while the previous one is still running? If so, that might be worth an issue on it's own. It also sometimes throws an error via the --onFailure hook; even though according to VSCode nothing is running. I have not figured out how to reproduce this reliably; if I do I'll let you know.


When patching up the testing project, I actually managed to get an error logged instead of a stuck background process:

/home/magiclegend/.nvm/versions/node/v12.16.1/bin/npm run-script start-watch

> ts-testing-project@1.0.0 start-watch /home/magiclegend/Documents/gitrepos/ts-testing-project
> tsc-watch --onSuccess "node --inspect bin/index.js" --onFailure "notify-send -a Node 'Transpilation error!' 'Check the task terminal for results...'" -p tsconfig.dev.json
�c[11:56:49 AM] Starting compilation in watch mode...

[11:56:51 AM] Found 0 errors. Watching for file changes.

Debugger listening on ws://127.0.0.1:9229/95297f83-6505-435f-a430-7c3c2fb29b60
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Wed, 06 May 2020 09:56:52 GMT sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators at node_modules/sequelize/lib/sequelize.js:245:13
Executing (default): DROP TABLE IF EXISTS `TP`;
Executing (default): DROP TABLE IF EXISTS `p`;
Executing (default): DROP TABLE IF EXISTS `A`;
Executing (default): DROP TABLE IF EXISTS `T`;
Executing (default): DROP TABLE IF EXISTS `T`;
Executing (default): CREATE TABLE IF NOT EXISTS `T` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `defaultCommand` VARCHAR(255), `requiredArguments` VARCHAR(255), `description` VARCHAR(255), PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `T` FROM `testdb`
Executing (default): DROP TABLE IF EXISTS `A`;
Executing (default): CREATE TABLE IF NOT EXISTS `A` (`id` INTEGER NOT NULL auto_increment , `a` VARCHAR(255), `b` VARCHAR(255), `tId` INTEGER, `version` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (`id`), FOREIGN KEY (`tId`) REFERENCES `T` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `A` FROM `testdb`
Executing (default): DROP TABLE IF EXISTS `p`;
Executing (default): CREATE TABLE IF NOT EXISTS `p` (`id` INTEGER(10) NOT NULL auto_increment , `a` INTEGER(10), `t_id` INTEGER(10), `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, `version` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `p` FROM `testdb`
Executing (default): DROP TABLE IF EXISTS `TP`;
Executing (default): CREATE TABLE IF NOT EXISTS `TP` (`tId` INTEGER , `pId` INTEGER(10) , PRIMARY KEY (`tId`, `pId`), FOREIGN KEY (`tId`) REFERENCES `T` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (`pId`) REFERENCES `p` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `TP` FROM `testdb`
Server running on port 3001
Unhandled error for request GET /movies/1: Error: Model not initialized: "Movie" needs to be added to a Sequelize instance before "findByPk" can be called.
    at new ModelNotInitializedError (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize-typescript/lib/models/errors/ModelNotInitializedError.js:18:16)
    at checkInitialization (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize-typescript/lib/models/BaseModel.js:64:23)
    at Function._target.<computed> [as findByPk] (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize-typescript/lib/models/BaseModel.js:34:21)
    at /home/magiclegend/Documents/gitrepos/ts-testing-project/bin/lib/routes/movies.js:35:45
    at Generator.next (<anonymous>)
    at /home/magiclegend/Documents/gitrepos/ts-testing-project/bin/lib/routes/movies.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/home/magiclegend/Documents/gitrepos/ts-testing-project/bin/lib/routes/movies.js:4:12)
    at /home/magiclegend/Documents/gitrepos/ts-testing-project/bin/lib/routes/movies.js:33:40
    at Layer.handle [as handle_request] (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/layer.js:95:5)
    at /home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:281:22
    at param (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:354:14)
    at param (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:410:3)
    at next (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:174:3)
    at router (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/express/lib/router/index.js:317:13)
�c[11:57:22 AM] File change detected. Starting incremental compilation...

[11:57:23 AM] Found 0 errors. Watching for file changes.

Error evaluating `process.pid`: Cannot find context with specified id
Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:154:25)
    at writeGeneric (internal/stream_base_commons.js:145:3)
    at Socket._writeGeneric (net.js:780:11)
    at Socket._write (net.js:792:8)
    at doWrite (_stream_writable.js:441:12)
    at writeOrBuffer (_stream_writable.js:425:5)
    at Socket.Writable.write (_stream_writable.js:316:11)
    at Function.log (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/depd/index.js:244:18)
    at Logger.deprecate [as depd] (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/depd/index.js:116:9)
    at Logger.deprecate (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize/lib/utils/logger.js:28:10)
    at new Sequelize (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize/lib/sequelize.js:245:13)
    at new Sequelize (/home/magiclegend/Documents/gitrepos/ts-testing-project/node_modules/sequelize-typescript/lib/models/v4/Sequelize.js:24:13)
    at Object.<anonymous> (/home/magiclegend/Documents/gitrepos/ts-testing-project/src/lib/seq.ts:13:26)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/home/magiclegend/Documents/gitrepos/ts-testing-project/src/lib/server.ts:3:1)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
Emitted 'error' event on Socket instance at:
    at errorOrDestroy (internal/streams/destroy.js:108:12)
    at onwriteError (_stream_writable.js:456:5)
    at onwrite (_stream_writable.js:483:5)
    at internal/streams/destroy.js:50:7
    at Socket.dummyDestroy [as _destroy] (internal/bootstrap/switches/is_main_thread.js:97:3)
    at Socket.destroy (internal/streams/destroy.js:38:8)
    at afterWriteDispatched (internal/stream_base_commons.js:154:17)
    at writeGeneric (internal/stream_base_commons.js:145:3)
    [... lines matching original stack trace ...]
    at Module.load (internal/modules/cjs/loader.js:1002:32)

This throws when the Launch via NPM launch config is used. I would expect you to be able to reproduce this. What is curious is that in my actual codebase it doesn't log anything.

@gilamran
Copy link
Owner

gilamran commented May 6, 2020

Add me as a contributor, I've simplified the repo.

@MagicLegend
Copy link
Author

Done. Please push your work to a different branch, so I can use the master branch for other issues.

@gilamran
Copy link
Owner

gilamran commented May 6, 2020

ERROR: Permission to MagicLegend/ts-testing-project.git denied to gilamran.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

@MagicLegend
Copy link
Author

@gilamran You should have an email from Github, you need to click on that link to accept the invite.

@gilamran
Copy link
Owner

gilamran commented May 6, 2020

Didn't get one...

@MagicLegend
Copy link
Author

@gilamran I have re-added you... I believe you are supposed to get an email from Github about this; which you have to click to accept. Otherwise forking my repo would also solve the issue.

image

@gilamran
Copy link
Owner

gilamran commented May 6, 2020

ok, found it it my github notifications
done: https://github.com/MagicLegend/ts-testing-project/tree/tsc-watch-test

@MagicLegend
Copy link
Author

@gilamran And are you able to reproduce the issues with the given config files?

@gilamran
Copy link
Owner

gilamran commented May 7, 2020

I was able to debug (breakpoints) and tsc-watch detected file change, compiled, BUT did not rerun the debugger. I'm checking!

Edit: My mistake, it rerun the debugger as expected, but VSCode did not connect to the new debugger instance, and just ended the debugger session

@gilamran
Copy link
Owner

gilamran commented May 7, 2020

I was also trying the same with nodemon and it doesn't work as well. only with attach (tsc-watch also works with attach)
Are you sure that it's possible to use restart with nodemon?

@MagicLegend
Copy link
Author

Are you sure that it's possible to use restart with nodemon?

I haven't tried nodemon personally; but if the VSCode docs say so; I would think it'd be compatible right? :-) Given somebody uses it in microsoft/vscode#95128 I would say that the functionality works.

My mistake, it rerun the debugger as expected, but VSCode did not connect to the new debugger instance, and just ended the debugger session

Is this something that should be fixed on VSCode's side, or on tsc-watch's side?

Relating to the refused port, it could also be related to microsoft/vscode#89819. Though that issue mentions it being logged repeatedly; which is not the case for me.

@MagicLegend
Copy link
Author

microsoft/vscode#87321 could also be related to the stuck node processes that keep running in the background. Though I don't know how much of tsc-watch is similar to nodemon in that aspect. You have a better idea of that I presume :)

@gilamran
Copy link
Owner

gilamran commented May 7, 2020

ok, I found the issue (Pushed to the repo)
You must use this: "console": "integratedTerminal" to be able to use restart...

@gilamran gilamran closed this as completed May 7, 2020
@MagicLegend
Copy link
Author

Hmm, I guess that the other flow I had (CTRL+SHIFT+P > Tasks: Run task > npm: start-dev-server) inferred the usage of the terminal instead of the debug one. Would make sense I guess. Perhaps it's worth making a note of this setup in the README of this repo; since I think it could be a useful usecase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants