From 7c172fc6a755c72d3efe83ba0952acbe1d52f682 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Fri, 3 Oct 2025 18:18:44 +0200 Subject: [PATCH 1/4] fix: exit with code 0 to prevent spurious blank line --- .vscode/launch.json | 17 +++++++++++------ src/alr/alr-commands-exec.adb | 1 - src/alr/alr-main.adb | 24 ++++++++++++++++++++++++ src/alr/alr-utils-temp_file.adb | 2 -- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7f4572754..d6823cb8e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,12 +25,17 @@ "justMyCode": false }, { - "name": "(gdb) Launch alr at /tmp/a/xxx", + "name": "Alire: debug ./bin/alr at /tmp/a/xxx", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/bin/alr", - "args": ["-d", "-vv", "with", "libfoo"], - "stopAtEntry": true, + "args": [ + "exec", + "--", + "echo", + "blah" + ], + "stopAtEntry": false, "cwd": "/tmp/a/xxx", "environment": [], "externalConsole": false, @@ -42,11 +47,11 @@ "ignoreFailures": true }, { - "description": "Set Disassembly Flavor to Intel", - "text": "-gdb-set disassembly-flavor intel", + "description": "Disable questions on multiple matches", + "text": "set multiple-symbols cancel", "ignoreFailures": true } ] } ] -} \ No newline at end of file +} diff --git a/src/alr/alr-commands-exec.adb b/src/alr/alr-commands-exec.adb index 03952db1b..9a20ad1a0 100644 --- a/src/alr/alr-commands-exec.adb +++ b/src/alr/alr-commands-exec.adb @@ -18,7 +18,6 @@ package body Alr.Commands.Exec is use GNAT.Strings; use Ada.Containers; use AAA.Strings; - begin Cmd.Forbids_Structured_Output; diff --git a/src/alr/alr-main.adb b/src/alr/alr-main.adb index 313d0bbd3..e305d2577 100644 --- a/src/alr/alr-main.adb +++ b/src/alr/alr-main.adb @@ -1,7 +1,9 @@ with Ada.Exceptions; +with Alire.Directories; with Alire_Early_Elaboration; pragma Elaborate_All (Alire_Early_Elaboration); with Alire.Errors; +with Alire.OS_Lib; with Alr.Commands; with Alr.Last_Chance_Handler; @@ -11,6 +13,28 @@ begin Trace.Debug ("alr platform configured"); Commands.Execute; + + -- If we let the program end by its own means, there's a spurious blank + -- line printed sometimes. Debugging traces it to a call to + -- + -- procedure s_stalib_adafinal; + -- pragma Import (Ada, s_stalib_adafinal, + -- "system__standard_library__adafinal"); + -- + -- in the generated b__alr-main.adb + -- + -- Since this doesn't always happen, it might be some thing we're doing in + -- those cases that comes back at finalization time. Cursory check of our + -- Finalize procedures didn't turn anything up. That call cannot be stepped + -- into, which difficults things. To be investigated... + -- + -- Sample trigger: `alr exec echo whatever` + + Alire.Directories.Delete_Temporaries; + -- Should not be needed as any temporary is now out of scope, but just in + -- case we call as if we were ending prematurely. + + Alire.OS_Lib.Bailout (0); -- See previous paragraph exception when E : Program_Error => Alire.Log_Exception (E); diff --git a/src/alr/alr-utils-temp_file.adb b/src/alr/alr-utils-temp_file.adb index 7f29dbdaf..35b066169 100644 --- a/src/alr/alr-utils-temp_file.adb +++ b/src/alr/alr-utils-temp_file.adb @@ -12,9 +12,7 @@ package body Alr.Utils.Temp_File is begin if Exists (This.Name) then Delete_File (This.Name); - null; end if; - exception when E : others => Alire.Utils.Finalize_Exception (E); From 543c38bf714998220c3b2f726ee3ef674b4b1f22 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Fri, 3 Oct 2025 22:14:27 +0200 Subject: [PATCH 2/4] Better fix understanding the root cause --- src/alire/alire-os_lib-subprocess.adb | 8 ++++++- src/alr/alr-main.adb | 24 -------------------- testsuite/tests/exec/no-extra-line/test.py | 20 ++++++++++++++++ testsuite/tests/exec/no-extra-line/test.yaml | 3 +++ 4 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 testsuite/tests/exec/no-extra-line/test.py create mode 100644 testsuite/tests/exec/no-extra-line/test.yaml diff --git a/src/alire/alire-os_lib-subprocess.adb b/src/alire/alire-os_lib-subprocess.adb index 41f8ef52e..601275a0c 100644 --- a/src/alire/alire-os_lib-subprocess.adb +++ b/src/alire/alire-os_lib-subprocess.adb @@ -6,6 +6,7 @@ with AnsiAda; use AnsiAda; with CLIC.TTY; +with GNAT.IO; with GNAT.OS_Lib; package body Alire.OS_Lib.Subprocess is @@ -208,7 +209,12 @@ package body Alire.OS_Lib.Subprocess is and then CLIC.TTY.Is_TTY and then CLIC.TTY.Color_Enabled then - Ada.Text_IO.Put (Style (Dim, State)); + -- We cannot use Ada.Text_IO here, as it mandates endlines, + -- and the Ada runtime flushes this with and extra '\n' on + -- finalization if this is the last output, creating an extra + -- empty line in e.g. `alr exec echo whatever`. GNAT.IO is + -- uncooked so it works as expected. + GNAT.IO.Put (Style (Dim, State)); end if; end Dim; diff --git a/src/alr/alr-main.adb b/src/alr/alr-main.adb index e305d2577..313d0bbd3 100644 --- a/src/alr/alr-main.adb +++ b/src/alr/alr-main.adb @@ -1,9 +1,7 @@ with Ada.Exceptions; -with Alire.Directories; with Alire_Early_Elaboration; pragma Elaborate_All (Alire_Early_Elaboration); with Alire.Errors; -with Alire.OS_Lib; with Alr.Commands; with Alr.Last_Chance_Handler; @@ -13,28 +11,6 @@ begin Trace.Debug ("alr platform configured"); Commands.Execute; - - -- If we let the program end by its own means, there's a spurious blank - -- line printed sometimes. Debugging traces it to a call to - -- - -- procedure s_stalib_adafinal; - -- pragma Import (Ada, s_stalib_adafinal, - -- "system__standard_library__adafinal"); - -- - -- in the generated b__alr-main.adb - -- - -- Since this doesn't always happen, it might be some thing we're doing in - -- those cases that comes back at finalization time. Cursory check of our - -- Finalize procedures didn't turn anything up. That call cannot be stepped - -- into, which difficults things. To be investigated... - -- - -- Sample trigger: `alr exec echo whatever` - - Alire.Directories.Delete_Temporaries; - -- Should not be needed as any temporary is now out of scope, but just in - -- case we call as if we were ending prematurely. - - Alire.OS_Lib.Bailout (0); -- See previous paragraph exception when E : Program_Error => Alire.Log_Exception (E); diff --git a/testsuite/tests/exec/no-extra-line/test.py b/testsuite/tests/exec/no-extra-line/test.py new file mode 100644 index 000000000..99225e299 --- /dev/null +++ b/testsuite/tests/exec/no-extra-line/test.py @@ -0,0 +1,20 @@ +""" +Check that `alr exec` does not add an extra line at the end of the output. +Fix for issue #2004. This test is more or less moot, since the bug can only be +observed when running interactively with ANSI coloring, which is disabled in +the testsuite and cannot be forced on. At least, it checks that no extra line is +added in the non-interactive case. +""" + +from drivers.alr import run_alr, init_local_crate +from drivers.asserts import assert_eq +# from drivers.asserts import assert_eq, assert_match + +# Initialize a crate, enter it, and run an echo through exec. Check the output. + +init_local_crate() + +p = run_alr("exec", "--", "echo", "HELLO", quiet=False) +assert_eq("HELLO\n", p.out) # Note: only one newline at the end + +print("SUCCESS") diff --git a/testsuite/tests/exec/no-extra-line/test.yaml b/testsuite/tests/exec/no-extra-line/test.yaml new file mode 100644 index 000000000..fa855459b --- /dev/null +++ b/testsuite/tests/exec/no-extra-line/test.yaml @@ -0,0 +1,3 @@ +driver: python-script +indexes: + compiler_only_index: {} From b5f751946680e1a15a9746c5696a557086f01809 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Fri, 3 Oct 2025 22:22:26 +0200 Subject: [PATCH 3/4] Flush to avoid possibly intermixed output --- src/alire/alire-os_lib-subprocess.adb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/alire/alire-os_lib-subprocess.adb b/src/alire/alire-os_lib-subprocess.adb index 601275a0c..1aea0be22 100644 --- a/src/alire/alire-os_lib-subprocess.adb +++ b/src/alire/alire-os_lib-subprocess.adb @@ -210,10 +210,11 @@ package body Alire.OS_Lib.Subprocess is and then CLIC.TTY.Color_Enabled then -- We cannot use Ada.Text_IO here, as it mandates endlines, - -- and the Ada runtime flushes this with and extra '\n' on + -- and the Ada runtime flushes this with an extra '\n' on -- finalization if this is the last output, creating an extra -- empty line in e.g. `alr exec echo whatever`. GNAT.IO is -- uncooked so it works as expected. + Ada.Text_IO.Flush; -- So we don't mix bufferings GNAT.IO.Put (Style (Dim, State)); end if; end Dim; From 277fed335f524a185690f8cfa38cbe15df14df40 Mon Sep 17 00:00:00 2001 From: "Alejandro R. Mosteo" Date: Fri, 3 Oct 2025 22:34:29 +0200 Subject: [PATCH 4/4] Remove dead code --- testsuite/tests/exec/no-extra-line/test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/testsuite/tests/exec/no-extra-line/test.py b/testsuite/tests/exec/no-extra-line/test.py index 99225e299..a76c7cf9e 100644 --- a/testsuite/tests/exec/no-extra-line/test.py +++ b/testsuite/tests/exec/no-extra-line/test.py @@ -8,7 +8,6 @@ from drivers.alr import run_alr, init_local_crate from drivers.asserts import assert_eq -# from drivers.asserts import assert_eq, assert_match # Initialize a crate, enter it, and run an echo through exec. Check the output.