From 1e09a0ee7e91842c18408a85169f8e29946a8f0c Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 12 Jun 2020 12:05:31 -0700 Subject: [PATCH] When a linter raises a message at a nonexistent line, don't fatal during rendering Summary: See PHI1782. If a linter raises a message at a line which does not exist in the file, render a confused warning rather than fataling. This is a long-existing issue which was exacerbated by D21044. Test Plan: Modified a linter to raise issues at line 99999. Before change: fatal in console rendering. After change: reasonable rendering. Differential Revision: https://secure.phabricator.com/D21357 --- .../renderer/ArcanistConsoleLintRenderer.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lint/renderer/ArcanistConsoleLintRenderer.php b/src/lint/renderer/ArcanistConsoleLintRenderer.php index 6f6daf33..31169084 100644 --- a/src/lint/renderer/ArcanistConsoleLintRenderer.php +++ b/src/lint/renderer/ArcanistConsoleLintRenderer.php @@ -122,6 +122,41 @@ final class ArcanistConsoleLintRenderer extends ArcanistLintRenderer { $old_impact = substr_count($original, "\n") + 1; $start = $line; + // See PHI1782. If a linter raises a message at a line that does not + // exist, just render a warning message. + + // Linters are permitted to raise a warning at the very end of a file. + // For example, if a file is 13 lines long, it is valid to raise a message + // on line 14 as long as the character position is 1 or unspecified and + // there is no "original" text. + + $max_old = count($old_lines); + + $invalid_position = false; + if ($start > ($max_old + 1)) { + $invalid_position = true; + } else if ($start > $max_old) { + if (strlen($original)) { + $invalid_position = true; + } else if ($char !== null && $char !== 1) { + $invalid_position = true; + } + } + + if ($invalid_position) { + $warning = $this->renderLine( + $start, + pht( + '(This message was raised at line %s, but the file only has '. + '%s line(s).)', + new PhutilNumber($start), + new PhutilNumber($max_old)), + false, + '?'); + + return $warning."\n\n"; + } + if ($message->isPatchable()) { $patch_offset = $line_map[$line] + ($char - 1);