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 an option to [working-directory: <path>] create the directory if it's missing #2510

Closed
ricardoseriani opened this issue Dec 9, 2024 · 4 comments

Comments

@ricardoseriani
Copy link

ricardoseriani commented Dec 9, 2024

Sorry to "re-open" this issue... I know that the same issue has already been discussed on Issue #2293, but if possible, add an opt-in flag to the working-directory attribute to create the directory if it's missing.
As @casey said, the issue is that it would mask typos, i.e., the working directory does exist, but you misspelled it in the justfile, just would create it and continue instead of erroring.
But if you create an opt-in feature, it could be enabled in some specific cases to avoid some trouble.
Currently, I have some recipes that needed a specific directory, but in the first run, it wouldn't exist.
One way that I found to "bypass" this problem is by creating another task that create the directories, like the following:

@create_dir path:
  mkdir -p {{path}}
  echo {{path}}

[working-directory: '/tmp/my-task-directory']
my-task: (create_dir "/tmp/my-task-directory")

But the problem is that the path is duplicated, so I think that it's more error-prone.

If that's not possible or desirable, please disregard this issue.

Thanks in advance.

@casey
Copy link
Owner

casey commented Dec 10, 2024

Thanks for the issue! Can you describe the use case a little bit? It's a bit of a niche feature, so I want to make sure there isn't a better workaround.

@ricardoseriani
Copy link
Author

Sure, for example, I want to create/update some kind of "project", so I use the attribute to configure the correct folder.
In the first run, the folder will not exist yet, so I have to create it manually, but in the next run it will already exist.
In that case, I have to either deal with this in the recipe itself, or use the workaround that I mentioned above to create the folder before executing the recipe.
I know that I can use the mkdir inside the current recipe and then add this folder to scp or other tools, or even use a shell script to make the cd "permanent", but I think that if the working-directory could create that folder it will make it more easy and less error-prone.
That makes easy for most of the tasks, like copying some files from a remote machine to this project with scp or any other tools.
Like I said, I know that this could lead to some wrong directory being used, but it could be an opt-in feature, so the default will still stop on this error.

@casey
Copy link
Owner

casey commented Dec 11, 2024

Gotcha!

Something that would be a little more general would be if it were possible to use variables in attributes, so you could do this:

path := '/tmp/my-task-directory'

[working-directory: path]
my-task: (create_dir path)

So you could avoid the deduplication.

Another idea is giving child recipes access to the parent recipe working directory:

[working-directory: '/tmp/my-task-directory']
my-task: mkdir

mkdir:
  mkdir -p {{ DEPENDANT_WORKING_DIRECTORY }}

But, actually, that would require re-running recipes even if their arguments haven't changed, because they're being called by different recipes with different working directories, so that's probably no good.

Another idea is creating a variable that's usable in dependencies which refers to the current working directory:

[working-directory: '/tmp/my-task-directory']
my-task: (mkdir working_directory)

mkdir path:
  mkdir -p {{ path }}

But that's also kind of weird.

I think probably allowing variables in attributes is the best, most general solution to avoid dupllication.

A [mkdir] attribute which created the working directory if it doesn't exist wouldn't be terrible, but it would be quite limited in functionality, i.e., you couldn't also do other kinds of setup which you might want to do.

@casey
Copy link
Owner

casey commented Dec 11, 2024

Closing in favor of #2521, since that fixes this for all attributes.

@casey casey closed this as completed Dec 11, 2024
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

2 participants