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

skaffold debug: support npm-nodemon #2170

Closed
briandealwis opened this issue May 24, 2019 · 5 comments · Fixed by #4086
Closed

skaffold debug: support npm-nodemon #2170

briandealwis opened this issue May 24, 2019 · 5 comments · Fixed by #4086
Assignees
Labels

Comments

@briandealwis
Copy link
Member

#2141 has one caveat:

Note: npm run-scripts that use nodemon (a node-based script) instead of node will not work as nodemon's node will interpret the NODE_OPTIONS and configure itself for debugging.

This npm-nodemon style of launch seems useful, but it's unclear if nodemon makes sense with use with a debugger as DevTools seems to be able to upload and install file changes on the server.

If we do need to support npm invoking nodemon, I think we'll need to take a different approach where we would wrap node with a script that would determine whether it is launching an application script or a library script from node_modules such as nodemon. If a library script, then it should not run with --inspect and somehow defer it to a child node.

@briandealwis
Copy link
Member Author

I've attached a proof-of-concept node-wrapper script here that propagates an --inspect-style argument to a child node that invokes an application script, skipping intermediate node executions of library scripts like nodemon. The --inspect argument is propagated via a NODE_DEBUG environment variable.

This node wrapper parses the command-line arguments and NODE_OPTIONS (set by npm --node-options="--inspect" ...) for an --inspect argument and the script to be executed.

  • if the script is an application script then we invoke the real node with the --inspect argument as conveyed through NODE_DEBUG (if any)
  • if invoked with --inspect argument then set NODE_DEBUG to the value and strip it from the command-line
  • if the provided script is .../node_modules/.../nodemon then we run nodemon $NODE_DEBUG
    • we need to do this as nodemon --inspect will ensure that it uses spawn to invoke the child node, which will pick up our wrapped node. Otherwise it uses fork to launch the actual application script file directly, which circumvents the use of the node wrapper.
  • Otherwise the provided script is in node_modules and we invoke the real node with the provided script

(I'm pretty sure this script does not properly handle arguments with spaces; I wanted to avoid assuming bash.)

@briandealwis
Copy link
Member Author

briandealwis commented Jul 23, 2019

This affects Skaffold's own examples/nodejs.

There are two issues here, from what I can see:

  1. how do we prevent nodemon from interpreting the NODE_OPTIONS and instead pass it on to the application's node? (addressed above with the node wrapper script?)
  2. does nodemon make sense with debugging?

To expand on the latter, nodemon has the same problem during debugging as skaffold dev's triggering a redeploy-on-change: a change leads to the debug session being terminated, so you lose your debugging context. We disable Skaffold's redeploy-on-change during skaffold debug.

So perhaps we should provide a nodemon wrapper that just disables the file-watching and invokes node directly. This would prevent file-sync from having an effect, though the devtools protocol already seems to support installing changes.

Or could we make nodemon debugger-aware and disable it file-watching during a debug session?

@briandealwis
Copy link
Member Author

Another thing we could do is look for a package.json file and:

  1. Examine the package.json to see if it has a special run-script for debugging and use it if found. I came across one example that uses a "start:inspect": "nodemon --inspect src/index.js".
  2. Otherwise throw an error if it uses nodemon with pointers how to fix.

@briandealwis
Copy link
Member Author

  • Finding the package.json from the local disk is non-trivial; it's entirely possible for a Dockerfile to copy it from an arbitrary location.
  • Finding the package.json from the remote image is non-trivial but more tractable as it's likely in the WORKDIR.

The node wrapper script may be the more robust implementation.

@briandealwis briandealwis added the priority/p2 May take a couple of releases label Aug 21, 2019
@briandealwis briandealwis self-assigned this Apr 8, 2020
@tstromberg tstromberg changed the title Should skaffold debug support npm-nodemon? skaffold debug: support npm-nodemon Apr 21, 2020
@tstromberg
Copy link
Contributor

Making a non-question since there seems to be a PR toward implementing this.

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

Successfully merging a pull request may close this issue.

2 participants