Skip to content

Commit

Permalink
Skip over godebug lines in go.mod/go.work files
Browse files Browse the repository at this point in the history
Go 1.21 introduced support for godebug lines in go.mod and go.work
files. Skip over these lines instead of failing

Related bazel-contrib#1945
  • Loading branch information
zecke committed Oct 5, 2024
1 parent 27bbbc6 commit 93618ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/bzlmod/go_mod.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def parse_go_work(content, go_work_label):
state["use"].append(tokens[0])
elif current_directive == "replace":
_parse_replace_directive(state, tokens, go_work_label.name, line_no)
elif current_directive == "godebug":
pass
else:
fail("{}:{}: unexpected directive '{}'".format(go_work_label.name, line_no, current_directive))
elif tokens[0] == "go":
Expand All @@ -101,6 +103,9 @@ def parse_go_work(content, go_work_label):
state["use"].append(tokens[1])
elif tokens[0] == "toolchain":
continue
elif tokens[0] == "godebug":
if tokens[1] == "(":
current_directive = tokens[0]
else:
fail("{}:{}: unexpected directive '{}'".format(go_work_label.name, line_no, tokens[0]))

Expand Down Expand Up @@ -210,7 +215,7 @@ def parse_go_mod(content, path):
continue

if not current_directive:
if tokens[0] not in ["module", "go", "require", "replace", "exclude", "retract", "toolchain"]:
if tokens[0] not in ["module", "go", "require", "replace", "exclude", "retract", "toolchain", "godebug"]:
fail("{}:{}: unexpected token '{}' at start of line".format(path, line_no, tokens[0]))
if len(tokens) == 1:
fail("{}:{}: expected another token after '{}'".format(path, line_no, tokens[0]))
Expand Down
14 changes: 14 additions & 0 deletions tests/bzlmod/go_mod_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ _GO_MOD_21_CONTENT = """go 1.21.0rc1
module example.com
toolchain go1.22.2
godebug (
default=go1.21
panicnil=1
asynctimerchan=0
)
godebug tlsrsakex=1
"""

_EXPECTED_GO_MOD_21_PARSE_RESULT = struct(
Expand Down Expand Up @@ -114,6 +121,13 @@ use (
replace github.com/go-fsnotify/fsnotify => github.com/fsnotify/fsnotify v1.4.2
replace github.com/bmatcuk/doublestar/v4 v4.0.2 => github.com/bmatcuk/doublestar/v4 v4.0.3
replace example.org/hello => ../fixtures/hello
godebug (
default=go1.21
panicnil=1
asynctimerchan=0
)
godebug tlsrsakex=1
"""

_EXPECTED_GO_WORK_PARSE_RESULT = struct(
Expand Down

0 comments on commit 93618ad

Please sign in to comment.