just icon indicating copy to clipboard operation
just copied to clipboard

justfile_directory() -> canonical_justfile_directory()

Open woutervh opened this issue 1 year ago • 7 comments

usecase: I started to make a symlink to my justfile in the root of my filesystem / to have access to my just-recipes from anywhere.

in this case the justfile is a symlink, this introduces an error justfile_directory() which is not resolved

ln -s /path/to/justfile /

It would be nice the have a resolved canonical directory "canonical_justfile_directory()"

woutervh avatar May 01 '24 00:05 woutervh

Can you provide steps to reproduce the issue? I was able to create a justfile, symlink it to /justfile, and then print justfile_directory() which was equal to /.

Is the goal to have justfile_directory() print out the original directory of the justfile?

I think you could use:

dir := `parent(canonicalize(justfile()))`

Does that work for you?

casey avatar May 15 '24 04:05 casey

dir := `parent(canonicalize(justfile()))`

this line fails:

bash: -c: line 1: syntax error near unexpected token `canonicalize'
bash: -c: line 1: `parent(canonicalize(justfile()))'
error: Backtick failed with exit code 2
  ——▶ justfile:31:8
   │
31 │ dir := `parent(canonicalize(justfile()))`
   │        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

woutervh avatar May 17 '24 08:05 woutervh

this works to get the canonical justfile-directory:

canonical_justfile_dir := parent_directory(canonicalize(justfile()))

with a symlinked justfile in the root of the linux filesystem /justfile -> /etc/justfile

just --evaluate
...
canonical_justfile_dir := "/etc"

but now I want to use this variable to import justfiles from /etc/just/.justfile to replace hardcoded paths:

canonical_justfile_dir := parent_directory(canonicalize(justfile()))
import_dir := canonical_justfile_dir / "just"

import '/etc/just/doc8.justfile'
import '/etc/just/docker.justfile'

woutervh avatar May 17 '24 08:05 woutervh

import "${import_dir}/doc8.justfile"

or

import "{{import_dir}}/doc8.justfile"

gives:

tsc@661febeb270f:~$ just --evaluate
error: Could not find source file for import.
  ——▶ justfile:33:8
   │
33 │ import "{{import_dir}}/doc8.justfile"
   │        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tsc@661febeb270f:~$ vi  /etc/justfile
tsc@661febeb270f:~$ just --evaluate
error: Could not find source file for import.
  ——▶ justfile:33:8
   │
33 │ import "${import_dir}/doc8.justfile"
   │        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

woutervh avatar May 17 '24 08:05 woutervh

Note that imports are currently happening relative to the non-canonical justfile-dir. This is incorrect behaviour since you can symlink from many different directories

(I started out using multiple symlinks, ending up with a symlink in /)

directory-structure (in a docker)

/justfile  --> symlink to /etc/justfile
/etc/
    justfile
    just/
        docker.justfile
        git.justfile

in /etc/justfile imports relative to the canonical location of the justfile fail except when $PWD is /etc;

import 'just/docker.justfile'
tsc@661febeb270f:~$ just
error: Could not find source file for import.
  ——▶ justfile:34:8
   │
34 │ import 'just/docker.justfile'
   │        ^^^^^^^^^^^^^^^^^^^^^^

woutervh avatar May 17 '24 09:05 woutervh

Gotcha, that makes sense. Whoops, yeah, I meant dir := parent_directory(canonicalize(justfile())).

Hmm, this is a tricky one.

Could you make your justfile like this:

import '/etc/justfile'`

Instead of a symlink?

casey avatar May 19 '24 00:05 casey