目录

ohpm Version Number Rules

The version numbers of third-party packages managed by ohpm follow Semantic Versioning 2.0.0 (SemVer 2.0.0) rules.

Version Number Format

Format: Major version number.Minor version number.Patch version number[-Pre-release version number][+Build information], for example, 1.0.0-PRERELEASE+BUILD.

[h2]Format Constraints

[h2]Special Version Examples

Version Comparison Rules

During version comparison, first compare the standard version number (that is, the major version number, minor version number, and patch version number). The priorities in descending order are as follows: MAJOR, MINOR, and PATCH.

[h2]Rules for Comparing Standard Versions with Pre-Release Versions

The comparison methods for standard versions and pre-release versions are as follows:

// Example 1
> Available versions of dependency liba in the repository: ['1.0.0-snapshot.1', '1.0.0-snapshot.2', '1.0.0', '2.0.0-snapshot.1', '2.0.0-snapshot.2', '2.0.0', '3.0.0-snapshot.1', '3.0.0-snapshot.2']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": ">=1.0.0"
  }
}
> The liba version installed via ohpm install is 2.0.0.

// Example 2
> Available versions of dependency liba in the repository: ['1.0.0-snapshot.1', '1.0.0-snapshot.2', '2.0.0-snapshot.1', '2.0.0-snapshot.2']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": ">=1.0.0"
  }
}
> The liba version installed via ohpm install is 2.0.0-snapshot.2.

// Example 3
> Available versions of dependency liba in the repository: ['1.0.0', '1.0.2-snapshot', '2.0.0-snapshot.1', '2.0.0-snapshot.2']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": "^1.0.0"
  }
}
> If the project depends on liba,
the configuration in the oh-package.json5 file of the project is as follows: {
  "modelVersion": "x.0.0",
  "description": "Please describe the basic information.",
  "dependencies": {
    "liba": "1.0.2-snapshot"
  }
}
> The liba version installed via ohpm install is 1.0.2-snapshot.

// Example 4
> Available versions of dependency liba in the repository: ['1.0.0-snapshot', '1.0.0']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": ">=1.0.0-snapshot"
  }
}
> The liba version installed via ohpm install is 1.0.0.

// Example 5
> Available versions of dependency liba in the repository: ['0.0.1','1.0.0-snapshot', '1.0.1-snapshot']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": ">=1.0.0-snapshot"
  }
}
> The liba version installed via ohpm install is 1.0.1-snapshot.

// Example 6
> Available versions of dependency liba in the repository: ['1.0.0-snapshot.1', '1.0.0-snapshot.2', '2.0.0-snapshot.1', '2.0.0-snapshot.2']
> If the entry module depends on liba, the configuration in the oh-package.json5 file of the entry module is as follows: {
  "name": "entry",
  "version": "1.0.0",
  "description": "Please describe the basic information.",
  "main": "Index.ets",
  "license": "Apache-2.0",
  "dependencies": {
    "liba": ">=1.0.0-snapshot"
  }
}
> The liba version installed via ohpm install is 2.0.0-snapshot.2.

[h2]Pre-Release Version Comparison Rules

When the major, minor, and patch version numbers are identical, the precedence between versions is determined by their pre-release identifiers. Identifiers separated by dots (.) are compared sequentially from left to right, according to the following rules:

Version Range Operators

[h2]Caret (^)

The caret operator (^) fixes the major version number while allowing upgrades to the latest version within the same major version. When the major version is 0, it fixes the first non-zero version number from the left. It can be used for automatically receiving upgrades for the current major version.

// Examples
^1.2.301 is equivalent to: 1.2.301 <= Version < 2.0.0. // Fixes the major version: 1 remains unchanged.
^0.2.301 is equivalent to: 0.2.301 <= Version < 0.3.0. // Fixes the first non-zero version from left: minor version 2 remains unchanged.
^0.0.301 is equivalent to: 0.0.301 <= Version < 0.0.302. // Fixes the first non-zero version from left: patch version 301 remains unchanged.

