Skip to content

How to add shell command with pipe? #44

Description

@chrisgrieser

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?

Activity

  1. ribru17 commented on Sep 14, 2023

    @ribru17
  2. stevearc commented on Sep 15, 2023

    @stevearc
    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"
  3. stevearc commented on Sep 15, 2023

    @stevearc
    Owner

    @ribru17 I have moved your comment to #50

  4. chrisgrieser commented on Sep 15, 2023

    @chrisgrieser
    ContributorAuthor

    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?

  5. stevearc commented on Sep 15, 2023

    @stevearc
    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.

  6. chrisgrieser commented on Sep 15, 2023

    @chrisgrieser
    ContributorAuthor

    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)
    
  7. stevearc commented on Sep 15, 2023

    @stevearc
    Owner

    Try this definition:

    formatters = {
      shellcheck = {
        command = "shellcheck",
        args = "$FILENAME --shell=bash --format=diff | patch -p1 $FILENAME",
        stdin = false,
      },
    },
  8. chrisgrieser commented on Sep 15, 2023

    @chrisgrieser
    ContributorAuthor

    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)

  9. added a commit that references this issue on Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions