sourced files can't be always resolved

Open
#63 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
bash, shell
Domain
cli, devtools

Research direction

Reproduce the shown script and inspect the trap handler, especially the debug output for $_Dbg_bash_command, $PWD, and BASH_SOURCE. Investigate how sourced paths are currently resolved from $_Dbg_init_cwd, $_Dbg_cdir, and $(pwd); done means reliably locating ../lib.bash after it is sourced from another working directory, including a regression test for the example.

Written by the indexing model from the issue text.

Description

Follow up to https://github.com/Trepan-Debuggers/bashdb/issues/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?

Dominant language
Shell
Stars
98
Forks
13
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Trepan-Debuggers/bashdb

All issues in Trepan-Debuggers/bashdb

Similar issues

More Shell/Bash issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.