Skip to content

Add additional error context for HelperSubprocessFailed Sentry errors - #16967

Open
v-abhishekbhaskar wants to merge 2 commits into
mainfrom
abhishekbhaskar/npm-subprocess-error-context
Open

v-abhishekbhaskar wants to merge 2 commits into
mainfrom
abhishekbhaskar/npm-subprocess-error-context

Conversation

@v-abhishekbhaskar

Copy link
Copy Markdown
Contributor

What are you trying to accomplish?

When npm, pnpm or Yarn commands fail (including Yarn YN0001), the resulting HelperSubprocessFailed errors have no safe debugging context, so these failures are hard to triage. This PR attaches three safe error-context keys to those errors using ErrorReportContext.attach:

Key Example Source
command-name pnpm install, yarn up, npm The package manager name plus the first word of the command, only if it is on an allowlist (add, audit, config, dedupe, exec, info, install, list, outdated, remove, up, update, upgrade, view, why). Otherwise only the package manager name is sent.
package-manager-version 10.9.2 See "Where the package manager version comes from" below.
registry-host registry.npmjs.org Host only, from npm_config_registry in the Corepack env, or else from RegistryHelper#registry_url. Left out if it cannot be parsed safely.

Where the package manager version comes from

No extra subprocess runs after a failure. The version comes from information Dependabot already has:

  1. Exact selector: if the command runs through an exact Corepack selector such as npm@10.8.2, that version is used.
  2. Version Dependabot already looked up: PackageManagerHelper#installed_version already runs corepack <pm> -v through Helpers.package_manager_version. That method now also records the result in a per-thread store, keyed by the exact name it ran (npm, yarn, pnpm@8, npm@10). When a command fails, we look up the version recorded under the same name.
  3. Otherwise the key is left out. For example, if installed_version fell back to the lockfile without running corepack <pm> -v, nothing is recorded, so we don't report a guessed version.

The store is per thread, like the existing dependency_files and npm_version_selector state in Helpers.

Changes

  • New NpmAndYarn::SubprocessErrorContext: the subcommand allowlist and the attach logic, including the registry host check.
  • Helpers:
    • Wraps the four places that run npm, pnpm and Yarn commands (run_npm_command, run_selected_pnpm, run_single_yarn_command, package_manager_run_command). It rescues HelperSubprocessFailed, attaches the context and re-raises the original error.
    • package_manager_version records the versions it looks up.
    • If attaching the context fails for any reason, the original error is still raised and a warning is logged.
  • RegistryHelper#registry_url: a new public method that returns the configured registry, or https://registry.npmjs.org. It makes no network calls; unlike find_corepack_env_variables, it never fetches integrity keys.

Anything you want to highlight for special attention from reviewers?

  • Why not run --version or config get after a failure? Many of these failures are expected and rescued (for example in version_resolver.rb). Extra commands there would add latency to normal update runs and could fail themselves. Reusing the version Dependabot already looked up costs nothing.
  • What is never sent: command lines, arguments, paths, full registry URLs, credentials and stderr. Specs check this.
  • Caveat: for npm without a selector, Dependabot runs the local npm binary, while the recorded version comes from corepack npm -v. raise_unless_local_npm_major_matches! ensures the major versions match, so the reported version is accurate to the major version.
  • Nested calls: a command that goes through two wrapped methods (for example, the npm selector path calls package_manager_run_command) gets the context attached once.
  • Bun: bun/lib/dependabot/bun/helpers.rb#run_bun_command is a separate implementation with no Corepack or registry handling, so it isn't changed here. It is tracked as a follow-up.
  • Why the version store is in Helpers: PackageManagerHelper is already at the Metrics/ClassLength limit, so the store lives in Helpers, which already holds similar per-thread state.

How will you know you've accomplished your goal?

New specs check the result of ErrorReportContext.build for each case:

  • helpers_spec.rb:
    • npm with a private .npmrc registry
    • pnpm@8 reports its version; pnpm@7 leaves the version out
    • an exact npm@10.8.2 selector
    • Yarn YN0001 reports yarn up, 4.5.0 and registry.npmjs.org
    • a non-allowlisted command reports only pnpm
    • the original error is re-raised if attaching fails
    • the version is recorded by package_manager_version
  • subprocess_error_context_spec.rb: the allowlist, nothing sensitive leaking, a registry URL that can't be parsed, and a missing version or registry.
  • registry_helper_spec.rb: #registry_url from .npmrc, from a replaces-base credential (no network calls), and the default registry.

Checklist

  • I have run the complete test suite to ensure all tests and linters pass.
  • I have thoroughly tested my code changes to ensure they work as expected, including adding additional tests for new functionality.
  • I have written clear and descriptive commit messages.
  • I have provided a detailed description of the changes in the pull request, including the problem it addresses, how it fixes the problem, and any relevant details about the implementation.
  • I have ensured that the code is well-documented and easy to understand.

@v-abhishekbhaskar v-abhishekbhaskar self-assigned this Nov 18, 2026
@v-abhishekbhaskar
v-abhishekbhaskar requested a review from a team as a code owner November 18, 2026 07:41
Copilot AI balanced review requested due to automatic review settings November 18, 2026 07:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Package-manager version handling can leak credentials into error reports and report cached versions from unrelated projects or runtimes.

2 open issues
What changed in this PR

Adds diagnostic context to npm, pnpm, and Yarn subprocess failures to help triage Dependabot errors without running extra commands after a failure.

Changes:

  • Attaches an allowlisted command name, available package-manager version, and registry host.
  • Records previously queried versions and adds registry lookup without network requests.
  • Adds specs for context filtering, version selection, registry lookup, and preserving original errors.
File Description
npm_and_yarn/​spec/​dependabot/​npm_and_yarn/​subprocess_error_context_spec.rb Tests allowed context fields and sensitive-data filtering.
npm_and_yarn/​spec/​dependabot/​npm_and_yarn/​registry_helper_spec.rb Tests configured and default registry lookup.
npm_and_yarn/​spec/​dependabot/​npm_and_yarn/​helpers_spec.rb Tests failure context, version recording, and error preservation.
npm_and_yarn/​lib/​dependabot/​npm_and_yarn/​subprocess_error_context.rb Builds and attaches filtered subprocess context.
npm_and_yarn/​lib/​dependabot/​npm_and_yarn/​registry_helper.rb Exposes registry lookup without network calls.
npm_and_yarn/​lib/​dependabot/​npm_and_yarn/​helpers.rb Wraps subprocess failures and records package-manager versions.

Review effort: Balanced

Only report whole semver versions so command output that starts with a
version cannot leak credentials or paths, and key recorded versions by
working directory, project and runnable name so failures never report a
version observed for another project or runtime. Also record versions
from in-project -v/--version probes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 42070f36-d24f-4896-beb6-a42a798b2c99
Copilot AI balanced review requested due to automatic review settings November 18, 2026 21:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The new examples do not clear a shared version cache, allowing an earlier test’s cached pnpm version to cause failures under randomized test ordering.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Clear the version cache to isolate examples

npm_and_yarn/​spec/​dependabot/​npm_and_yarn/​helpers_spec.rb:1260

Clear the version cache before seeding these examples. The memoization example in package_manager_helper_spec.rb:973–983 records pnpm@7 as 7.1.0 under the same working-directory/no-manifest key, and the shared cleanup does not clear this cache. With randomized ordering, the new “omits the version when none was read” example can inherit that value and fail if it runs first in this group; resetting the cache here isolates it.

2 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants