Skip to content

Commit

Permalink
viewMany: Exclude options.packageFile from memoization comparison (#1351
Browse files Browse the repository at this point in the history
).

This was preventing memoization of duplicate packages in workspaces.
  • Loading branch information
raineorshine committed Nov 19, 2023
1 parent 82776d5 commit 3542078
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const isExplicitRange = (spec: VersionSpec) => {
return range.some(parsed => EXPLICIT_RANGE_OPS.has(parsed.operator || ''))
}

/** Returns true if the version is sa valid, exact version. */
const isExactVersion = (version: Version) =>
version && (!nodeSemver.validRange(version) || versionUtil.isWildCard(version))

/** Normalizes the keys of an npm config for pacote. */
export const normalizeNpmConfig = (
npmConfig: NpmConfig,
Expand Down Expand Up @@ -428,7 +432,7 @@ async function viewMany(
return mockViewMany(mockReturnedVersions)(packageName, fields, currentVersion, options)
}

if (currentVersion && (!nodeSemver.validRange(currentVersion) || versionUtil.isWildCard(currentVersion))) {
if (isExactVersion(currentVersion)) {
return Promise.resolve({} as Index<Packument>)
}

Expand Down Expand Up @@ -473,8 +477,31 @@ async function viewMany(
return resultNormalized
}

/** Memoize viewMany for --deep performance. */
export const viewManyMemoized = memoize(viewMany)
/** Memoize viewMany for --deep and --workspaces performance. */
export const viewManyMemoized = memoize(viewMany, {
// serializer args are incorrectly typed as any[] instead of being generic, so we need to cast it
serializer: (([
packageName,
fields,
currentVersion,
options,
retried,
npmConfigLocal,
npmConfigWorkspaceProject,
]: Parameters<typeof viewMany>) =>
JSON.stringify([
packageName,
fields,
// currentVersion does not change the behavior of viewMany unless it is an invalid/inexact version which causes it to short circuit
isExactVersion(currentVersion),
// packageFile varies by cwd in workspaces/deep mode, so we do not want to memoize on that
omit(options, 'packageFile'),
// make sure retries do not get memoized
retried,
npmConfigLocal,
npmConfigWorkspaceProject,
])) as (args: any[]) => string,
})

/**
* Returns the value of one of the properties retrieved by npm view.
Expand Down

0 comments on commit 3542078

Please sign in to comment.