mbrobbel / rustfmt-check

GitHub Action to format Rust code using rustfmt
MIT License
30 stars 6 forks source link

bug: Unexpected end of JSON input #988

Open Kamilcuk opened 3 months ago

Kamilcuk commented 3 months ago

I am running the action on our enterprise against our private repo. The result in github actions window is a long json follows by a Unexpected end of JSON input message.

Run avt/thirdparty.rustfmt-check@0.9.0
  with:
    token: ***
    mode: review
    commit-message: Format Rust code using rustfmt
  env:
    CARGO_TERM_COLOR: always
    CARGO_INCREMENTAL: 0
/home/runner/.cargo/bin/cargo +nightly fmt -- --emit json
[{"name":"/_work/avt-runner-..............);\n"}]}]
Error: Unexpected end of JSON input

I have confirmed mulitple times that the string [{"name":"/_work/avt-runner-..............);\n"}]}] is a valid JSON. The JSON has 18938 bytes.

Looking at the source code, I see the following https://github.com/mbrobbel/rustfmt-check/blob/master/src/check.ts#L42 . Most probably 19000 bytes is more than output buffering, so I suspect what actually happens, is that only part of the JSON, most probably 16384 bytes, goes into add function. Because it is not a full message, add is not able to parse the full JSON.

Could this be changed into buffering, like https://github.com/mbrobbel/rustfmt-check/blob/master/src/rustfmt.ts#L18? Does fixing it to the following would work?

  const buffer = "";
  await exec.exec(
    "cargo",
    ["+nightly", "fmt"]
      .concat(stringArgv(args))
      .concat(["--", "--emit", "json"]),
    {
      listeners: {
        stdout: (data: buffer) => { buffer += data.toString(); },
      },
    },
  );
  const result: Result[] = [];
    JSON.parse(buffer.trim()).forEach((output: Output) => {
      output.mismatches.forEach((mismatch) => {
        result.push({ path: output.name, mismatch });
      });
    });
  return result;

Thanks

Kamilcuk commented 3 months ago

I am still getting the same error with 0.10.0. Anything you might recommend? This is workflow file:

jobs:
  format:
    if: ${{ github.event_name == 'pull_request' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ....rust-toolchain@stable
        with:
          toolchain: nightly
          components: rustfmt
      - uses: ....rustfmt-check@0.10.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          mode: review
mbrobbel commented 3 months ago

I'm trying to reproduce this with a test that outputs a very large JSON, but I can't reproduce the error. Is there anything that you share that might help me reproduce this, so I can attempt to fix it?