Skip to content

pnpm why -r crashes with TypeError: Cannot read properties of undefined (reading 'resolution') at pkgSnapshotToResolution #10700

@meowtec

Description

@meowtec

Verify latest release

  • I verified that the issue exists in the latest pnpm release

pnpm version

10.30.2

Which area(s) of pnpm are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

No response

Reproduction steps

Unfortunately, I'm unable to share a reproduction as the project is in a private internal repo. I am also unable to create a minimal reproduction at this time.

However, I can provide the full error logs and stack traces.

Describe the Bug

When running pnpm why <pkg> -r in a monorepo, the command crashes with a pkgSnapshot is undefined error. This happens because the pkgSnapshotToResolution function is called even when pkgSnapshot is undefined.

 ERROR  Cannot read properties of undefined (reading 'resolution')
pnpm: Cannot read properties of undefined (reading 'resolution')
    at pkgSnapshotToResolution (/snapshot/dist/pnpm.cjs)
    at getPkgInfo (/snapshot/dist/pnpm.cjs)
    at walk (/snapshot/dist/pnpm.cjs)
    at resolvePackageNodes (/snapshot/dist/pnpm.cjs)
    at buildDependentsTree (/snapshot/dist/pnpm.cjs)
    at async whyForPackages (/snapshot/dist/pnpm.cjs)

pkgSnapshotToResolution is called with the (potentially undefined) pkgSnapshot:

    let pkgSnapshot!: PackageSnapshot
    if (opts.currentPackages[depPath]) {
      pkgSnapshot = opts.currentPackages[depPath]
      const parsed = nameVerFromPkgSnapshot(depPath, pkgSnapshot)
      name = parsed.name
      version = parsed.version
    } else {
      pkgSnapshot = opts.wantedPackages[depPath]
      if (pkgSnapshot) {
        const parsed = nameVerFromPkgSnapshot(depPath, pkgSnapshot)
        name = parsed.name
        version = parsed.version
      } else {
        name = opts.alias
        version = opts.ref
      }
      isMissing = true
      isSkipped = opts.skipped.has(depPath)
    }
    resolved = (pkgSnapshotToResolution(depPath, pkgSnapshot, opts.registries) as TarballResolution).tarball

https://github.com/pnpm/pnpm/blob/main/reviewing/dependencies-hierarchy/src/getPkgInfo.ts#L71-L90

In pkgSnapshotToResolution:

function pkgSnapshotToResolution(depPath, pkgSnapshot, registries) {
  // Accessing pkgSnapshot.resolution triggers the TypeError
  if (Boolean(pkgSnapshot.resolution.type) || ...) { 
    return pkgSnapshot.resolution;
  }
}

Expected Behavior

The command should gracefully handle missing snapshots (perhaps by skipping resolution or using a fallback) instead of crashing.

Which Node.js version are you using?

v22.22.0

Which operating systems have you used?

  • macOS
  • Windows
  • Linux

If your OS is a Linux based, which one it is? (Include the version if relevant)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions