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

RFC: npm debug command #618

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

RFC: npm debug command #618

wants to merge 13 commits into from

Conversation

about-code
Copy link

@about-code about-code commented Jul 29, 2022

This RFC proposes an npm debug command to simplify debugging npm packages.

Prior discussions at npm/feedback#585

@darcyclarke darcyclarke added the Agenda will be discussed at the Open RFC call label Aug 3, 2022
Comment on lines 61 to 64
1. GIVEN a `bin` property is present in `package.json`
- THEN launch a debug session for the script referred to by the `bin` property using `node --inspect-brk <bin-script> <script-argv>`. END.
- ELSE continue
1. GIVEN a `main` property is present in `package.json`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about packages with both a bin and a main that want to debug both?

(i realize it's a bad practice now to have both in the same package, but there's lots of older packages that do)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the following options:

  1. Keep implict priority and order and execute bin. Require a --main switch to allow for debugging main.
  2. Keep implicit priority but change order and execute main. Require a --bin switch to allow for debugging bin.
  3. Consider it to be a conflict and exit with an NPM ERR. Require a --main or --bin argument in npm debugs argv

Consequences

  1. would require us to have a --main and --bin argument, since a --bin argument seems to be necessary for the case of multiple bin executables, anyways.
  2. would get us around a --main argument and just require a --bin CLI argument. The latter seems to be necessary for the case of multiple executables (update on this, follows)
  3. would ask for an explicit decision from the user.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update 70c0a01 decides for 1 and would allow users to be explicit about whether to debug main or bin.

npm debug --main
npm debug --bin

Given they provided both --bin and --main or none at all then --bin would take precedence:

npm debug
npm debug --main --bin

... executes a packages bin script if a package.json has a bin and a main script.

@ljharb
Copy link
Contributor

ljharb commented Aug 3, 2022

I believe that setting NODE_OPTIONS='--inspect-brk' would make debugging Just Work with any node-based command you wanted to run. Does that work for you?

@about-code
Copy link
Author

about-code commented Aug 6, 2022

I believe that setting NODE_OPTIONS='--inspect-brk' would make debugging Just Work with any node-based command you wanted to run. Does that work for you?

From a purely technical point of view, yes, it works (similar to how node --inspect-brk <some-path> would do it, as well). Just note: this RFC is less about making debugging just work but should be perceived as being about developer experience and how to make debugging particular packages more approachable using the npm command and its implict knowledge of packages. Edit: Your proposal might be applicable internally in an implementation of npm debug

From a usability perspective I consider NODE_OPTIONS or a run-script uncomfortable for users for two reasons:

  • they'd need to set up such run-scripts for every new project they start, even though debugging is a recurrent development task quite similar to init, start, test or publish a package.
  • in an npm workspace with multiple CLI packages they'd need a separate run-script for every such package in the workspace

Possible Use Cases

1: Authoring an npm package with a CLI

Instead of crafting a command-line or run-script with

NODE_OPTIONS='--inspect-brk' node ./bin/foo.js

this RFC proposes to provide something by npm out of the box which allows typing

npm debug

2: Workspace with one or more project packages having a CLI

Instead of crafting a command-line or run-script

NODE_OPTIONS='--inspect-brk' node ./packages/foo-package/bin/foo.js

this RFC proposes to provide something by npm out of the box which allows typing

npm debug foo

3: Debugging tools and 3rd-party packages in a node_modules folder

Consider a project with a package like eslint installed within its node_modules folder. So the package has a CLI. Except, the tool not being eslint but a less prominent one foo with less maintainers and less quality.

  • As a user of such package you've tried everything to make it work, but it doesn't. You want to confirm by debugging the tool that the bug is not in the way you use it but in the tool itself.
  • As a maintainer of such package you request a sample repository for debugging your tool's usage within that sample repository.

Instead of crafting a command-line or run-script

NODE_OPTIONS='--inspect-brk' node ./node_modules/.bin/foo.js

this RFC proposes to provide something by npm out of the box which allows typing

npm debug foo

For example

*node_modules/foo/package.json*
~~~
"bin": {
   "not-my-pkg-name": "./bin/script.js",
   "not-my-pkg-name-2": "./bin/script2.js"
}
~~~

Proposal:
~~~
npm debug <package> --bin <name>
~~~
as a general syntax.

1. GIVEN there are multiple executables THEN there MUST be a `--bin` key and value in `argv`
2. GIVEN there's only a single `bin` object property THEN a `--bin` in `argv` *MAY* be omitted
3. GIVEN there is a `--bin` in `argv` THEN `<name>` MUST match some name in the `bin` object ELSE EXIT with NPM ERR.

So the example above requires
~~~
npm debug foo --bin not-my-pkg-name
~~~

However

*node_modules/foo/package.json*
~~~
"bin": {
   "not-my-pkg-name": "./bin/script.js"
}
~~~

or

~~~
{
  "bin": "./bin/script.js"
}
~~~

both allow for

~~~
npm debug foo
~~~
…debug both?

Allow for selecting which one to debug with --bin and --main argument.
When both arguments are given --bin takes precedence over --main.
When none are given fallback priority is the same.
@darcyclarke darcyclarke removed the Agenda will be discussed at the Open RFC call label Aug 10, 2022
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

Successfully merging this pull request may close these issues.

None yet

3 participants