Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source_directory() function #1965

Closed
bamiaux opened this issue Mar 14, 2024 · 11 comments
Closed

Add source_directory() function #1965

bamiaux opened this issue Mar 14, 2024 · 11 comments

Comments

@bamiaux
Copy link

bamiaux commented Mar 14, 2024

Edit: Hijacking original comment for visibility.

Add a source_directory() function for getting the directory containing the current file, which can be used regardless of whether it is a module or import.


Hello,

First, thanks for your tool, it replaced Makefile on all my projects, including those on windows platforms!

One of my just file which was working on just 1.21 is now broken.
It seems related to #1813
I understand why it was fixed, but it breaks my use-case, and seems to make the recipe attribute no-cd useless ?

Here's a reduced test case

/tmp/justbug/justfile

import "bar/bar.just
import "foo/foo.just

/tmp/justbug/bar/bar.just

[no-cd, private]
bar:
    @echo bar
    pwd
    # echo *.c

/tmp/justbug/foo/foo.just

foo:
    @echo foo
    pwd
    just @bar
    # cc *.c

On 1.21

/tmp> cd justbug
/tmp/justbug> just foo
foo
/tmp/justbug/foo
bar
/tmp/justbug/foo

It was nice, I could put some just files into dedicated directories & work directly into those directories. I could import other files and share their code. If I only wanted to share some code, I could use the no-cd attribute to make it explicit.

On latest version

/tmp> cd justbug
/tmp/justbug> just foo
foo
/tmp/justbug
bar
/tmp/justbug

Now, no-cd does nothing, the current directory is always the root directory. If you want to keep this behavior, may I suggest removing 'no-cd' and replace it with 'cd' so I could add it to the foo: command & avoid adding cd foo to every commands ?

@laniakea64
Copy link
Contributor

laniakea64 commented Mar 14, 2024

Can you use modules instead of import?

@bamiaux
Copy link
Author

bamiaux commented Mar 14, 2024

I'm not sure about modules:

  • they are behind an --unstable flag, making, imho, commands counter-intuitive to run. job --unstable foo::foo instead of job foo
  • recipes are not imported by default from a module. job bar calls the default recipe from bar module
  • just --unstable --list seems glitchy when printing module recipes, which looks like another bug

@casey
Copy link
Owner

casey commented May 19, 2024

I think modules are indeed the way. It's imperfect, but I decided that imports should behave as if they were included in the justfile directly, whereas modules should behave as independent justfiles, and have their recipes run in the same directory of the module source file. Modules are unstable while they're being developed, so for now the --unstable flag is required.

To support your use-case, we could consider adding a function to get the current directory of the source file, something like source_directory() which can be used in imports to refer to the directory containing the import.

@casey casey changed the title Confusion around current working directory Add source_directory() function May 19, 2024
@bamiaux
Copy link
Author

bamiaux commented May 22, 2024

For information, I added a workaround by scripting commands, so it always tries to change directory to target directory from root directory.
My main issue is that now, most of my just scripts looks like something like that:

foobar:
    cd source_directory() && command A
    cd source_directory() && command B
    cd source_directory() && command C

source_directory() does give me the source directory efficiently, but what I'd like is to setup the working directory once per task.
I know I can do this by starting a shell script, but doing so, we enter into shell script shenanigans under windows.

I'm not asking for this as it may be too obscure, but what I'd really like is something like that:

[cwd=source_directory()]
foobar:
    command A
    command B
    command C

@casey
Copy link
Owner

casey commented May 22, 2024

I think we should add a setting which causes imports to run in the same directory as the file. It would be pretty simple, just pass settings to Recipe::working_directory and if the setting is set, always return self.working_directory.

@casey
Copy link
Owner

casey commented May 22, 2024

Would that be desirable? I.e., would it be okay if there was a setting which applied to all recipes in an import, or do you just want to run some of them in the import source dir? A [cd(DIR) attribute would also be reasonable.

@bamiaux
Copy link
Author

bamiaux commented May 23, 2024

I think we should add a setting which causes imports to run in the same directory as the file. It would be pretty simple, just pass settings to Recipe::working_directory and if the setting is set, always return self.working_directory.

I'm not sure about the syntax, do you mean something like this on top of some imported files ?
Would it be scoped to the current file only ?

set working_directory := source_directory()

In any case, it is certainly enough for my use-case.
But the attribute '[cd(DIR)]' is also much more flexible, and would give another option for https://just.systems/man/en/chapter_47.html

@casey
Copy link
Owner

casey commented May 25, 2024

Yeah, something like that. I agree that [cd(DIR)] would be good too.

@casey
Copy link
Owner

casey commented May 25, 2024

Since I already highjacked this thread for source_directory(), I opened #2082 for [cd(DIR)].

@casey
Copy link
Owner

casey commented May 30, 2024

This was done in #2088.

@casey casey closed this as completed May 30, 2024
@bamiaux
Copy link
Author

bamiaux commented May 31, 2024

Thanks !

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

No branches or pull requests

3 participants