pinterest / arcanist-linters

A collection of custom Arcanist linters
Apache License 2.0
62 stars 45 forks source link

Handle too much output from graphql-schema-linter, other json errors #54

Closed steverice closed 3 years ago

steverice commented 3 years ago

When there are a lot of lint errors raised by graphql-schema-linter, the output JSON it produces can easily exceed the 2^16 character limit on the $stdout contents provided by Arcanist. The resulting truncated string will fail to JSON parse and the script will raise an error.

To correct for this, we check if there were any errors encountered in JSON decoding and raise these explicitly so the user can take action.

For this specific case, we see if the stdout buffer is at its maximum length and, if so, advise the author to run the linter outside of Arcanist and correct issues before running again.

jparise commented 3 years ago

There is also the phutil_json_decode utility function that we could use:

/**
 * Decode a JSON dictionary.
 *
 * @param   string    A string which ostensibly contains a JSON-encoded list or
 *                    dictionary.
 * @return  mixed     Decoded list/dictionary.
 */
function phutil_json_decode($string) {
  $result = @json_decode($string, true);

  if (!is_array($result)) {
    // Failed to decode the JSON. Try to use @{class:PhutilJSONParser} instead.
    // This will probably fail, but will throw a useful exception.
    $parser = new PhutilJSONParser();
    $result = $parser->parse($string);
  }

  return $result;
}

I think it's intended to provide nicer exceptions when fed invalid JSON, which may or not be helpful for this case.

steverice commented 3 years ago

After playing around with this quite a bit more, it seems that it's not Arcanist which is truncating the output, but rather graphql-schema-linter itself: https://github.com/cjoudrey/graphql-schema-linter/pull/262

With that fixed this check is no longer needed.