Support parsing netrc files with comments

Fixes #10215

Closes #10337.

PiperOrigin-RevId: 283044152
diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh
index 3b753e4..3ed8f48 100755
--- a/src/test/shell/bazel/skylark_repository_test.sh
+++ b/src/test/shell/bazel/skylark_repository_test.sh
@@ -1604,6 +1604,7 @@
 mget *
 quit
 
+# this is a comment
 machine example.com login
 myusername password mysecret default
 login anonymous password myusername@example.com
diff --git a/tools/build_defs/repo/utils.bzl b/tools/build_defs/repo/utils.bzl
index a369f61..3866b0c 100644
--- a/tools/build_defs/repo/utils.bzl
+++ b/tools/build_defs/repo/utils.bzl
@@ -228,7 +228,10 @@
     currentmacro = ""
     cmd = None
     for line in contents.splitlines():
-        if macdef:
+        if line.startswith("#"):
+            # Comments start with #. Ignore these lines.
+            continue
+        elif macdef:
             # as we're in a macro, just determine if we reached the end.
             if line:
                 currentmacro += line + "\n"