Since OHPM v5.0.11 (DevEco Studio 5.0.7.210), the caret operator (^) consistently locks the major version only. It no longer pins the leftmost non-zero version segment.

// Examples after caret (^) semantic unification
^1.2.301 is equivalent to: 1.2.301 <= Version < 2.0.0. // Fixes the major version: 1 remains unchanged.
^0.2.301 is equivalent to: 0.2.301 <= Version < 1.0.0. // Fixes the major version: 0 remains unchanged.
^0.0.301 is equivalent to: 0.0.301 <= Version < 1.0.0. // Fixes the major version: 0 remains unchanged.

[h2]Tilde (~)

The tilde operator (~) fixes both the major and minor version numbers while allowing upgrades to the latest patch version within the specified major.minor range. It can be used for receiving only patch updates (bug fixes) while avoiding minor or major version upgrades.

// Examples
~1.2.301 is equivalent to: 1.2.301 <= Version < 1.3.0. // Fixes the major and minor version numbers: 1.2 remains unchanged.
~1.2 is equivalent to ~1.2.0, which means: 1.2.0 <= Version < 1.3.0. // Fixes the major and minor version numbers: 1.2 remains unchanged.
~1 is equivalent to ~1.x, which means: 1.0.0 <= Version < 2.0.0-0. // Fixes the major version number: 1 remains unchanged (special case).
~0.10.100 is equivalent to: 0.10.100 <= Version < 0.11.0. // Fixes the major and minor version numbers: 0.10 remains unchanged.
~0.0 is equivalent to: 0.0.0 <= Version < 0.1.0. // Fixes the major and minor version numbers: 0.0 remains unchanged.
~0 is equivalent to 0.x, which means: 0.0.0 <= Version < 1.0.0-0. // Fixes the major version number: 0 remains unchanged (special case).

[h2]Hyphen (-)/Ellipsis (...)

The hyphen (-) and ellipsis (...) are used to specify a continuous version range that includes both the start and end versions (with spaces required before and after these symbols in the expression). Of the two, the hyphen (-) is recommended due to its broader support across toolchains. This notation is suitable for defining a closed range and simplifying version expressions.

// Examples
1.0.1 - 2.1.1 is equivalent to: 1.0.1 <= Version <= 2.1.1
1.0.1 ... 2.1.1 is equivalent to: 1.0.1 <= Version <= 2.1.1
// Example with an incomplete start version
1.1 - 2.1.0 is equivalent to: 1.1.0 <= Version <= 2.1.0
// Examples with an incomplete end version
1.1.0 - 2.1 is equivalent to: 1.1.0 <= Version < 2.2.0-0
1.1.0 - 2 is equivalent to: 1.1.0 <= Version < 3.0.0-0

[h2]Comparison Operators (>, <, >=, <=, and =)

Comparison operators (>, <, >=, <=, and =) are used to match version ranges literally. For example, ">1.2.0" means the version must be higher than 1.2.0.

Wildcard and Logical Operators

[h2]Wildcard Operators (x and *)

Wildcard operators (x and *) indicate matching any value, typically used to omit fields and simplify range definitions.

// Examples
1.x is equivalent to: 1.0.0 <= Version < 2.0.0, that is, ^1.0.0.
1.1.x is equivalent to: 1.1.0 <= Version < 1.2.0, that is, ~1.1.0.

[h2]Logical AND (Space)

When multiple range conditions are separated by spaces, they are combined with an implicit logical AND, that is, all conditions must be satisfied.

// Example
>=1.0.101 <2.0.100 is equivalent to: 1.0.101 <= Version < 2.0.100.

[h2]Logical OR (||)

The logical OR operator (||) combines multiple version ranges (the union of version ranges that satisfy any one of the conditions).

// Example
^1.0.101 || ^2.0.100 is equivalent to: 1.0.101 <= Version < 2.0.0 or 2.0.100 <= Version < 3.0.0.