Skip to content

Provide a fast-semver module in hardhat-utils and use it wherever possible #8159

@alcuadrado

Description

@alcuadrado

We use semver in multiple parts of the build system, including the config validation/resolution.

This package is notoriously slow to load, so we should only use it when needed.

The only places where we need the full power of semver are:

  • packages/hardhat/src/internal/core/plugins/detect-plugin-npm-dependency-problems.ts
  • packages/hardhat/src/internal/builtin-plugins/solidity/build-system/solc-config-selection.ts
  • And maybe the packages/hardhat/src/internal/cli/init — This is less important to answer though, as that part of the codebase is not present in any hot-path.

In every other case, we only do basic comparisons, and we can get away with a much simpler module built by ouserlves as part of hardhat-utils.

Something like this should be enough:

export type SemverVersion = [major: number, minor: number, patch: number];

export function parseVersion(version: string): SemverVersion | undefined;

// A comparator following the the Array#sort's compareFn interface: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
export function compare(a: SemverVersion, b: SemverVersion): number;

Note that we don't need to support prerelease tags.

We can also add utility methods because the compartor interface can be confusing:

export function equals(a: SemverVersion, b: SemverVersion ): boolean;

export function lowerThan(compared: SemverVersion, comparator: SemverVersion): boolean;

export function greaterThan(compared: SemverVersion, comparator: SemverVersion): boolean;

Once that's implemented, we should use replace all our usages of semver with it, except the onces I mentioned above.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions