Fix crash bug when a BUILD/bzl file begins with a tab character.

RELNOTES: None
PiperOrigin-RevId: 245440648
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java
index ea93e00..f098a26 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java
@@ -298,9 +298,9 @@
       } else if (c == '\r') {
         pos++;
       } else if (c == '\t') {
-        error("Tab characters are not allowed for indentation. Use spaces instead.");
         indentLen++;
         pos++;
+        error("Tab characters are not allowed for indentation. Use spaces instead.");
       } else if (c == '\n') { // entirely blank line: discard
         indentLen = 0;
         pos++;
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/LexerTest.java b/src/test/java/com/google/devtools/build/lib/syntax/LexerTest.java
index 33a66d1..801e5e1 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/LexerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/LexerTest.java
@@ -502,6 +502,17 @@
   }
 
   @Test
+  public void testFirstCharIsTab() {
+    assertThat(names(tokens("\t"))).isEqualTo("NEWLINE EOF");
+    assertThat(lastErrorLocation.getStartOffset()).isEqualTo(0);
+    assertThat(lastErrorLocation.getEndOffset()).isEqualTo(0);
+    assertThat(lastError)
+        .isEqualTo(
+            "/some/path.txt:1: Tab characters are not allowed for indentation. Use spaces"
+                + " instead.");
+  }
+
+  @Test
   public void testLexerLocationCodec() throws Exception {
     new SerializationTester(createLexer("foo").createLocation(0, 2)).runTests();
   }