From a9a8fb1df86e0ce71fe7663c7798fc5826c7b02f Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Tue, 30 Jan 2024 11:16:51 +0100 Subject: [PATCH] Bugfix: exception when trying to select a tool without the assistant while the configured one is missing (#1551) * Fix exception when trying to select a toolchain This problem arised only if trying to select a toolchain with --select while the configured tool was missing on disk. * Test for fix --- src/alire/alire-solutions-diffs.adb | 5 ++- src/alire/alire-toolchains.adb | 39 +++++++++++++++++++ src/alire/alire-toolchains.ads | 7 ++++ src/alr/alr-commands-toolchain.adb | 1 + .../toolchain/select-while-missing/test.py | 18 +++++++++ .../toolchain/select-while-missing/test.yaml | 4 ++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 testsuite/tests/toolchain/select-while-missing/test.py create mode 100644 testsuite/tests/toolchain/select-while-missing/test.yaml diff --git a/src/alire/alire-solutions-diffs.adb b/src/alire/alire-solutions-diffs.adb index 7d4a89580..e2bd6eea9 100644 --- a/src/alire/alire-solutions-diffs.adb +++ b/src/alire/alire-solutions-diffs.adb @@ -431,7 +431,10 @@ package body Alire.Solutions.Diffs is and then Toolchains.Tool_Is_Configured (GNAT_Crate) and then Toolchains.Tool_Milestone (GNAT_Crate).Crate = GNAT_External_Crate - and then not Toolchains.Tool_Release (GNAT_Crate).Satisfies (Dep) + and then + (Toolchains.Tool_Is_Missing (GNAT_Crate) + or else + not Toolchains.Tool_Release (GNAT_Crate).Satisfies (Dep)) then Trace.Log (Prefix, Level); Trace.Log (Prefix & Icon (Missing) diff --git a/src/alire/alire-toolchains.adb b/src/alire/alire-toolchains.adb index afb32f1c1..184fffe4c 100644 --- a/src/alire/alire-toolchains.adb +++ b/src/alire/alire-toolchains.adb @@ -632,6 +632,34 @@ package body Alire.Toolchains is Put_Info (Release.Milestone.TTY_Image & " installed successfully."); end Deploy; + -------------------- + -- Deploy_Missing -- + -------------------- + + procedure Deploy_Missing is + begin + for Tool of Tools loop + if Tool_Is_Configured (Tool) and then Tool_Is_Missing (Tool) then + declare + Mil : constant Milestones.Milestone := Tool_Milestone (Tool); + begin + Put_Warning ("Tool " & Mil.TTY_Image + & " is missing, redeploying..."); + if Index.Exists (Mil.Crate, Mil.Version) then + Deploy (Index.Find (Mil.Crate, Mil.Version)); + else + Raise_Checked_Error + (Errors.Wrap + ("A configured tool is missing on disk and unavailable " + & "in the loaded index.", + " Run " & TTY.Terminal ("alr toolchain --select") + & " to select another toolchain")); + end if; + end; + end if; + end loop; + end Deploy_Missing; + ------------ -- Remove -- ------------ @@ -725,4 +753,15 @@ package body Alire.Toolchains is raise Constraint_Error with "Not installed: " & Target.TTY_Image; end Release; + --------------------- + -- Tool_Is_Missing -- + --------------------- + + function Tool_Is_Missing (Crate : Crate_Name) return Boolean is + begin + return not (for some Release of Available (Detect_Externals => + Tool_Is_External (Crate)) + => Release.Milestone = Tool_Milestone (Crate)); + end Tool_Is_Missing; + end Alire.Toolchains; diff --git a/src/alire/alire-toolchains.ads b/src/alire/alire-toolchains.ads index 9dd52ac85..55695a614 100644 --- a/src/alire/alire-toolchains.ads +++ b/src/alire/alire-toolchains.ads @@ -71,6 +71,10 @@ package Alire.Toolchains is -- Use the stored config to check if the tool is external without having to -- detect it. Defaults to True if unset or tool is not configured. + function Tool_Is_Missing (Crate : Crate_Name) return Boolean + with Pre => Tool_Is_Configured (Crate); + -- Says is the configured tool is not ready for use and must be downloaded + function Tool_Milestone (Crate : Crate_Name) return Milestones.Milestone; function Tool_Release (Crate : Crate_Name) return Releases.Release; @@ -132,6 +136,9 @@ package Alire.Toolchains is Location : Any_Path := Path); -- Deploy a release in the specified location + procedure Deploy_Missing; + -- Deploy any configured tool that is not found where expected on disk + procedure Remove (Release : Releases.Release; Confirm : Boolean := not CLIC.User_Input.Not_Interactive) diff --git a/src/alr/alr-commands-toolchain.adb b/src/alr/alr-commands-toolchain.adb index e828e7deb..cdfa6f842 100644 --- a/src/alr/alr-commands-toolchain.adb +++ b/src/alr/alr-commands-toolchain.adb @@ -124,6 +124,7 @@ package body Alr.Commands.Toolchain is -- command-line, will impose an origin compatibility constraint if Toolchains.Tool_Is_Configured (Tool) + and then not Toolchains.Tool_Is_Missing (Tool) and then not (for some P of Pending => Toolchains.Tool_Release (Tool).Provides (P) or else diff --git a/testsuite/tests/toolchain/select-while-missing/test.py b/testsuite/tests/toolchain/select-while-missing/test.py new file mode 100644 index 000000000..66fe36263 --- /dev/null +++ b/testsuite/tests/toolchain/select-while-missing/test.py @@ -0,0 +1,18 @@ +""" +Check a particular error in which selecting a compiler via `alr toolchain +--select ` fails if the previously configured compiler is missing on +disk. +""" + +from drivers.alr import run_alr + +# Configure a valid compiler +run_alr("toolchain", "--select", "gnat_native", "gprbuild") + +# Configure an invalid compiler +run_alr("config", "--global", "--set", "toolchain.use.gnat", "gnat_nono=1.2.3") + +# This must succeed +run_alr("toolchain", "--select", "gnat_native=1.0") + +print("SUCCESS") diff --git a/testsuite/tests/toolchain/select-while-missing/test.yaml b/testsuite/tests/toolchain/select-while-missing/test.yaml new file mode 100644 index 000000000..4a56fd231 --- /dev/null +++ b/testsuite/tests/toolchain/select-while-missing/test.yaml @@ -0,0 +1,4 @@ +driver: python-script +build_mode: both +indexes: + gnat_toolchain_index: {}