Re: Function declarations produce no trace nor trigger debug trap while having side effects

2022-10-31 Thread Chet Ramey

On 10/28/22 12:09 PM, Erik Adelbert wrote:

Hello Chet and other bash maintainers,

Thanks for your time.

On 27 Oct 2022, at 23:26, Chet Ramey > wrote:


we believe the current semantic to be at least unclear and would like to 
see either:

0 the DEBUG trap called _before_ any function declaration (preferred)


While calling the DEBUG trap before certain compound commands has value, I
don't think that function definition commands rise to that level.


In the demonstrated case (2nd example of our report), we made clear that
commenting in/out a function definition *seemed* to make an unrelated
error appear/disappear from the debugging support because it was in the
vicinity.


There are a couple of ways to overcome that. You could save and restore the
value of $? around a function declaration, or you could reorder code so
that function definitions come before the command whose exit status you're
interested in.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: Function declarations produce no trace nor trigger debug trap while having side effects

2022-10-28 Thread Oğuz
28 Ekim 2022 Cuma tarihinde Erik Adelbert  yazdı:
>
> 1 there is an error
> 2 the debug TRAP is not invoked before the function definition
> 3 the function definition is required to return 0 if successful
> 4 the debug TRAP is invoked after the successful function definition
>
> This results in an effective masking of the error from the debugging
> support.


This sounds like a misuse of the debug trap. The error can be caught using
the err trap, why don't you use that instead?


-- 
Oğuz


Re: Function declarations produce no trace nor trigger debug trap while having side effects

2022-10-28 Thread Erik Adelbert
Hello Chet and other bash maintainers,

Thanks for your time.

> On 27 Oct 2022, at 23:26, Chet Ramey  wrote:
> 
> we believe the current semantic to be at least unclear and would like to see 
> either:
>> 0 the DEBUG trap called _before_ any function declaration (preferred)
> 
> While calling the DEBUG trap before certain compound commands has value, I
> don't think that function definition commands rise to that level.


In the demonstrated case (2nd example of our report), we made clear that
commenting in/out a function definition *seemed* to make an unrelated
error appear/disappear from the debugging support because it was in the
vicinity.

The explanation lies in the trap *and* function parts of bash documentation:
1 there is an error
2 the debug TRAP is not invoked before the function definition
3 the function definition is required to return 0 if successful
4 the debug TRAP is invoked after the successful function definition

This results in an effective masking of the error from the debugging
support. Function definitions are hardly thought to do that and this situation 
could be the starting point of a slow failure with a super-cloaked origin.

Making a call to the DEBUG trap has some value here:
* it does not compromise existing workflows
* it would make the error visible and actionable by a debugging logic 
* the workflow would always-ish be the same as if the next command 
  was not, well, a function definition

Finally, we are using the DEBUG trap and a bunch of pure-bash routines to
devise a lightweight and efficient error handling scheme for bash scripts. We
believe it to be of interest and have plan to release it. This handler helped 
us 
found the reported problem and would benefit from the proposed modification.

Thanks again and regards,
e.

Re: Function declarations produce no trace nor trigger debug trap while having side effects

2022-10-27 Thread Chet Ramey

On 10/27/22 11:33 AM, Erik Adelbert wrote:


Bash Version: 5.2
Patch Level: 2
Release Status: release

Description:
We have found that 
[https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions shell 
function] _declarations_:
0 do not emit trace output
0 do not call DEBUG trap
0 modify $?

Unless we have missed the related documentation,


This is from the man page for `set':

  -x  After  expanding  each simple command, for command, case
  command, select command, or arithmetic for command, dis-
  play  the expanded value of PS4, followed by the command
  and its expanded arguments or associated word list.

No shell includes function definitions in `set -x' output, and I don't
see how doing so adds value.

And for `trap':

  If a sigspec is EXIT (0) the command arg  is  executed  on  exit
  from  the shell.  If a sigspec is DEBUG, the command arg is exe-
  cuted before every simple command, for  command,  case  command,
  select  command,  every  arithmetic  for command, and before the
  first command executes in a shell function  (see  SHELL  GRAMMAR
  above).   Refer to the description of the extdebug option to the
  shopt builtin for details of its effect on the DEBUG trap.  If a
  sigspec is RETURN, the command arg is executed each time a shell
  function or a script executed with the . or source builtins fin-
  ishes executing.

A function definition is a command itself. This is from the man page in
the `Shell Function Definitions' section:

"The exit status of a function definition  is  zero
 unless  a  syntax  error  occurs or a readonly function with the
 same name already exists."

 we believe the current semantic to be at least unclear and would like to 
see either:

0 the DEBUG trap called _before_ any function declaration (preferred)


While calling the DEBUG trap before certain compound commands has value, I
don't think that function definition commands rise to that level.


0 or function declarations preserving $? as they are not commands appearing in 
trace and DEBUG (has cons)


They are required to exit with status 0.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05_01

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: Function declarations produce no trace nor trigger debug trap while having side effects

2022-10-27 Thread Oğuz
27 Ekim 2022 Perşembe tarihinde Erik Adelbert  yazdı:

> Unless we have missed the related documentation,
>

You have.

> trap [-lp] [[arg] sigspec ...]
> [...] If a sigspec is DEBUG, the command
> arg is executed before every simple command, for
> command, case command, select command, every arithmetic
> for command, and before the first command executes in a
> shell function [...]


-- 
Oğuz