casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
21.1k stars 466 forks source link

Add ${just_variable_name} reference in shell script code block #2420

Open linux-china opened 2 weeks ago

linux-china commented 2 weeks ago

Now I'm developing JetBrains Just plugin, and some developers reported error when editing justfile:

image

It's not Just syntax error, and it's caused by inspection from shell script because {{xxx}} is not legal for shell script in some cases.

Snip20241009_24

Now I must disable language injection for code block, but UX is not good because no more shell error hint for developers when write shell script.

A solution is to use ${just_variable_name} style and make it legal for shell script, still be nice or just to replace placeholder with Just variable. Argc uses this way to deal with variable reference from argc.

{{xxx}} could be kept for expression evaluation, and ${just_variable_name} for variable reference.

laniakea64 commented 2 weeks ago

This general approach is not viable in just and anyway doesn't solve the problem.

As you noted, ${just_variable_name} is valid shell syntax. So turning this into just syntax is not backwards-compatible with existing shell script recipes. If someone happens to have a just recipe where a shell variable is named same as a just variable (which I have several such justfile recipes), this syntax addition would change the meaning of the recipe in a way that will break things.

And just recipe bodies are not necessarily shell scripting. They can be any arbitrary language. Even if the ${just_variable_name} syntax were implemented, the same problem would still exist for languages where both {{just_variable_name}} and ${just_variable_name} are invalid syntax.

I'm not familiar with JetBrains at all, so no idea what to suggest for the issue, sorry. In any case, it seems addressing this would need to be done in the JetBrains plugin: a third-party tool erroring on valid just syntax won't have that fixed by just adding a completely new, separate syntax.

linux-china commented 2 weeks ago

According to https://github.com/search?q=path%3A**%2Fjustfile&type=code&ref=advsearch Most Justfiles use shell as primary language. For shell, I think that it should be allowed developers to choose ${just_variable_name} style according to use cases.

For IDEs, it's some hard to add compatible feature because IDEs uses other tools(ShellCheck, Shfmt, Explainshell) too. Every IDE or editor has different plugin APIs, and sometime it's really hard to implement.

laniakea64 commented 2 weeks ago

If just had something like just --show but with interpolations evaluated (like just --dry-run but without printing dependencies), would your JetBrains plugin be able to leverage that functionality for syntax checking?

linux-china commented 2 weeks ago

@laniakea64 still some hard. IDE/Editor has its incremental AST parser based on Document object from editor. If you change or input some characters in shell script, and it's very expensive to call just command and re-build AST.

linux-china commented 1 week ago

@casey any idea about UX for shell script?

laniakea64 commented 1 week ago

Actually, this would seem to be a bug in that shell syntax checker. Copying the script shown in the screenshots into a test.sh file, and trying to run it, results in

./test.sh: line 7: {{echo_command}}: command not found

(run outside a git repository - not including the long output from git diff)

With creating an executable named literally {{echo_command}} on that script's $PATH...

echo Echoing "$@"

... then running test.sh produces

Echoing Locally modified files