From 5f726f73c644aa44a3d122751329a8a7d22a7b4d Mon Sep 17 00:00:00 2001 From: Roberto Tyley <52038+rtyley@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:42:09 +0100 Subject: [PATCH] feat: Enable use of `latest:` in `.tool-versions` files This change enables `asdf`'s existing latest-version-resolution functionality within the `.tool-versions` file itself. Rather than having to have a `.tool-versions` file that contains a full version number: ``` java corretto-21.0.5.11.1 ``` ...you can now use the same `latest:` syntax that is already available in the `local` & `global` commands, ie: ``` java latest:corretto-21 ``` ### Use case For many tool/runtime ecosystems (eg Java), if a program runs correctly under a specific version of that runtime, it can generally be relied on to run correctly under any _later_ version of that runtime with the same major version number (eg if a project runs under Corretto Java 21.0.5.11.1, it will run on any _later_ version of Corretto Java 21). This means that for projects in those ecosystems, there is little incentive to pin to fully-specified versions like `21.0.5.11.1`, and in fact there are downsides - over time, developers will default to using older, unpatched versions of Java, unless they are assiduous in continually updating the contents of the `.tool-versions` file, or have tooling devoted to doing so. At the Guardian we have several hundred projects that run on the Java platform, and due to our security obligations we generally want to be running under the _latest_ security-patched version of the Java runtime that matches our major-version requirement. We love `asdf` as a tool, and like that the `.tool-versions` file can become a source-of-truth documenting which version of Java a project uses, but we don't want to have to commit fully-specified version numbers like `21.0.5.11.1` to source control, or set up tooling to increment those version numbers across those hundreds of repositories. Allowing the use of `latest:` in the `.tool-versions` file means that we don't need to continually update those `.tool-versions` files. It also partially addresses some of the needs raised by https://github.com/asdf-vm/asdf/issues/1736, though this solution uses the existing `asdf` version-resolution functionality, rather than adopting the version requirements system used in nodejs. ### Implementation A new `resolve_version_spec()` function has been extracted from the existing `version_command()` function. This takes a version-spec string, like `latest:corretto-11` or `corretto-21.0.5.11.1`, and resolves it to a definite installed version number (if the resolved version is not installed, the appropriate error message is shown). This new `resolve_version_spec()` function is now also called in `select_version()`, used by `with_shim_executable()`, meaning that any execution of the `asdf` shim (eg, executing `java`) will now resolve any version specifications found in the `.tool-versions` file - if `.tool-versions` contains `java latest:corretto-21`, this will be resolved and the latest version of Java 21 used. ## Other Information Previous `asdf` PRs relating to `latest`: * https://github.com/asdf-vm/asdf/pull/575 in November 2019: added the `latest` command, eg `asdf latest python 3.6` reports the latest version of Python 3.6. * https://github.com/asdf-vm/asdf/pull/633 in July 2021: made it possible to specify `latest` when using the `local` & `global` commands, eg: `asdf local python latest:3.7` - this would save a precise version number to `.tools-versions`, which is undesired behaviour for us at the Guardian. A couple of Guardian systems attempting to standardise on using `.tool-versions` as a source of truth: * https://github.com/guardian/gha-scala-library-release-workflow/pull/36 * https://github.com/guardian/setup-scala --- lib/commands/command-current.bash | 5 ++++- lib/commands/command-env.bash | 2 ++ lib/commands/command-exec.bash | 2 ++ lib/functions/versions.bash | 26 +++++++++++++++++--------- lib/utils.bash | 3 ++- test/current_command.bats | 10 ++++++++++ test/install_command.bats | 8 ++++++++ 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/commands/command-current.bash b/lib/commands/command-current.bash index f4724626e..eab5ad3ab 100644 --- a/lib/commands/command-current.bash +++ b/lib/commands/command-current.bash @@ -1,6 +1,8 @@ # -*- sh -*- # shellcheck source=lib/functions/plugins.bash . "$(dirname "$(dirname "$0")")/lib/functions/plugins.bash" +# shellcheck source=lib/functions/versions.bash +. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" # shellcheck disable=SC2059 plugin_current_command() { @@ -21,7 +23,8 @@ plugin_current_command() { local description="" IFS=' ' read -r -a versions <<<"$full_version" - for version in "${versions[@]}"; do + for version_spec in "${versions[@]}"; do + version="$(resolve_version_spec "$version_spec")" if ! (check_if_version_exists "$plugin_name" "$version"); then version_not_installed="$version" fi diff --git a/lib/commands/command-env.bash b/lib/commands/command-env.bash index 30cf7ec4e..8c34169e1 100644 --- a/lib/commands/command-env.bash +++ b/lib/commands/command-env.bash @@ -1,4 +1,6 @@ # -*- sh -*- +# shellcheck source=lib/functions/versions.bash +. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" shim_env_command() { local shim_name="$1" diff --git a/lib/commands/command-exec.bash b/lib/commands/command-exec.bash index cb56b36c3..0d4e8d829 100644 --- a/lib/commands/command-exec.bash +++ b/lib/commands/command-exec.bash @@ -1,4 +1,6 @@ # -*- sh -*- +# shellcheck source=lib/functions/versions.bash +. "$(dirname "$(dirname "$0")")/lib/functions/versions.bash" shim_exec_command() { local shim_name diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 7d1d941d6..f9f6ebf8b 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -37,15 +37,7 @@ version_command() { declare -a resolved_versions local item for item in "${!versions[@]}"; do - IFS=':' read -r -a version_info <<<"${versions[$item]}" - if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name" "${version_info[1]}") - elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then - version=$(latest_command "$plugin_name") - else - # if branch handles ref: || path: || normal versions - version="${versions[$item]}" - fi + version="$(resolve_version_spec "${versions[$item]}")" # check_if_version_exists should probably handle if either param is empty string if [ -z "$version" ]; then @@ -79,6 +71,22 @@ version_command() { fi } +resolve_version_spec() { + local version_spec=$1 + + IFS=':' read -r -a version_info <<<"$version_spec" + if [ "${version_info[0]}" = "latest" ] && [ -n "${version_info[1]}" ]; then + version=$(latest_command "$plugin_name" "${version_info[1]}") + elif [ "${version_info[0]}" = "latest" ] && [ -z "${version_info[1]}" ]; then + version=$(latest_command "$plugin_name") + else + # if branch handles ref: || path: || normal versions + version="$version_spec" + fi + + printf "%s\n" "$version" +} + list_all_command() { local plugin_name=$1 local query=$2 diff --git a/lib/utils.bash b/lib/utils.bash index 21978a929..8b40bf548 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -736,7 +736,8 @@ select_version() { version_and_path=$(find_versions "$plugin_name" "$search_path") IFS='|' read -r version_string _path <<<"$version_and_path" IFS=' ' read -r -a usable_plugin_versions <<<"$version_string" - for plugin_version in "${usable_plugin_versions[@]}"; do + for version_spec in "${usable_plugin_versions[@]}"; do + plugin_version="$(resolve_version_spec "$version_spec")" for plugin_and_version in "${shim_versions[@]}"; do local plugin_shim_name local plugin_shim_version diff --git a/test/current_command.bats b/test/current_command.bats index 687bc9da8..b61d2afb9 100755 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -47,6 +47,16 @@ teardown() { [ "$output" = "$expected" ] } +@test "current should not error on version specifications like 'latest:'" { + cd "$PROJECT_DIR" + echo "dummy 1.2.0 latest:1.1" >>"$PROJECT_DIR/.tool-versions" + expected="dummy 1.2.0 latest:1.1 $PROJECT_DIR/.tool-versions" + + run asdf current "dummy" + [ "$status" -eq 0 ] + [ "$output" = "$expected" ] +} + @test "current should derive from the legacy file if enabled" { cd "$PROJECT_DIR" echo 'legacy_version_file = yes' >"$HOME/.asdfrc" diff --git a/test/install_command.bats b/test/install_command.bats index bad99898a..0160ef9e4 100644 --- a/test/install_command.bats +++ b/test/install_command.bats @@ -45,6 +45,14 @@ teardown() { [ "$(cat "$ASDF_DIR/installs/dummy/1.2.0/version")" = "1.2.0" ] } +@test "install_command installs the 'latest:' version in .tool-versions" { + cd "$PROJECT_DIR" + echo -n 'dummy latest:1.1' >".tool-versions" + run asdf install dummy + [ "$status" -eq 0 ] + [ "$(cat "$ASDF_DIR/installs/dummy/1.1.0/version")" = "1.1.0" ] +} + @test "install_command set ASDF_CONCURRENCY" { run asdf install dummy 1.0.0 [ "$status" -eq 0 ]