Skip to content

Comments

feat: add openFileInNpmx command for node_modules files#34

Merged
9romise merged 11 commits intonpmx-dev:mainfrom
dgp1130:open-file
Feb 9, 2026
Merged

feat: add openFileInNpmx command for node_modules files#34
9romise merged 11 commits intonpmx-dev:mainfrom
dgp1130:open-file

Conversation

@dgp1130
Copy link
Contributor

@dgp1130 dgp1130 commented Feb 8, 2026

This adds a new command to open the current file on npmx.dev. The intent is to be able to easily create a shareable deep link to a node_modules/... file from within an IDE.

Open.file.on.npmx.2.mp4

Individual commits provide more detail, but the general idea is to resolve the current file to its nearest package.json parent, identify the package name and version, then resolve the package-relative file path and current line number. Then it resolves an npmx.dev link to that file and opens it.

This currently only works for files within an NPM package. I think there's space for another "Open package on npmx.dev" command which works for the directories rather than the files, but I think the value proposition is a little lower and I didn't want to include it in scope here.

Disclaimer: I'm not terribly familiar with VSCode APIs and most of this was written by Gemini in Antigravity with some heavy hand-holding along the way. I'm fairly confident this works and is at least a reasonable implementation of the feature, but this PR might benefit from some thinking about edge cases I didn't consider.

This allows testing code which imports `vscode`, particularly including infrastructure for manipulating `Uri` objects.
This is a generic utility for creating a fake filesystem used by `workspace.fs`. Only `readFile` is implemented for now as that's all that's immediately needed, but other functions could be mocked just as easily when they become necessary.
This adds a new `resolvePackageRelativePath` function which takes an arbitrary file within `node_modules/` and returns its package-relative path. This is useful for generating links to specific files in npmx.dev.

Given a file path, the logic for finding its associated `package.json` is the following:
* Walk up its ancestor directories.
* Check each for a `package.json` file.
* If the file exists, and has a `name` and `version` field, use that path.
  * The motivation for checking `name` and `version` is to ignore "fake" `package.json` files used for configuring specific directories, such as Webpack's [`sideEffects: false`](https://webpack.js.org/guides/tree-shaking/) flag.
* Calculate the relative path from the `package.json` to the original file path.

For this module, we assume the file path is within a `node_modules/` directory, though this will be actually enforced earlier in the process.
This command opens a given file under `node_modules/` in npmx.dev, deep linking to its specific package version, relative file path, and line number. The command is only shown when in a `node_modules/` context and only includes a line number when opening the currently active file.

There are some edge cases for opening files like:
* `node_modules/.bin/foo`
* `node_modules/foo`
* `node_modules/@foo/bar`
* `node_modules/.pnpm`

Ideally these wouldn't even support the command through the `when` filter, but that regex starts to get complicated, so I didn't bite off that complexity here. A potential future feature might be "npmx: Open package" and work specifically on `node_modules/foo` and `node_modules/@foo/bar` directories, opening the package itself rather than deep-linking to any specific file. But such a command is out of scope for now.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 8, 2026

📝 Walkthrough

Walkthrough

This pull request introduces a new VSCode command npmx.openFileInNpmx that opens files located within node_modules directories through the npmx service. The change includes manifest registration of the command with conditional visibility in editor context menus, editor title bars, file explorer context menus, and the command palette, all filtered to node_modules paths. Supporting implementation includes utility functions for constructing npmx URLs and resolving packages relative to files, command handler registration, and test infrastructure with VSCode API mocks and test suites covering filesystem mocking and package resolution scenarios.

🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The PR description clearly explains the feature's purpose, implementation approach, and scope limitations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/commands/open-file-in-npmx.ts (1)

7-13: Make fileUri optional to match actual invocation

Line 7: the handler is called from the command palette without an argument, yet the signature requires Uri. This is misleading and can break under stricter TypeScript settings.

Suggested change
-export async function openFileInNpmx(fileUri: Uri) {
+export async function openFileInNpmx(fileUri?: Uri) {
   const textEditor = useActiveTextEditor()
 
   // If triggered from context menu, fileUri is provided.
   // If triggered from command palette, use active text editor.
-  const uri = fileUri || textEditor.value?.document.uri
+  const uri = fileUri ?? textEditor.value?.document.uri

Copy link
Member

@9romise 9romise left a comment

Choose a reason for hiding this comment

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

Thank you for the creative idea! ❤️ I think this feature is really useful! I've made some changes, as described in each commit message.

@9romise 9romise changed the title Open file on npmx.dev feat: add openFileInNpmx command for node_modules files Feb 8, 2026
@9romise 9romise merged commit d576f06 into npmx-dev:main Feb 9, 2026
6 checks passed
@dgp1130 dgp1130 deleted the open-file branch February 9, 2026 05:05
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.

2 participants