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

How to add shell command with pipe? #44

Closed
chrisgrieser opened this issue Sep 14, 2023 · 8 comments · Fixed by #49
Closed

How to add shell command with pipe? #44

chrisgrieser opened this issue Sep 14, 2023 · 8 comments · Fixed by #49

Comments

@chrisgrieser
Copy link
Contributor

chrisgrieser commented Sep 14, 2023

The way conform.nvim is set up, it seems slightly more complicated commands do not work.

For example, shellcheck can be used to auto-fix some issues using this, effectively replacing shellharden:

shellcheck --format=diff my_file.sh | git apply

This works fine in the terminal, but the following config does not work:

formatters = {
	shellcheck = {
		command = "shellcheck",
		args = {"--format=diff", "$FILENAME", "|", "git", "apply" },
		stdin = false,
	},
},

I assume this is because in the background, conform.nvim uses fn.system with a list of strings, where pipes do not work. However, using fn.system with a single string does work fine, but passing a string to args results in an error.

Is there a way to make this work?

@ribru17

This comment was marked as off-topic.

@stevearc
Copy link
Owner

@chrisgrieser can you take a look at #49 and see if it works for you? You can run the command in a shell by returning args as a string instead of a list. e.g.

args = "--format=diff $FILENAME | git apply"

@stevearc
Copy link
Owner

@ribru17 I have moved your comment to #50

@chrisgrieser
Copy link
Contributor Author

thx, indeed the change works as it should, that is the using a string as args.

Only problem is, the related shell command does not play nice with temp files, it seems? 🤔

shellcheck foobar.sh --format=diff
--- a/foobar.sh
+++ b/foobar.sh
@@ -1,1 +1,1 @@
-echo $SHELL
+echo "$SHELL"

Reading via stdin does not work here, since the diff is missing the filenames for git apply:

--- a/-
+++ b/-

Reading the file directly apparently has the issue that the temp files apparently cause some issue, too. Here the error message from :ConformInfo

07:38:23[ERROR] Formatter 'shellcheck' error: shellcheck: shellcheck: openBinaryFile: does not exist (No such file or directory)
error: zsh/Users/chrisgrieser/.config/zsh/.conform.6310933.foobar.sh: No such file or directory

for reference, here is the config I am using:

shellcheck = {
	command = "shellcheck",
	args = "shellcheck $FILENAME --shell=bash --format=diff | git apply",
	stdin = false,
},

In case this is not directly solvable, an alternative approach for this particular case might be for conform.nvim to use the diff resulting from shellcheck --format=diff and apply the changes, since it has some diff-applying method anyway?

@stevearc
Copy link
Owner

In the linked issue they mention using | patch -p1. Does that approach work better than git apply? Anything that relies on git is going to have problems with temp files.

@chrisgrieser
Copy link
Contributor Author

hmm, using | patch -p1 also does not seem to work

11:40:15[INFO] Run shellcheck on /Users/chrisgrieser/.config/zsh/foobar.sh
11:40:15[DEBUG] Creating temp file /Users/chrisgrieser/.config/zsh/.conform.6310933.foobar.sh
11:40:15[DEBUG] Run command: shellcheck shellcheck /Users/chrisgrieser/.config/zsh/.conform.6310933.foobar.sh --shell=bash --format=diff | patch -p1
11:40:15[INFO] shellcheck exited with code 1
11:40:15[DEBUG] shellcheck stdout: nil
11:40:15[DEBUG] shellcheck stderr: { "shellcheck: shellcheck: openBinaryFile: does not exist (No such file or directory)", "" }
11:40:15[DEBUG] Cleaning up temp file /Users/chrisgrieser/.config/zsh/.conform.6310933.foobar.sh
11:40:15[ERROR] Formatter 'shellcheck' error: shellcheck: shellcheck: openBinaryFile: does not exist (No such file or directory)

@stevearc
Copy link
Owner

Try this definition:

formatters = {
  shellcheck = {
    command = "shellcheck",
    args = "$FILENAME --shell=bash --format=diff | patch -p1 $FILENAME",
    stdin = false,
  },
},

@chrisgrieser
Copy link
Contributor Author

nice, that one works! Thx for the support. 🥳

Guess that one can also be directly be added to the conform.nvim, I guess? (when #49 is merged)

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 a pull request may close this issue.

3 participants