casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
22.48k stars 491 forks source link

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

Open Ziqi-Yang opened 8 months ago

Ziqi-Yang commented 8 months ago

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)

casey commented 7 months ago

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?

Ziqi-Yang commented 7 months ago

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'
  │        ^^^^^^^^^^
casey commented 7 months ago

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.