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

Implement loading justfile from stdin #1933

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

drzymalanet
Copy link

I think it would be nice to have the possibility to pipe justfiles into just, like so:

cat input.txt | just --dump --justfile - > output.txt

It would work by detecting a special symbol "-" as the -f / --justfile argument.

Benefits:

  • Some code editors like helix require this in order to make auto formatting to work,
  • Many command line tools work like this and it seems it will not hurt to have,

The plan:

  1. Move Search{} functionality into Source{}
  2. Implement Source::from_stdin()
  3. Add "-" detection to the argument parser.

Here is the first of the commits, more commits will be added while work progresses.

@casey
Copy link
Owner

casey commented May 15, 2024

This seems reasonable, but this PR looks a bit too complicated and I think some of the changes are unnecessary.

I would create two new SearchConfig variants, WithStdin, and WithStdinAndWorkingDirectory, and handle those two cases separately in Search::find.

@drzymalanet drzymalanet force-pushed the load-from-stdin branch 2 times, most recently from 522568c to 2b3bed9 Compare May 15, 2024 12:03
@drzymalanet
Copy link
Author

@casey I have added the variants, now we need to change Search.justfile type because it is a PathBuf which does not fit to the stdin stream. It seems to me that changing it to an Rc<dyn std::io::Read> or a custom JustfileSource would be some options. What do you think?

@casey
Copy link
Owner

casey commented May 16, 2024

There are two options I can think of:

  1. Add an additional field to Search, which is a string containing the loaded source
  2. Make Search::justfile an enum, like JustfileSource

I would try out 1 first and see how it looks and see if it causes any problems, or requires a big refactor.

@casey casey marked this pull request as draft May 25, 2024 07:32
@drzymalanet drzymalanet reopened this Jun 11, 2024
@drzymalanet
Copy link
Author

Sorry for closing and reopening. It was by mistake.

@casey Please have a look at the new commit. I have added a JustfileKind enum to the Search struct. It does not feel right, but I have no idea how to make it better without a bigger refactor. Let me know what you think.

@casey
Copy link
Owner

casey commented Jun 13, 2024

This looks like a good approach to me!

@drzymalanet
Copy link
Author

Hi @casey I have changed a little the implementation of this PR. Previous approach did not felt right for me. I think the new approach makes more sense. However, I stumbled upon a failing integration test: working_directory::justfile_without_working_directory. It seems it runs the command in a subshell, which makes it hard to find out what's the problem. Any advice on how to trace this?

Let me know what you think about the new changes.

Thanks!

src/search.rs Outdated
@@ -6,7 +6,7 @@ const PROJECT_ROOT_CHILDREN: &[&str] = &[".bzr", ".git", ".hg", ".svn", "_darcs"

#[derive(Debug)]
pub(crate) struct Search {
pub(crate) justfile: PathBuf,
pub(crate) justfile_path: Option<PathBuf>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be justfile, since the type is already a path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Fixed.

Copy link
Owner

@casey casey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really review this PR yet, because it doesn't actually implement reading justfiles from stdin, it's just kind of preliminaries, which, without seeing the implementation, I'm can't give good feedback on.

src/search.rs Outdated
/// Returns an empty path if the justfile path is relative and has only one component.
/// Returns an empty path if the justfile path is itself empty.
/// Returns the current working directory if the justfile is not a file on disk.
pub fn justfile_directory(&self) -> PathBuf {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are kind of footguns. If the justfile was read from stdin, then there is no path or directory to the justfile. I understand that these are kind of compatibility shims, but I think the code just has to be adapted to deal with the fact that the justfile path might be None.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that you still need to know the working directory even if you load the justfile from stdin. On the other hand, many functions don't need to know both the working directory and the optional justfile path. That's what this function does. It abstracts away the justfile directory over the Search struct. I don't have strong opinion on this, but the abstraction seems reasonable. How do you see it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

working_directory is fine, and should always be set. It's justfile and justfile_directory that I think are the issue, since there is no justfile, and no directory containing it.

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

Successfully merging this pull request may close these issues.

2 participants