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

Proto extension support Buf formatter #1979

Open
1 task done
06kellyjac opened this issue Feb 1, 2025 · 0 comments
Open
1 task done

Proto extension support Buf formatter #1979

06kellyjac opened this issue Feb 1, 2025 · 0 comments
Labels
needs infrastructure Zed's extension infrastructure doesn't currently support this type of extension

Comments

@06kellyjac
Copy link

Check for existing issues

  • Completed

Misc notes

It'd be nice, in addition to having buf lsp support (#1928) to also have buf formatter support.

There are a few upstream blockers to this


For now you can hack in support for buf formatting like so (use at your own risk):

  "languages": {
    "Proto": {
      "formatter": {
        "external": {
          "command": "bash",
          "arguments": [
            "-c",
            "INPUT=\"$(cat)\";TMPFILE=\"$(mktemp -u -p /tmp).proto\";echo \"$INPUT\" > $TMPFILE;trap 'rm -rf -- \"$TMPFILE\"' EXIT;cd /tmp;buf format --path $TMPFILE;"
          ]
        }
      }
    }
  },

It executes into bash with a script that:

  • captures stdin, being the file content
  • prepares a filename to use in /tmp
    • -u for dryrun because we need to add a suffix
    • .proto needs to be the suffix or buf format --path doesn't actually read it/run
  • dump the stdin capture to the tmpfile location
  • create a trap so when bash exits it'll clean up the tmpfile
    • this also helps if buf crashes or doesn't exist
  • cd into /tmp because buf wants to be relative to the file
  • run buf pointing at the single file with --path

I imagine this should work on Mac but it certainly works for me on Linux.
I think the mktemp usage could be cleaned up a bit but I wanted to avoid using too many features in-case they're GNU mktemp specific.
Maybe there should be a check that $TMPFILE gets written to just to double check the right file is being targeted for deletion.

@06kellyjac 06kellyjac added the needs infrastructure Zed's extension infrastructure doesn't currently support this type of extension label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs infrastructure Zed's extension infrastructure doesn't currently support this type of extension
Projects
None yet
Development

No branches or pull requests

1 participant