Skip to content

sourced files can't be always resolved #63

@jansorg

Description

@jansorg

Follow up to #50
I think that I have a better understanding now of what's going on.

For this script:

cd ./some-dir
. ../lib.bash
cd ~
function_from_lib.bash

The trap handler invoked when function_from_lib.bash is executed, has this data:

  • $BASH_SOURCE[1] == ../lib.bash
  • $PWD == $HOME

To manage breakpoints, we have to resolve ../lib.bash to its source file.

But atm we're only attempting to guess its directory by looking into directories${_Dbg_init_cwd}, $_Dbg_cdir and $(pwd).
But files can be sourced from any directory, based on the working directory at the time source is called.

To reliably find the source file of ../lib.bash we need to know the working directory when source ../lib.bash is called, i.e. we need to know that it's /path/to/some-dir.

With debug output added to the TRAP handler, this is what's available when cd some-dir and . ../lib.bash is called:

TRAP: commmand='cd 'some-dir'', PWD=/Work/source/bashdb/tmp/sources, BASH_SOURCE=../lib/hook.sh sources/main.bash ../bashdb
TRAP: commmand='. ../lib.bash', PWD=/Work/source/bashdb/tmp/sources/some-dir, BASH_SOURCE=../lib/hook.sh sources/main.bash ../bashdb

Possible approach: In the trap handler, it may work to detect . and source commands in $_Dbg_bash_command and store the absolute path of ../lib.bash based on the current $PWD. This would involve parsing the bash command and there won't be a complete solution handling all edge-cases. But managing the basic source ./file/path ignored-args... should be possible, I think.

I'm not an expert with trap handlers and it's possible that I'm not aware of other, possible solutions.

@rocky Do you think that this may work? Do you perhaps know a better approach to solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions