From 3d7a1092011d86faa1ffb79f998fd99cdcfb7bb1 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Mon, 26 Feb 2024 18:29:55 +0100 Subject: [PATCH 1/4] Don't run OS initialization during elaboration As this makes impossible to tweak such initialization with `alr config`. --- src/alire/alire-platforms-current.ads | 9 ++++++ .../alire-platforms-current__freebsd.adb | 6 ++++ .../alire-platforms-current__linux.adb | 6 ++++ .../alire-platforms-current__macos.adb | 6 ++++ .../alire-platforms-current__windows.adb | 31 ++++++++++--------- src/alr/alr-commands-config.ads | 4 ++- src/alr/alr-commands.adb | 20 ++++++++++++ .../tests/index/local-index-not-found/test.py | 5 +++ 8 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/alire/alire-platforms-current.ads b/src/alire/alire-platforms-current.ads index 5baae4b92..738075925 100644 --- a/src/alire/alire-platforms-current.ads +++ b/src/alire/alire-platforms-current.ads @@ -31,6 +31,15 @@ package Alire.Platforms.Current is function Operating_System return Platforms.Operating_Systems; + ---------------------- + -- Self configuration + + procedure Initialize; + -- Do any initialization that is necessary for this platform. This is + -- called as soon as we know the user is not running `alr config`, as we + -- want to allow the opportunity to configure things without triggering + -- this initialization. + -------------------------------- -- Portable derived utilities -- -------------------------------- diff --git a/src/alire/os_freebsd/alire-platforms-current__freebsd.adb b/src/alire/os_freebsd/alire-platforms-current__freebsd.adb index baaf885bf..d4ab8fb56 100644 --- a/src/alire/os_freebsd/alire-platforms-current__freebsd.adb +++ b/src/alire/os_freebsd/alire-platforms-current__freebsd.adb @@ -31,4 +31,10 @@ package body Alire.Platforms.Current is function Operating_System return Alire.Platforms.Operating_Systems is (Alire.Platforms.FreeBSD); + ---------------- + -- Initialize -- + ---------------- + + procedure Initialize is null; + end Alire.Platforms.Current; diff --git a/src/alire/os_linux/alire-platforms-current__linux.adb b/src/alire/os_linux/alire-platforms-current__linux.adb index 802574644..cbe071ff2 100644 --- a/src/alire/os_linux/alire-platforms-current__linux.adb +++ b/src/alire/os_linux/alire-platforms-current__linux.adb @@ -133,4 +133,10 @@ package body Alire.Platforms.Current is function Operating_System return Alire.Platforms.Operating_Systems is (Alire.Platforms.Linux); + ---------------- + -- Initialize -- + ---------------- + + procedure Initialize is null; + end Alire.Platforms.Current; diff --git a/src/alire/os_macos/alire-platforms-current__macos.adb b/src/alire/os_macos/alire-platforms-current__macos.adb index faa45aef2..8e220465a 100644 --- a/src/alire/os_macos/alire-platforms-current__macos.adb +++ b/src/alire/os_macos/alire-platforms-current__macos.adb @@ -74,4 +74,10 @@ package body Alire.Platforms.Current is function Operating_System return Platforms.Operating_Systems is (MacOS); + ---------------- + -- Initialize -- + ---------------- + + procedure Initialize is null; + end Alire.Platforms.Current; diff --git a/src/alire/os_windows/alire-platforms-current__windows.adb b/src/alire/os_windows/alire-platforms-current__windows.adb index 971f7e2dd..f3a8bfd39 100644 --- a/src/alire/os_windows/alire-platforms-current__windows.adb +++ b/src/alire/os_windows/alire-platforms-current__windows.adb @@ -27,21 +27,9 @@ package body Alire.Platforms.Current is ------------------ function Detect_Msys2 return Boolean is - use AAA.Strings; begin -- Try to detect if Msys2's pacman tool is already in path - declare - Unused : Vector; - begin - Unused := OS_Lib.Subprocess.Checked_Spawn_And_Capture - ("pacman", Empty_Vector & ("-V"), - Err_To_Out => True); - return True; - exception when others => - null; - end; - - return False; + return OS_Lib.Locate_Exec_On_Path ("pacman") /= ""; end Detect_Msys2; ----------------------- @@ -268,6 +256,12 @@ package body Alire.Platforms.Current is return Alire.Outcome_Success; end if; + -- Prevent unwilling installation of msys2 during testsuite runs + if OS_Lib.Getenv (Environment.Testsuite, "unset") /= "unset" then + raise Program_Error + with "Attempting to install msys2 during testsuite run"; + end if; + Result := Download_File (Msys2_Installer_URL, Msys2_Installer, Install_Dir); @@ -421,6 +415,13 @@ package body Alire.Platforms.Current is end Setup_Msys2; -begin - Setup_Msys2; + ---------------- + -- Initialize -- + ---------------- + + procedure Initialize is + begin + Setup_Msys2; + end Initialize; + end Alire.Platforms.Current; diff --git a/src/alr/alr-commands-config.ads b/src/alr/alr-commands-config.ads index b675a16cb..32d3101c5 100644 --- a/src/alr/alr-commands-config.ads +++ b/src/alr/alr-commands-config.ads @@ -4,9 +4,11 @@ package Alr.Commands.Config is type Command is new Commands.Command with private; + Command_Name : constant String := "config"; + overriding function Name (Cmd : Command) return CLIC.Subcommand.Identifier - is ("config"); + is (Command_Name); overriding procedure Execute (Cmd : in out Command; diff --git a/src/alr/alr-commands.adb b/src/alr/alr-commands.adb index 3917bdf62..5ad5b79ad 100644 --- a/src/alr/alr-commands.adb +++ b/src/alr/alr-commands.adb @@ -518,6 +518,26 @@ package body Alr.Commands is begin + -- Once we know the user is not trying to configure, run the + -- platform-specific initialization (which may rely on such config). + + begin + if Sub_Cmd.What_Command /= Config.Command_Name + then + Alire.Platforms.Current.Initialize; + Trace.Debug ("Platform-specific initialization done."); + else + Trace.Debug + ("Platform-specific initialization skipped (alr config)."); + end if; + exception + when Sub_Cmd.Error_No_Command => + Trace.Debug + ("Platform-specific initialization skipped (no command)."); + -- If the user is running plain `alr` or `alr --version`, it's + -- likely not the time to interrup with an msys2 installation. + end; + Set_Builtin_Aliases; Sub_Cmd.Load_Aliases (Alire.Config.DB.all); diff --git a/testsuite/tests/index/local-index-not-found/test.py b/testsuite/tests/index/local-index-not-found/test.py index 69ba36611..3997eebb2 100644 --- a/testsuite/tests/index/local-index-not-found/test.py +++ b/testsuite/tests/index/local-index-not-found/test.py @@ -13,7 +13,12 @@ for d in ('no-such-directory', 'file://no-such-directory', ): + + # Delete old configuration and indexes, but disable msys2 installation or + # installation will be reattempted. rm('alr-config', recursive=True) + run_alr("config", "--global", "--set", "msys2.do_not_install", "true") + prepare_indexes('alr-config', '.', {'bad_index': {'dir': d, 'in_fixtures': False}}) p = run_alr("search", "--crates", complain_on_error=False, debug=False) From 223116376d900e305696a858c9e0e71d4cde6c52 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Mon, 26 Feb 2024 10:57:34 +0100 Subject: [PATCH 2/4] Make msys live inside overriden config path by default --- src/alire/alire-platforms-folders.ads | 4 ++++ .../os_windows/alire-config-builtins-windows.ads | 4 ++-- testsuite/drivers/alr.py | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/alire/alire-platforms-folders.ads b/src/alire/alire-platforms-folders.ads index 768313865..456866be4 100644 --- a/src/alire/alire-platforms-folders.ads +++ b/src/alire/alire-platforms-folders.ads @@ -2,6 +2,10 @@ package Alire.Platforms.Folders is -- This spec must be fulfilled by bodies for each different OS we support + -- IMPORTANT NOTE: these are not intended to be used directly in most + -- cases; instead, use the ones in Alire.Config.Edit that take into + -- account user overriding of the config location. + function Config return Absolute_Path; -- Folder where alire will store its global configuration, indexes, and -- any other global data. Deleting it is akin to running alr afresh for diff --git a/src/alire/os_windows/alire-config-builtins-windows.ads b/src/alire/os_windows/alire-config-builtins-windows.ads index c787506f2..1cde220ff 100644 --- a/src/alire/os_windows/alire-config-builtins-windows.ads +++ b/src/alire/os_windows/alire-config-builtins-windows.ads @@ -1,4 +1,4 @@ -with Alire.Platforms.Folders; +with Alire.Config.Edit; package Alire.Config.Builtins.Windows is @@ -19,7 +19,7 @@ package Alire.Config.Builtins.Windows is Msys2_Install_Dir : constant Builtin := New_Builtin (Key => "msys2.install_dir", Kind => Cfg_Absolute_Path, - Def => Platforms.Folders.Cache / "msys64", + Def => Config.Edit.Cache_Path / "msys64", Help => "Directory where Alire will detect and/or install" & " msys2 system package manager. (Windows only)"); diff --git a/testsuite/drivers/alr.py b/testsuite/drivers/alr.py index 4ad58ed7a..5635af9f6 100644 --- a/testsuite/drivers/alr.py +++ b/testsuite/drivers/alr.py @@ -47,6 +47,19 @@ def prepare_env(config_dir, env): # We pass config location explicitly in the following calls since env is # not yet applied (it's just a dict to be passed later to subprocess) + # Set the msys2 path to the default one, so that every test does not try to + # reinstall a new copy in their private config + if platform.system() == "Windows": + run_alr("-c", config_dir, "config", "--global", + "--set", "msys2.install_dir", + os.path.join(os.environ.get("LocalAppData"), "alire", "msys64")) + + # And, to make completely sure, disable msys2 installation, as tests + # use their own config that should not rely on a fresh msys2 anyway. On + # GitHub CI, msys2 is set up in advance. + run_alr("-c", config_dir, "config", "--global", + "--set", "msys2.do_not_install", "true") + # Disable autoconfig of the community index, to prevent unintended use of # it in tests, besides the overload of fetching it run_alr("-c", config_dir, "config", "--global", From 3faf72048861e274ef3f3aa838bc89ae0166ba38 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Tue, 27 Feb 2024 12:28:10 +0100 Subject: [PATCH 3/4] Better explanation in alr.py --- testsuite/drivers/alr.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/testsuite/drivers/alr.py b/testsuite/drivers/alr.py index 33c907223..a4d0c5fdf 100644 --- a/testsuite/drivers/alr.py +++ b/testsuite/drivers/alr.py @@ -47,18 +47,16 @@ def prepare_env(config_dir, env): # We pass config location explicitly in the following calls since env is # not yet applied (it's just a dict to be passed later to subprocess) - # Set the msys2 path to the default one, so that every test does not try to - # reinstall a new copy in their private config if platform.system() == "Windows": + # Disable msys inadvertent installation run_alr("-c", config_dir, "config", "--global", - "--set", "msys2.install_dir", - os.path.join(os.environ.get("LocalAppData"), "alire", "msys64")) + "--set", "msys2.do_not_install", "true") - # And, to make completely sure, disable msys2 installation, as tests - # use their own config that should not rely on a fresh msys2 anyway. On - # GitHub CI, msys2 is set up in advance. + # And configure the one set up in the environment so it is used by + # tests that need it. run_alr("-c", config_dir, "config", "--global", - "--set", "msys2.do_not_install", "true") + "--set", "msys2.install_dir", + os.path.join(os.environ.get("LocalAppData"), "alire", "msys64")) # Disable autoconfig of the community index, to prevent unintended use of # it in tests, besides the overload of fetching it From f940d3f5fda14b53ad6eb277e700160f4ac7674e Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Tue, 27 Feb 2024 15:39:01 +0100 Subject: [PATCH 4/4] Fix config loading before Windows builtins --- src/alire/alire-config-edit-early_load.adb | 2 ++ src/alire/alire-config-edit-early_load.ads | 2 +- src/alire/os_windows/alire-config-builtins-windows.ads | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/alire/alire-config-edit-early_load.adb b/src/alire/alire-config-edit-early_load.adb index 039df26f0..e0df70e51 100644 --- a/src/alire/alire-config-edit-early_load.adb +++ b/src/alire/alire-config-edit-early_load.adb @@ -9,4 +9,6 @@ package body Alire.Config.Edit.Early_Load is Alire.Config.Edit.Load_Config; end Load_Config; +begin + Load_Config; end Alire.Config.Edit.Early_Load; diff --git a/src/alire/alire-config-edit-early_load.ads b/src/alire/alire-config-edit-early_load.ads index fbb8af507..73656602e 100644 --- a/src/alire/alire-config-edit-early_load.ads +++ b/src/alire/alire-config-edit-early_load.ads @@ -1,4 +1,4 @@ -package Alire.Config.Edit.Early_Load is +package Alire.Config.Edit.Early_Load with Elaborate_Body is procedure Load_Config; -- For internal use of Alire_Early_Elaboration, DO NOT CALL otherwise. diff --git a/src/alire/os_windows/alire-config-builtins-windows.ads b/src/alire/os_windows/alire-config-builtins-windows.ads index 1cde220ff..ef3d2b422 100644 --- a/src/alire/os_windows/alire-config-builtins-windows.ads +++ b/src/alire/os_windows/alire-config-builtins-windows.ads @@ -1,4 +1,6 @@ -with Alire.Config.Edit; +-- Ensure config is loaded for some defaults below +with Alire.Config.Edit.Early_Load; +pragma Unreferenced (Alire.Config.Edit.Early_Load); package Alire.Config.Builtins.Windows is