just icon indicating copy to clipboard operation
just copied to clipboard

'import' relative path issue when run symbol linked self-executbale justfile

Open Ziqi-Yang opened this issue 1 year ago • 3 comments

I have these content in my justfile:

#!/usr/bin/env -S just --working-directory . --justfile

import 'lib.just'
import 'config.just'
import 'template_var.just'

I can run the file normally. However, when I symbol linked file (for example ln -sf) and run the symbol link, it prompts error: Could not find source file for import.. (Note the symbol link is at the different directory than targeted justfile)

Ziqi-Yang avatar Mar 25 '24 11:03 Ziqi-Yang

I tried using a symlinked import source file, and I was able to get it to work:

: tree
8192 B symlink-justfile
4096 B ├─ bar.just
4096 B ├─ justfile
   0 B └─ foo.just → /Users/rodarmor/tmp/symlink-justfile/bar.just

2 files, 1 link
: cat justfile
import 'foo.just'
: cat foo.just
bar:
  echo BAR
: just bar
echo BAR
BAR

Can you provide steps to reproduce the issue you're getting?

casey avatar May 15 '24 03:05 casey

Yes, here is the steps for reproducing this issue (make sure you use bash or bash-compatible shells.

cd /tmp
mkdir ./issue-reproduce
cd ./issue-reproduce/

# write justfile
cat > justfile << EOF
#!/usr/bin/env -S just --working-directory . --justfile
pwd:
    pwd

import 'foo.just'
EOF

chmod +x ./justfile

# write foo.just
cat > foo.just << EOF
foo:
    pwd
EOF

Then

cd .. # go back to /tmp
ln -s /tmp/issue-reproduce/justfile ./justfile
./justfile

Finally we can see the error:

❯ ./justfile 
error: Could not find source file for import.
 ——▶ justfile:5:8
  │
5 │ import 'foo.just'
  │        ^^^^^^^^^^

Ziqi-Yang avatar May 15 '24 05:05 Ziqi-Yang

Ahh gotcha, I see the issue. This could be fixed by interpreting imports relative to the canonicalized path to the current justfile, since canonicalization resolves symlinks. However, I worry that this would be somewhat unexpected behavior. I.e., if you didn't want this behavior, and you had a justfile which was symlinked from somewhere else, you would get a pretty confusing error. I could see enabling this with a setting.

casey avatar May 15 '24 06:05 casey