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

Remove dependency on sh #161

Closed
casey opened this issue Mar 13, 2017 · 11 comments
Closed

Remove dependency on sh #161

casey opened this issue Mar 13, 2017 · 11 comments

Comments

@casey
Copy link
Owner

casey commented Mar 13, 2017

sh is just's only significant dependency. I've been thinking for a long time that it would be nice to get rid of it.

I think the best way would be to write a shell library in rust. It would probably be easiest to make it a drop in replacement for dash. It could be integrated as a library to avoid having to fork a new binary to run each command.

Some benefits of this would be:

  • Faster execution time, since there is no need to fork a new binary to run each command
  • Works the same independent of the system-provided sh
  • Support windows
  • Can tokenize and parse recipes ahead of time, and thus emit errors regarding malformed recipes without having to actually run them
  • Currently, variable assignment and cding in recipes don't work as one would expect, since each line is executed by a new shell. If instead some kind of sh library were used, these could be made to work with the expected semantics
  • Support pipefail
  • Can better integrate just variables and shell variables, perhaps allowing both to be accessed with $NAME
  • Can check for use of undefined shell variables
  • Can provide better error messages
  • Since the sh library will be doing the parsing, multi-line constructs would not need to be backslash escaped

Additionally, I think that such a project would have a lot of utility outside of just. sh is one of the most important programs on a system, and writing it in a more secure language like rust would have a lot of benefits. It might be possible to add a lot of aggressive optimizations, features which aren't feasible in C. Also, although the base library would have a very small feature set, and would be a drop in replacement for dash, with the ability to add plugins, it could be the starting point for a more feature rich shell.

One difficult constraint is that we currently depend on Cygwin on Windows. Cygwin binaries contain a system call translation layer, which allows them to use unix paths and a unix userland on windows. If we wrote our own shell we would have to figure out how to continue to provide this functionality.

@jD91mZM2
Copy link

jD91mZM2 commented Aug 17, 2017

I think a cool way would be to let you choose shell. And then it would start the shell and pass commands to its STDIN. This way it wouldn't fork it every time, and also wouldn't be too much work, AND also would be simpilest to implement.

@casey
Copy link
Owner Author

casey commented Aug 17, 2017

@legolord208 I think that would be a neat, although I'm hoping to make just as cross-platform as possible. If we allowed using any shell, then it would also require that that shell be installed wherever the justfile was run.

@jD91mZM2
Copy link

jD91mZM2 commented Aug 17, 2017

@casey OK. I might be able to set up some parser PR some time later. How would you like the syntax?

EDIT: Like, what happens if you mix quote and not quote like "hello "world? In bash this would become "hello world". Should we do the same or throw an error?
Should it support continuing over multiple lines? (Either with unclosed quote or trailing backslash)

@casey
Copy link
Owner Author

casey commented Aug 17, 2017

@legolord208 I've been wanting to do this for a while, but I think it's too massive of a project to be done in a single PR. Also, I'm hoping for strict compatibility with an existing shell, either dash (the debian almquist shell, and the standard sh on most unices), or rc which is a simple sh alternative

@tbodt
Copy link

tbodt commented Aug 18, 2017

The exact functionality of sh is detailed in the POSIX spec, http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html

@jD91mZM2
Copy link

@tbodt Ouch, that sounds like a lot of work :O

@casey
Copy link
Owner Author

casey commented Aug 18, 2017

Yeah, shells do a lot of stuff! If you're interested in tackling writing a shell in rust, I'd suggest making it its own project.

@jD91mZM2
Copy link

@casey Oh, yeah! Perhaps there could be a flag to use ion as shell. Not sure if it has any way to communicate with Rust, but it would be cross-platform 👍

@tbodt
Copy link

tbodt commented Aug 19, 2017

A posix shell parsing library in Rust sounds like a useful thing. Not sure if it exists.

@bugabinga
Copy link

@jD91mZM2 ion is not aiming for POSIX compatability (which I assume to be important for Just based on previous comments). There is cicada, which calls itself a subset of bash, but as of today it is rather a POC. Neither support windows.

For anyone seriously attempting this, I suggest taking a look at oil. It aims to be a superset of bash in order to gradually replace it with saner syntax. It is a C/Python project but take a look at andyc's blog to understand what a massive endeavor this kind of project really is.

@casey casey modified the milestones: 2.0, soon Apr 16, 2019
@casey casey modified the milestones: soon, 1.0, eventually May 27, 2019
@casey casey modified the milestones: 1.0, soon Nov 11, 2019
@casey casey modified the milestones: soon, eventually Nov 11, 2019
@casey
Copy link
Owner Author

casey commented Nov 13, 2019

I just released v0.5.0, which includes the ability to set the shell used for recipes and backticks on a justfile-wide basis. This gives just rudimentary support for cmd.exe and powershell, by adding set shell := ["cmd.exe", "/c"] or set shell := ["powershell", "-c"]. (see #530 and the readme for more details)

I think this sort-of addresses this issue, since sh is no longer a hard dependency. However, a big part of what's been discussed in this issue is the desire to have Just be usable with no external dependencies, and to be able to write cross-platform justfiles.

I opened #531 to track the issue of cross-platform justfiles (I also included a summary of the discussion and design from this thread) and #534 to track the wishlist item of bundling a shell and userland with just.

I hope the new set shell := ... feature is useful! Let me know if you have problems with it, or have suggestions for improving it.

@casey casey closed this as completed Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants