Repository: bazelbuild/bazel
Issue: .netrc files with comments break http_archive downloads
Labels: team-ExternalDeps
untriaged

### Description of the problem / feature request:

With the following `~/.netrc`, `bazel build ...` fails when the `WORKSPACE` has an un-downloaded repository:

```ini
# Production
machine build.company login foo password bar
machine build-prod.company login foo password bar

# Staging
machine build.stage.company login foo password bar
machine build-dev.company login foo password bar
```

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Run `bazel build` with the above `.netrc` in the user's home directory.

### What operating system are you running Bazel on?

macOS 10.14.4

### What's the output of `bazel info release`?

`release 1.1.0`

###  Have you found anything relevant by searching the web?

Looks like this was caused by https://github.com/bazelbuild/bazel/pull/9388, which does not handle `#` as a token

### Any other information, logs, or outputs that you want to share?

```
ERROR: An error occurred during the fetch of repository 'build_bazel_rules_apple':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/http.bzl", line 72
		_get_auth(ctx, all_urls)
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/http.bzl", line 52, in _get_auth
		read_netrc(ctx, netrcfile)
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 287, in read_netrc
		fail(<1 more arguments>)
Unexpected token '#' while reading /Users/build/.netrc
ERROR: no such package '@build_bazel_rules_apple//apple': Traceback (most recent call last):
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/http.bzl", line 72
		_get_auth(ctx, all_urls)
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/http.bzl", line 52, in _get_auth
		read_netrc(ctx, netrcfile)
	File "/private/var/tmp/_bazel_build/a0b0a7899b2e91189412dbb548223580/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 287, in read_netrc
		fail(<1 more arguments>)
Unexpected token '#' while reading /Users/build/.netrc
```


Issue comments:
The problem could well be alleviated by skipping lines that begin with a `#`. This is probably somewhere in the `read_netrc` function in `tools/build_defs/repo/utils.bzl` at around line 231, where `continue`ing after passing a check for `line.startswith("#")` might suffice. 
@shs96c https://github.com/bazelbuild/bazel/pull/10337

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/build_defs/repo/utils.bzl b/tools/build_defs/repo/utils.bzl
index a369f61954155b..3866b0c23bad5a 100644
--- a/tools/build_defs/repo/utils.bzl
+++ b/tools/build_defs/repo/utils.bzl
@@ -228,7 +228,10 @@ def read_netrc(ctx, filename):
     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"


Test patch:
diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh
index 3b753e42abbc78..3ed8f489d8c0bf 100755
--- a/src/test/shell/bazel/skylark_repository_test.sh
+++ b/src/test/shell/bazel/skylark_repository_test.sh
@@ -1604,6 +1604,7 @@ cd pub
 mget *
 quit
 
+# this is a comment
 machine example.com login
 myusername password mysecret default
 login anonymous password myusername@example.com