Skip to content

Conversation

@arkq
Copy link
Contributor

@arkq arkq commented Oct 23, 2025

This commits fixes the issue when cd is followed by the pwd in a bash one-liner which should get the directory in which the executed script is located. The problem with such approach is that the cd command can print the destination directory in case when one has the CDPATH environmental variable set.

From the man bash:

cd [-L|...] [dir]
Change the current directory to dir. [...] If a non-empty directory
name from CDPATH is used the absolute pathname of the new working
directory is written to the standard output.

Testing

I've only tested the runtime/tests/scripts/onert-test script, so I hope that the rest will be verified by CI. Now, in my setup I'm able to run it. Before that change the outcome was like this:

$ export CDPATH=.:$HOME
$ runtime/tests/scripts/onert-test nnpkg-test
ERROR: 'nnpkg-test' is not supported

ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy [email protected]

This commits fixes the issue when `cd` is followed by the `pwd` in a
bash one-liner which should get the directory in which the executed
script is located. The problem with such approach is that the `cd`
command can print the destination directory in case when one has the
CDPATH environmental variable set.

From the `man bash`:

> cd [-L|...] [dir]
>   Change the current directory to dir. [...] If a non-empty directory
>   name from CDPATH is used the absolute pathname of the new working
>   directory is written to the standard output.

ONE-DCO-1.0-Signed-off-by: Arkadiusz Bokowy <[email protected]>
Copy link
Contributor

@seanshpark seanshpark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz split to separate PR for circle-mlir/* ,
and for each compiler/* to each PRs,
and infra/*

''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # '''
''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # '''
''''exit 255 # '''
''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" >/dev/null && pwd)" # '''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this really needed?
what is the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just checked this particular one-liner and you are right, everything is OK here!

So, here is the detailed explanation what is going on, and why I've raised this PR:

Create some directory in your $HOME, e.g. mkdir -p $HOME/dir/subdir, then create a bash script in it (and make it executable), like this:

$ cat $HOME/dir/subdir/test.sh
#!/bin/bash
SOURCEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Source dir: $SOURCEDIR"

Then run this script:

$ ./dir/subdir/test.sh 
Source dir: /home/arkq/dir/subdir
$ dir/subdir/test.sh
Source dir: /home/arkq/dir/subdir

Now set the CDPATH env variable and run the script again:

$ export CDPATH=$HOME:$HOME/dir
$ ./dir/subdir/test.sh
Source dir: /home/arkq/dir/subdir
$ dir/subdir/test.sh
Source dir: /home/arkq/dir/subdir
/home/a.bokowy/dir/subdir

In the second test, the cd "$(dirname "${BASH_SOURCE[0]}")" prints the destination directory /home/arkq/dir/subdir, so the entire one-liner gets duplicated output. HOWEVER, if the one-liner uses readlink -f, e.g.: SOURCEDIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" the cd gets the absolute path, and in such case the destination directory is not printed!

$ export CDPATH=$HOME:$HOME/dir/subdir
$ cd dir/subdir
/home/arkq/dir/subdir
$ cd $HOME
$ cd $HOME/dir/subdir
$ cd $HOME

So, now the question which approach is better, discarding output or using readlink -f? I think that discarding output is safer, because resolving symlinks will cd you into different directory structure if somewhere along the path symlink was used. Then, relative paths (ones with ../../../) will no longer work.

I will update this PR to account work this finding. Also I will split this PR info separate ones for different parts of this repo.

Many thanks for the review! :)

@seanshpark
Copy link
Contributor

I've only tested the runtime/tests/scripts/onert-test script, so I hope that the rest will be verified by CI.

You should test all your changes. That said, posting 110 files in single PR is not good practice.
And current CI may not check all things, like the bash scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants