Core: Fix npm dependency detection#35083
Conversation
📝 WalkthroughWalkthroughfindInstallations now spawns npm with ChangesNPM installation discovery stderr handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Sidnioulz
left a comment
There was a problem hiding this comment.
Thanks for this PR @fallintoplace!
Could you please help us out with a few followup actions?
- The tests don't help a lot; could you look for ways to make them more focused on outcomes?
- Could you link the PR to an open issue so we understand what impact it has on end users?
Thanks
c6ef1ba to
47af599
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (2)
code/core/src/common/js-package-manager/NPMProxy.test.ts (2)
252-282: ⚡ Quick winConsider adding explanatory comments for complex test setup.
This integration test creates a sophisticated fake npm environment (temporary executables, PATH modification, stderr/stdout simulation) without inline documentation. Per the coding guideline "Document complex mock behaviors in Vitest tests," adding comments would improve maintainability.
Consider adding comments like:
// Create fake npm that writes warnings to stderr while emitting valid JSON to stdout // Validates that no shell redirection tokens (e.g., '2>/dev/null') are passed as args🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@code/core/src/common/js-package-manager/NPMProxy.test.ts` around lines 252 - 282, Add concise inline comments above the fake npm test setup (around the writeFileSync/chmodSync/process.env.PATH block in NPMProxy.test.ts) explaining the mock behavior: that fake-npm.cjs writes a warning to stderr while outputting valid JSON to stdout, that it validates incoming argv to ensure no shell redirection tokens are passed (the args.some check), and that npm and npm.cmd stubs are placed in tempDir and PATH is adjusted (using process.execPath) so the test invokes these executables; keep comments short and specific to help future maintainers understand why writeFileSync, chmodSync, and PATH manipulation are required.Source: Coding guidelines
251-251: 💤 Low valueMock implementation should be in
beforeEachblock.This line sets up the mock implementation inline within the test case, which violates the coding guideline requiring mock implementations in
beforeEachblocks. While this is consistent with the existing pattern in this file (where other tests also configureexecuteCommandmocks per-test), consider refactoring to follow the guideline more strictly.However, this integration-style test requires the real
executeCommandimplementation (unlike other tests that need specific return values), so the inline approach is pragmatic for this specific case.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@code/core/src/common/js-package-manager/NPMProxy.test.ts` at line 251, The mockedExecuteCommand.mockImplementation(actualCommand.executeCommand) call is placed inline in the test; move this mock setup into the file's beforeEach so all tests configure executeCommand there (locate mockedExecuteCommand and actualCommand.executeCommand in NPMProxy.test.ts), and for this special integration-style test either (a) allow the beforeEach to set the real implementation by default or (b) add a per-test override flag that the test can set before execution to keep the inline real implementation; adjust beforeEach to use that flag or the real implementation so the majority of tests follow the guideline while preserving this test's need for the real executeCommand.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@code/core/src/common/js-package-manager/NPMProxy.test.ts`:
- Around line 252-282: Add concise inline comments above the fake npm test setup
(around the writeFileSync/chmodSync/process.env.PATH block in NPMProxy.test.ts)
explaining the mock behavior: that fake-npm.cjs writes a warning to stderr while
outputting valid JSON to stdout, that it validates incoming argv to ensure no
shell redirection tokens are passed (the args.some check), and that npm and
npm.cmd stubs are placed in tempDir and PATH is adjusted (using
process.execPath) so the test invokes these executables; keep comments short and
specific to help future maintainers understand why writeFileSync, chmodSync, and
PATH manipulation are required.
- Line 251: The
mockedExecuteCommand.mockImplementation(actualCommand.executeCommand) call is
placed inline in the test; move this mock setup into the file's beforeEach so
all tests configure executeCommand there (locate mockedExecuteCommand and
actualCommand.executeCommand in NPMProxy.test.ts), and for this special
integration-style test either (a) allow the beforeEach to set the real
implementation by default or (b) add a per-test override flag that the test can
set before execution to keep the inline real implementation; adjust beforeEach
to use that flag or the real implementation so the majority of tests follow the
guideline while preserving this test's need for the real executeCommand.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 67cab0b3-69f8-492b-b15c-4ba97bec0359
📒 Files selected for processing (2)
code/core/src/common/js-package-manager/NPMProxy.test.tscode/core/src/common/js-package-manager/NPMProxy.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- code/core/src/common/js-package-manager/NPMProxy.ts
Package BenchmarksCommit: No significant changes detected, all good. 👏 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
code/core/src/common/js-package-manager/NPMProxy.test.ts (1)
242-250: ⚡ Quick winMove
executeCommandmock behavior setup intobeforeEachfor thisdescribeblock.The new tests configure/reset
mockedExecuteCommandinline inside eachit(...), which conflicts with the Vitest mock-setup rules for this repo. Please centralize reset/default behavior in a localbeforeEach, and keep per-test overrides minimal.As per coding guidelines, "Implement mock behaviors in
beforeEachblocks in Vitest tests" and "Avoid inline mock implementations within test cases in Vitest tests".Also applies to: 270-279, 299-305
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@code/core/src/common/js-package-manager/NPMProxy.test.ts` around lines 242 - 250, Move the mockedExecuteCommand.mockResolvedValue setup out of the individual test cases and into a beforeEach block at the describe block level. Extract the common mock behavior configuration (the default response with stdout containing dependencies and stderr with npm warnings) into the beforeEach, then override only the specific aspects that differ between individual tests within their respective it blocks. This ensures mock setup follows the repository's Vitest conventions where mock behaviors are centralized in beforeEach rather than configured inline within test cases.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@code/core/src/common/js-package-manager/NPMProxy.test.ts`:
- Around line 242-250: Move the mockedExecuteCommand.mockResolvedValue setup out
of the individual test cases and into a beforeEach block at the describe block
level. Extract the common mock behavior configuration (the default response with
stdout containing dependencies and stderr with npm warnings) into the
beforeEach, then override only the specific aspects that differ between
individual tests within their respective it blocks. This ensures mock setup
follows the repository's Vitest conventions where mock behaviors are centralized
in beforeEach rather than configured inline within test cases.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bad6a3dd-6891-47d7-a936-511bc791d81a
📒 Files selected for processing (1)
code/core/src/common/js-package-manager/NPMProxy.test.ts
Fixes #35098
What changed
This removes the fake shell redirection argument from
npm ls --jsonin npm dependency detection. stderr is now suppressed through process stdio options, while stdout stays piped so the JSON output can still be parsed.The regression test now uses a fake
npmexecutable onPATHthat writes valid JSON to stdout and warnings to stderr. That keeps the test focused on the observed dependency-detection outcome rather than only inspecting the command options.Why
executeCommandpasses an argv array directly to the child process. That means2>/dev/nulland2>NULare not shell redirections here; npm receives them as literal positional arguments/package filters, which can make installed dependency detection return incorrect results.Validation
node .yarn/releases/yarn-4.10.3.cjs vitest run --config code/core/vitest.config.ts code/core/src/common/js-package-manager/NPMProxy.test.tsnode .yarn/releases/yarn-4.10.3.cjs exec oxfmt --check code/core/src/common/js-package-manager/NPMProxy.ts code/core/src/common/js-package-manager/NPMProxy.test.tsnode ../.yarn/releases/yarn-4.10.3.cjs lint:js:cmd core/src/common/js-package-manager/NPMProxy.ts core/src/common/js-package-manager/NPMProxy.test.ts --quietManual testing
Not applicable. This change only affects how the npm dependency detection command is invoked, and the regression is covered by the targeted unit test above.
Summary by CodeRabbit
Tests
undefinedwhen stdout is missing.Refactor