Skip to content

Commit eb7833c

Browse files
committed
Add Bazel 8 compatibility with automatic bzlmod/WORKSPACE detection
Add centralized compatibility function Remove compat function Add compatibility to build file Import statement and capitalization Remove compat function Adapt import and using the function Remove moved compatibility file Add utils build file Move utils function to separate package and adapt imports
1 parent 30ec83d commit eb7833c

File tree

12 files changed

+2888
-80
lines changed

12 files changed

+2888
-80
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.6.1
1+
8.2.1

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bazel_dep(name = "bazel_skylib", version = "1.8.1")
44
bazel_dep(name = "rules_proto", version = "7.1.0")
55
bazel_dep(name = "rules_go", version = "0.55.1", repo_name = "io_bazel_rules_go")
66
bazel_dep(name = "gazelle", version = "0.44.0", repo_name = "bazel_gazelle")
7+
bazel_dep(name = "bazel_features", version = "1.21.0")
78

89
# Configure Go SDK
910
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")

MODULE.bazel.lock

Lines changed: 2764 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/ibazel/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var Version = "Development"
2929

3030
var overrideableStartupFlags []string = []string{
3131
"--bazelrc",
32+
"--bazelrc=",
33+
"--nobazelrc",
3234
"--home_rc",
3335
"--nohome_rc",
3436
"--output_base",
@@ -53,6 +55,9 @@ var overrideableBazelFlags []string = []string{
5355
"--define=",
5456
"--dynamic_mode=",
5557
"--enable_bzlmod=",
58+
"--enable_bzlmod",
59+
"--enable_workspace",
60+
"--enable_workspace=",
5661
"--features=",
5762
"--flaky_test_attempts=",
5863
"--host_jvmopt",
@@ -62,6 +67,7 @@ var overrideableBazelFlags []string = []string{
6267
"-k",
6368
"--nocache_test_results",
6469
"--noenable_bzlmod",
70+
"--noenable_workspace",
6571
"--nostamp",
6672
"--output_groups=",
6773
"--override_repository=",
@@ -213,7 +219,7 @@ func applyDefaultBazelArgs(bazelArgs []string) []string {
213219
return bazelArgs
214220
}
215221
}
216-
if (isTerminal()) {
222+
if isTerminal() {
217223
return append(bazelArgs, "--isatty=1")
218224
} else {
219225
return append(bazelArgs, "--isatty=0")

internal/bazel/bazel.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ func (b *bazel) newCommand(command string, args ...string) (*bytes.Buffer, *byte
238238
func (b *bazel) Info() (map[string]string, error) {
239239
b.WriteToStderr(false)
240240
b.WriteToStdout(false)
241-
stdoutBuffer, _ := b.newCommand("info")
242-
241+
stdoutBuffer, _ := b.newCommand("info", b.args...)
243242
// This gofunction only prints if 'bazel info' takes longer than 8 seconds
244243
doneCh := make(chan struct{})
245244
defer close(doneCh)
@@ -292,6 +291,7 @@ func (b *bazel) processInfo(info string) (map[string]string, error) {
292291
func (b *bazel) Query(args ...string) (*blaze_query.QueryResult, error) {
293292
blazeArgs := append([]string(nil), "--output=proto", "--order_output=no", "--color=no")
294293
blazeArgs = append(blazeArgs, args...)
294+
blazeArgs = append(blazeArgs, b.args...)
295295

296296
b.WriteToStderr(true) // revert 34c48343 (#536: Improve logging infrastructure) TODO: hide behind argument?
297297
b.WriteToStdout(false)
@@ -383,10 +383,13 @@ func (b *bazel) Test(args ...string) (*bytes.Buffer, error) {
383383
func (b *bazel) Run(args ...string) (*exec.Cmd, *bytes.Buffer, error) {
384384
b.WriteToStderr(true)
385385
b.WriteToStdout(true)
386-
stdoutBuffer, stderrBuffer := b.newCommand("run", append(b.args, args...)...)
386+
runArgs := append(b.args, args...)
387+
stdoutBuffer, stderrBuffer := b.newCommand("run", runArgs...)
387388
b.cmd.Stdin = os.Stdin
388389

389-
_, _ = stdoutBuffer.Write(stderrBuffer.Bytes())
390+
if _, err := stdoutBuffer.Write(stderrBuffer.Bytes()); err != nil {
391+
return nil, nil, fmt.Errorf("stdout.write(): %w", err)
392+
}
390393

391394
err := b.cmd.Run()
392395
if err != nil {

internal/e2e/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ go_library(
1818
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
1919
"@io_bazel_rules_go//go/tools/bazel_testing:go_default_library",
2020
"@org_golang_x_tools//txtar",
21+
"//internal/utils"
2122
],
2223
)

internal/e2e/ibazel.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/bazelbuild/bazel-watcher/internal/utils"
1718
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
1819
)
1920

@@ -22,6 +23,10 @@ const (
2223
defaultDelay = 50 * time.Second
2324
)
2425

26+
var defaultFlags = []string{
27+
"--graceful_termination_wait_duration=1s",
28+
}
29+
2530
type IBazelTester struct {
2631
t *testing.T
2732
ibazelLogFile string
@@ -35,7 +40,7 @@ type IBazelTester struct {
3540
}
3641

3742
func NewIBazelTester(t *testing.T) *IBazelTester {
38-
f, err := ioutil.TempFile("", "ibazel_output.*.log")
43+
f, err := os.CreateTemp("", "ibazel_output.*.log")
3944
if err != nil {
4045
panic(fmt.Sprintf("Error ioutil.Tempfile: %v", err))
4146
}
@@ -66,13 +71,14 @@ func (i *IBazelTester) Test(bazelArgs []string, targets ...string) {
6671
args := []string{"--bazel_path=" + i.bazelPath()}
6772
args = append(args,
6873
"--log_to_file="+i.ibazelLogFile,
69-
"--graceful_termination_wait_duration=1s")
74+
)
7075
args = append(args, "test")
71-
args = append(args, "--bazelrc=/dev/null")
76+
bazelArgs = utils.EnsureBazel8Compatibility(bazelArgs)
7277
args = append(args, targets...)
7378
args = append(args, bazelArgs...)
7479
i.cmd = exec.Command(ibazelPath, args...)
75-
i.t.Logf("ibazel invoked as: %s", strings.Join(i.cmd.Args, " "))
80+
i.t.Logf("ibazel invoked in %s as: %s", i.cmd.Dir,
81+
strings.Join(i.cmd.Args, " "))
7682

7783
i.stdoutBuffer = &Buffer{}
7884
i.cmd.Stdout = i.stdoutBuffer
@@ -89,15 +95,17 @@ func (i *IBazelTester) Coverage(bazelArgs []string, targets ...string) {
8995
i.t.Helper()
9096

9197
args := []string{"--bazel_path=" + i.bazelPath()}
98+
args = append(args, defaultFlags...)
9299
args = append(args,
93100
"--log_to_file="+i.ibazelLogFile,
94-
"--graceful_termination_wait_duration=1s")
101+
)
95102
args = append(args, "coverage")
96-
args = append(args, "--bazelrc=/dev/null")
103+
bazelArgs = utils.EnsureBazel8Compatibility(bazelArgs)
97104
args = append(args, targets...)
98105
args = append(args, bazelArgs...)
99106
i.cmd = exec.Command(ibazelPath, args...)
100-
i.t.Logf("ibazel invoked as: %s", strings.Join(i.cmd.Args, " "))
107+
i.t.Logf("ibazel invoked in %s as: %s", i.cmd.Dir,
108+
strings.Join(i.cmd.Args, " "))
101109

102110
i.stdoutBuffer = &Buffer{}
103111
i.cmd.Stdout = i.stdoutBuffer
@@ -112,24 +120,22 @@ func (i *IBazelTester) Coverage(bazelArgs []string, targets ...string) {
112120

113121
func (i *IBazelTester) Run(bazelArgs []string, target string) {
114122
i.t.Helper()
115-
i.run(target, bazelArgs, []string{
116-
"--log_to_file=" + i.ibazelLogFile,
117-
"--graceful_termination_wait_duration=1s",
118-
})
123+
i.run(target, utils.EnsureBazel8Compatibility(bazelArgs), append(defaultFlags,
124+
"--log_to_file="+i.ibazelLogFile,
125+
))
119126
}
120127

121128
func (i *IBazelTester) RunWithProfiler(target string, profiler string) {
122129
i.t.Helper()
123-
i.run(target, []string{}, []string{
124-
"--log_to_file=" + i.ibazelLogFile,
125-
"--graceful_termination_wait_duration=1s",
126-
"--profile_dev=" + profiler,
127-
})
130+
i.run(target, utils.EnsureBazel8Compatibility([]string{}), append(defaultFlags,
131+
"--log_to_file="+i.ibazelLogFile,
132+
"--profile_dev="+profiler,
133+
))
128134
}
129135

130136
func (i *IBazelTester) RunWithBazelFixCommands(target string) {
131137
i.t.Helper()
132-
i.run(target, []string{}, []string{
138+
i.run(target, utils.EnsureBazel8Compatibility([]string{}), []string{
133139
"--log_to_file=" + i.ibazelLogFile,
134140
"--graceful_termination_wait_duration=1s",
135141
"--run_output=true",
@@ -139,13 +145,13 @@ func (i *IBazelTester) RunWithBazelFixCommands(target string) {
139145

140146
func (i *IBazelTester) RunWithAdditionalArgs(target string, additionalArgs []string) {
141147
i.t.Helper()
142-
i.run(target, []string{}, additionalArgs)
148+
i.run(target, utils.EnsureBazel8Compatibility([]string{}), additionalArgs)
143149
}
144150

145151
func (i *IBazelTester) RunUnverifiedWithAdditionalArgs(target string, additionalArgs []string) {
146152
i.t.Helper()
147153
prebuild := false
148-
i.runUnverified(target, []string{}, additionalArgs, prebuild)
154+
i.runUnverified(target, utils.EnsureBazel8Compatibility([]string{}), additionalArgs, prebuild)
149155
}
150156

151157
func (i *IBazelTester) GetOutput() string {
@@ -415,15 +421,17 @@ func (i *IBazelTester) runUnverified(target string, bazelArgs []string, addition
415421
args := []string{"--bazel_path=" + i.bazelPath()}
416422
args = append(args, additionalArgs...)
417423
args = append(args, "run")
418-
args = append(args, "--bazelrc=/dev/null")
424+
419425
args = append(args, target)
420426
args = append(args, bazelArgs...)
421427
i.cmd = exec.Command(ibazelPath, args...)
422-
i.t.Logf("ibazel invoked as: %s", strings.Join(i.cmd.Args, " "))
428+
i.t.Logf("ibazel invoked in %s as: %s in: %s",
429+
i.cmd.Dir, strings.Join(i.cmd.Args, " "),
430+
os.Getenv("TEST_TMPDIR"))
423431

424432
checkArgs := []string{"build"}
425433
checkArgs = append(checkArgs, target)
426-
checkArgs = append(checkArgs, bazelArgs...)
434+
checkArgs = append(checkArgs, utils.EnsureBazel8Compatibility(bazelArgs)...)
427435
cmd := bazel_testing.BazelCmd(checkArgs...)
428436

429437
var buildStdout, buildStderr bytes.Buffer
@@ -436,6 +444,8 @@ func (i *IBazelTester) runUnverified(target string, bazelArgs []string, addition
436444
if exitErr, ok := err.(*exec.ExitError); ok {
437445
status := exitErr.Sys().(syscall.WaitStatus)
438446
i.t.Fatalf("Unable to build target. Error code: %d\nStdout:\n%s\nStderr:\n%s", status.ExitStatus(), buildStdout.String(), buildStderr.String())
447+
} else {
448+
i.t.Fatalf("Unable to build target. %v", err)
439449
}
440450
}
441451
}

internal/ibazel/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ go_library(
2626
visibility = ["//:__subpackages__"],
2727
deps = [
2828
"//internal/bazel",
29+
"//internal/utils",
2930
"//internal/ibazel/command",
3031
"//internal/ibazel/fswatcher",
3132
"//internal/ibazel/fswatcher/common",

internal/ibazel/ibazel.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/bazelbuild/bazel-watcher/internal/bazel"
29+
"github.com/bazelbuild/bazel-watcher/internal/utils"
2930
"github.com/bazelbuild/bazel-watcher/internal/ibazel/command"
3031
"github.com/bazelbuild/bazel-watcher/internal/ibazel/fswatcher"
3132
"github.com/bazelbuild/bazel-watcher/internal/ibazel/fswatcher/common"
@@ -43,9 +44,9 @@ var bazelNew = bazel.New
4344
var commandDefaultCommand = command.DefaultCommand
4445
var commandNotifyCommand = command.NotifyCommand
4546
var exitMessages = map[os.Signal]string{
46-
syscall.SIGINT: "Subprocess killed from getting SIGINT (trigger SIGINT again to stop ibazel)",
47-
syscall.SIGTERM: "Subprocess killed from getting SIGTERM",
48-
syscall.SIGHUP: "Subprocess killed from getting SIGHUP",
47+
syscall.SIGINT: "Subprocess killed from getting SIGINT (trigger SIGINT again to stop ibazel)",
48+
syscall.SIGTERM: "Subprocess killed from getting SIGTERM",
49+
syscall.SIGHUP: "Subprocess killed from getting SIGHUP",
4950
}
5051

5152
type State string
@@ -169,7 +170,8 @@ func (i *IBazel) handleSignals() {
169170
func (i *IBazel) newBazel() bazel.Bazel {
170171
b := bazelNew()
171172
b.SetStartupArgs(i.startupArgs)
172-
b.SetArguments(i.bazelArgs)
173+
bazelArgs := utils.EnsureBazel8Compatibility(i.bazelArgs)
174+
b.SetArguments(bazelArgs)
173175
return b
174176
}
175177

internal/ibazel/ibazel_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ func TestIBazelBuild(t *testing.T) {
301301
i.build("//path/to:target")
302302
expected := [][]string{
303303
{"SetStartupArgs"},
304-
{"SetArguments"},
304+
{"SetArguments", "--enable_bzlmod"},
305305
{"Info"},
306306
{"SetStartupArgs"},
307-
{"SetArguments"},
307+
{"SetArguments", "--enable_bzlmod"},
308308
{"Cancel"},
309309
{"WriteToStderr", "true"},
310310
{"WriteToStdout", "true"},
@@ -337,16 +337,16 @@ func TestIBazelTest(t *testing.T) {
337337
i.test("//path/to:target")
338338
expected := [][]string{
339339
{"SetStartupArgs"},
340-
{"SetArguments"},
340+
{"SetArguments", "--enable_bzlmod"},
341341
{"Info"},
342342
{"SetStartupArgs"},
343-
{"SetArguments"},
343+
{"SetArguments", "--enable_bzlmod"},
344344
{"SetStartupArgs"},
345-
{"SetArguments"},
345+
{"SetArguments", "--enable_bzlmod"},
346346
{"WriteToStderr", "false"},
347347
{"WriteToStdout", "false"},
348348
{"CQuery", "//path/to:target"},
349-
{"SetArguments", "--test_output=streamed"},
349+
{"SetArguments", "--test_output=streamed", "--enable_bzlmod"},
350350
{"Cancel"},
351351
{"WriteToStderr", "true"},
352352
{"WriteToStdout", "true"},

0 commit comments

Comments
 (0)