Skip to content

fix: handle non-native Error throws in requirePnpmfile#11081

Merged
zkochan merged 2 commits into
mainfrom
fix-run
Mar 24, 2026
Merged

fix: handle non-native Error throws in requirePnpmfile#11081
zkochan merged 2 commits into
mainfrom
fix-run

Conversation

@zkochan

@zkochan zkochan commented Mar 24, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace assert(util.types.isNativeError(err)) in requirePnpmfile with a guard that wraps non-native errors into a proper Error
  • Previously, if a pnpmfile threw a non-Error value (e.g. a string or an error from a different realm), pnpm would crash with an unhelpful assertion failure: assert(util.types.isNativeError(err))
  • Now non-native errors are wrapped in new Error(String(err)) and reported via PnpmFileFailError like any other pnpmfile error

Context

This was discovered when @pnpm/plugin-trusted-deps CI failed with assert11(util37.types.isNativeError(err)) during pnpmfile loading. The root cause was that a caught error was not a native Error instance, causing the assertion to crash instead of reporting the error properly.

Test plan

  • Added throwsString.cjs fixture that throws a string
  • Added test: requirePnpmfile wraps non-native-Error throws instead of crashing
  • All 16 pnpmfile tests pass

When a pnpmfile throws a non-native Error value (e.g. a string),
`assert(util.types.isNativeError(err))` crashes pnpm with an
unhelpful assertion failure. Replace the assertion with a guard
that wraps non-native errors into a proper Error and reports them
via PnpmFileFailError.
Copilot AI review requested due to automatic review settings March 24, 2026 16:21

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.

Pull request overview

Improves pnpmfile loading robustness by ensuring requirePnpmfile() reports failures consistently even when pnpmfiles throw non-Error values, avoiding assertion crashes during hook loading.

Changes:

  • Replace the isNativeError assertion with a guard that wraps non-native thrown values and rethrows as PnpmFileFailError.
  • Add a fixture pnpmfile that throws a string to reproduce the crash.
  • Add a regression test to ensure non-Error throws are handled without crashing.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
hooks/pnpmfile/src/requirePnpmfile.ts Replaces assertion with non-native throw wrapping and consistent error reporting.
hooks/pnpmfile/test/index.ts Adds regression test covering non-Error throws from pnpmfiles.
hooks/pnpmfile/test/fixtures/throwsString.cjs Adds fixture pnpmfile that throws a string.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread hooks/pnpmfile/src/requirePnpmfile.ts
@zkochan zkochan merged commit eba01e6 into main Mar 24, 2026
12 checks passed
@zkochan zkochan deleted the fix-run branch March 24, 2026 17:33
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