forked from bazelbuild/bazelisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazelisk.go
818 lines (702 loc) · 22.8 KB
/
bazelisk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
"time"
version "github.com/hashicorp/go-version"
homedir "github.com/mitchellh/go-homedir"
)
const (
bazelReal = "BAZEL_REAL"
skipWrapperEnv = "BAZELISK_SKIP_WRAPPER"
wrapperPath = "./tools/bazel"
bazelUpstream = "bazelbuild"
)
var (
BazeliskVersion = "development"
)
func findWorkspaceRoot(root string) string {
if _, err := os.Stat(filepath.Join(root, "WORKSPACE")); err == nil {
return root
}
if _, err := os.Stat(filepath.Join(root, "WORKSPACE.bazel")); err == nil {
return root
}
parentDirectory := filepath.Dir(root)
if parentDirectory == root {
return ""
}
return findWorkspaceRoot(parentDirectory)
}
func getBazelVersion() (string, error) {
// Check in this order:
// - env var "USE_BAZEL_VERSION" is set to a specific version.
// - env var "USE_NIGHTLY_BAZEL" or "USE_BAZEL_NIGHTLY" is set -> latest
// nightly. (TODO)
// - env var "USE_CANARY_BAZEL" or "USE_BAZEL_CANARY" is set -> latest
// rc. (TODO)
// - the file workspace_root/tools/bazel exists -> that version. (TODO)
// - workspace_root/.bazelversion exists -> read contents, that version.
// - workspace_root/WORKSPACE contains a version -> that version. (TODO)
// - fallback: latest release
bazelVersion := os.Getenv("USE_BAZEL_VERSION")
if len(bazelVersion) != 0 {
return bazelVersion, nil
}
workingDirectory, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("could not get working directory: %v", err)
}
workspaceRoot := findWorkspaceRoot(workingDirectory)
if len(workspaceRoot) != 0 {
bazelVersionPath := filepath.Join(workspaceRoot, ".bazelversion")
if _, err := os.Stat(bazelVersionPath); err == nil {
f, err := os.Open(bazelVersionPath)
if err != nil {
return "", fmt.Errorf("could not read %s: %v", bazelVersionPath, err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Scan()
bazelVersion := scanner.Text()
if err := scanner.Err(); err != nil {
return "", fmt.Errorf("could not read version from file %s: %v", bazelVersion, err)
}
if len(bazelVersion) != 0 {
return bazelVersion, nil
}
}
}
return "latest", nil
}
func parseBazelForkAndVersion(bazelForkAndVersion string) (string, string, error) {
var bazelFork, bazelVersion string
versionInfo := strings.Split(bazelForkAndVersion, "/")
if len(versionInfo) == 1 {
bazelFork, bazelVersion = bazelUpstream, versionInfo[0]
} else if len(versionInfo) == 2 {
bazelFork, bazelVersion = versionInfo[0], versionInfo[1]
} else {
return "", "", fmt.Errorf("invalid version \"%s\", could not parse version with more than one slash", bazelForkAndVersion)
}
return bazelFork, bazelVersion, nil
}
type release struct {
TagName string `json:"tag_name"`
Prerelease bool `json:"prerelease"`
}
func readRemoteFile(url string, token string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %v", err)
}
if token != "" {
req.Header.Set("Authorization", "token "+token)
}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("could not fetch %s: %v", url, err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code while reading %s: %v", url, res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read content at %s: %v", url, err)
}
return body, nil
}
// maybeDownload will download a file from the given url and cache the result under bazeliskHome.
// It skips the download if the file already exists and is not outdated.
// description is used only to provide better error messages.
func maybeDownload(bazeliskHome, url, filename, description string) ([]byte, error) {
cachePath := filepath.Join(bazeliskHome, filename)
if cacheStat, err := os.Stat(cachePath); err == nil {
if time.Since(cacheStat.ModTime()).Hours() < 1 {
res, err := ioutil.ReadFile(cachePath)
if err != nil {
return nil, fmt.Errorf("could not read %s: %v", cachePath, err)
}
return res, nil
}
}
// We could also use go-github here, but I can't get it to build with Bazel's rules_go and it pulls in a lot of dependencies.
body, err := readRemoteFile(url, os.Getenv("BAZELISK_GITHUB_TOKEN"))
if err != nil {
return nil, fmt.Errorf("could not download %s: %v", description, err)
}
err = ioutil.WriteFile(cachePath, body, 0666)
if err != nil {
return nil, fmt.Errorf("could not create %s: %v", cachePath, err)
}
return body, nil
}
func resolveLatestVersion(bazeliskHome, bazelFork string, offset int) (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/bazel/releases", bazelFork)
releasesJSON, err := maybeDownload(bazeliskHome, url, bazelFork+"-releases.json", "list of Bazel releases from github.com/"+bazelFork)
if err != nil {
return "", fmt.Errorf("could not get releases from github.com/%s/bazel: %v", bazelFork, err)
}
var releases []release
if err := json.Unmarshal(releasesJSON, &releases); err != nil {
return "", fmt.Errorf("could not parse JSON into list of releases: %v", err)
}
var tags []string
for _, release := range releases {
if release.Prerelease {
continue
}
tags = append(tags, release.TagName)
}
return getNthMostRecentVersion(tags, offset)
}
func getNthMostRecentVersion(versions []string, offset int) (string, error) {
if offset >= len(versions) {
return "", fmt.Errorf("cannot resolve version \"latest-%d\": There are only %d Bazel versions", offset, len(versions))
}
wrappers := make([]*version.Version, len(versions))
for i, v := range versions {
wrapper, err := version.NewVersion(v)
if err != nil {
log.Printf("WARN: Could not parse version: %s", v)
}
wrappers[i] = wrapper
}
sort.Sort(version.Collection(wrappers))
return wrappers[len(wrappers)-1-offset].Original(), nil
}
type gcsListResponse struct {
Prefixes []string `json:"prefixes"`
}
func resolveLatestRcVersion() (string, error) {
versions, err := listDirectoriesInReleaseBucket("")
if err != nil {
return "", fmt.Errorf("could not list Bazel versions in GCS bucket: %v", err)
}
latestVersion, err := getHighestBazelVersion(versions)
if err != nil {
return "", fmt.Errorf("got invalid version number: %v", err)
}
// Append slash to match directories
rcVersions, err := listDirectoriesInReleaseBucket(latestVersion + "/")
if err != nil {
return "", fmt.Errorf("could not list release candidates for latest release: %v", err)
}
return getHighestRcVersion(rcVersions)
}
func listDirectoriesInReleaseBucket(prefix string) ([]string, error) {
url := "https://www.googleapis.com/storage/v1/b/bazel/o?delimiter=/"
if prefix != "" {
url = fmt.Sprintf("%s&prefix=%s", url, prefix)
}
content, err := readRemoteFile(url, "")
if err != nil {
return nil, fmt.Errorf("could not list GCS objects at %s: %v", url, err)
}
var response gcsListResponse
if err := json.Unmarshal(content, &response); err != nil {
return nil, fmt.Errorf("could not parse GCS index JSON: %v", err)
}
return response.Prefixes, nil
}
func getHighestBazelVersion(versions []string) (string, error) {
for i, v := range versions {
versions[i] = strings.TrimSuffix(v, "/")
}
return getNthMostRecentVersion(versions, 0)
}
func getHighestRcVersion(versions []string) (string, error) {
var version string
var lastRc int
re := regexp.MustCompile(`(\d+.\d+.\d+)/rc(\d+)/`)
for _, v := range versions {
// Fallback: use latest release if there is no active RC.
if strings.Index(v, "release") > -1 {
return strings.Split(v, "/")[0], nil
}
m := re.FindStringSubmatch(v)
version = m[1]
rc, err := strconv.Atoi(m[2])
if err != nil {
return "", fmt.Errorf("Invalid version number %s: %v", strings.TrimSuffix(v, "/"), err)
}
if rc > lastRc {
lastRc = rc
}
}
return fmt.Sprintf("%src%d", version, lastRc), nil
}
func resolveVersionLabel(bazeliskHome, bazelFork, bazelVersion string) (string, bool, error) {
if bazelFork == bazelUpstream {
// Returns three values:
// 1. The label of a Blaze release (if the label resolves to a release) or a commit (for unreleased binaries),
// 2. Whether the first value refers to a commit,
// 3. An error.
lastGreenCommitPathSuffixes := map[string]string{
"last_green": "github.com/bazelbuild/bazel.git/bazel-bazel",
"last_downstream_green": "downstream_pipeline",
}
if pathSuffix, ok := lastGreenCommitPathSuffixes[bazelVersion]; ok {
commit, err := getLastGreenCommit(pathSuffix)
if err != nil {
return "", false, fmt.Errorf("cannot resolve last green commit: %v", err)
}
return commit, true, nil
}
if bazelVersion == "last_rc" {
version, err := resolveLatestRcVersion()
return version, false, err
}
}
r := regexp.MustCompile(`^latest(?:-(?P<offset>\d+))?$`)
match := r.FindStringSubmatch(bazelVersion)
if match != nil {
offset := 0
if match[1] != "" {
var err error
offset, err = strconv.Atoi(match[1])
if err != nil {
return "", false, fmt.Errorf("invalid version \"%s\", could not parse offset: %v", bazelVersion, err)
}
}
version, err := resolveLatestVersion(bazeliskHome, bazelFork, offset)
return version, false, err
}
return bazelVersion, false, nil
}
const lastGreenBasePath = "https://storage.googleapis.com/bazel-untrusted-builds/last_green_commit/"
func getLastGreenCommit(pathSuffix string) (string, error) {
content, err := readRemoteFile(lastGreenBasePath+pathSuffix, "")
if err != nil {
return "", fmt.Errorf("could not determine last green commit: %v", err)
}
return strings.TrimSpace(string(content)), nil
}
func determineBazelFilename(version string) (string, error) {
var machineName string
switch runtime.GOARCH {
case "amd64":
machineName = "x86_64"
default:
return "", fmt.Errorf("unsupported machine architecture \"%s\", must be x86_64", runtime.GOARCH)
}
var osName string
switch runtime.GOOS {
case "darwin", "linux", "windows":
osName = runtime.GOOS
default:
return "", fmt.Errorf("unsupported operating system \"%s\", must be Linux, macOS or Windows", runtime.GOOS)
}
filenameSuffix := ""
if runtime.GOOS == "windows" {
filenameSuffix = ".exe"
}
return fmt.Sprintf("bazel-%s-%s-%s%s", version, osName, machineName, filenameSuffix), nil
}
func determineURL(fork string, version string, isCommit bool, filename string) string {
if isCommit {
var platforms = map[string]string{"darwin": "macos", "linux": "ubuntu1404", "windows": "windows"}
// No need to check the OS thanks to determineBazelFilename().
log.Printf("Using unreleased version at commit %s", version)
return fmt.Sprintf("https://storage.googleapis.com/bazel-builds/artifacts/%s/%s/bazel", platforms[runtime.GOOS], version)
}
kind := "release"
if strings.Contains(version, "rc") {
versionComponents := strings.Split(version, "rc")
// Replace version with the part before rc
version = versionComponents[0]
kind = "rc" + versionComponents[1]
}
if fork == bazelUpstream {
return fmt.Sprintf("https://releases.bazel.build/%s/%s/%s", version, kind, filename)
}
return fmt.Sprintf("https://github.com/%s/bazel/releases/download/%s/%s", fork, version, filename)
}
func downloadBazel(fork string, version string, isCommit bool, directory string) (string, error) {
filename, err := determineBazelFilename(version)
if err != nil {
return "", fmt.Errorf("could not determine filename to use for Bazel binary: %v", err)
}
url := determineURL(fork, version, isCommit, filename)
destinationPath := filepath.Join(directory, filename)
if _, err := os.Stat(destinationPath); err != nil {
tmpfile, err := ioutil.TempFile(directory, "download")
if err != nil {
return "", fmt.Errorf("could not create temporary file: %v", err)
}
defer func() {
err := tmpfile.Close()
if err == nil {
os.Remove(tmpfile.Name())
}
}()
log.Printf("Downloading %s...", url)
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("HTTP GET %s failed: %v", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP GET %s failed with error %v", url, resp.StatusCode)
}
_, err = io.Copy(tmpfile, resp.Body)
if err != nil {
return "", fmt.Errorf("could not copy from %s to %s: %v", url, tmpfile.Name(), err)
}
err = os.Chmod(tmpfile.Name(), 0755)
if err != nil {
return "", fmt.Errorf("could not chmod file %s: %v", tmpfile.Name(), err)
}
tmpfile.Close()
err = os.Rename(tmpfile.Name(), destinationPath)
if err != nil {
return "", fmt.Errorf("could not move %s to %s: %v", tmpfile.Name(), destinationPath, err)
}
}
return destinationPath, nil
}
func maybeDelegateToWrapper(bazel string) string {
if os.Getenv(skipWrapperEnv) != "" {
return bazel
}
wd, err := os.Getwd()
if err != nil {
return bazel
}
root := findWorkspaceRoot(wd)
wrapper := filepath.Join(root, wrapperPath)
if stat, err := os.Stat(wrapper); err != nil || stat.Mode().Perm()&0001 == 0 {
return bazel
}
return wrapper
}
func runBazel(bazel string, args []string) (int, error) {
execPath := maybeDelegateToWrapper(bazel)
if execPath != bazel {
os.Setenv(bazelReal, bazel)
}
cmd := exec.Command(execPath, args...)
cmd.Env = append(os.Environ(), skipWrapperEnv+"=true")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return 1, fmt.Errorf("could not start Bazel: %v", err)
}
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
if runtime.GOOS != "windows" {
cmd.Process.Signal(s)
} else {
cmd.Process.Kill()
}
}()
err = cmd.Wait()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
return waitStatus.ExitStatus(), nil
}
return 1, fmt.Errorf("could not launch Bazel: %v", err)
}
return 0, nil
}
type label struct {
Name string `json:"name"`
}
type issue struct {
Title string `json:"title"`
URL string `json:"html_url"`
Labels []label `json:"labels"`
}
type issueList struct {
Items []issue `json:"items"`
}
type flagDetails struct {
Name string
ReleaseToFlip string
IssueURL string
}
func (f *flagDetails) String() string {
return fmt.Sprintf("%s (Bazel %s: %s)", f.Name, f.ReleaseToFlip, f.IssueURL)
}
func getIncompatibleFlags(bazeliskHome, resolvedBazelVersion string) (map[string]*flagDetails, error) {
result := make(map[string]*flagDetails)
// GitHub labels use only major and minor version, we ignore the patch number (and any other suffix).
re := regexp.MustCompile(`^\d+\.\d+`)
version := re.FindString(resolvedBazelVersion)
if len(version) == 0 {
return nil, fmt.Errorf("invalid version %v", resolvedBazelVersion)
}
url := "https://api.github.com/search/issues?per_page=100&q=repo:bazelbuild/bazel+label:migration-" + version
issuesJSON, err := maybeDownload(bazeliskHome, url, "flags-"+version, "list of flags from GitHub")
if err != nil {
return nil, fmt.Errorf("could not get issues from GitHub: %v", err)
}
var issueList issueList
if err := json.Unmarshal(issuesJSON, &issueList); err != nil {
return nil, fmt.Errorf("could not parse JSON into list of issues: %v", err)
}
re = regexp.MustCompile(`^incompatible_\w+`)
for _, issue := range issueList.Items {
flag := re.FindString(issue.Title)
if len(flag) > 0 {
name := "--" + flag
result[name] = &flagDetails{
Name: name,
ReleaseToFlip: getBreakingRelease(issue.Labels),
IssueURL: issue.URL,
}
}
}
return result, nil
}
func getBreakingRelease(labels []label) string {
for _, l := range labels {
if release := strings.TrimPrefix(l.Name, "breaking-change-"); release != l.Name {
return release
}
}
return "TBD"
}
// insertArgs will insert newArgs in baseArgs. If baseArgs contains the
// "--" argument, newArgs will be inserted before that. Otherwise, newArgs
// is appended.
func insertArgs(baseArgs []string, newArgs []string) []string {
var result []string
inserted := false
for _, arg := range baseArgs {
if !inserted && arg == "--" {
result = append(result, newArgs...)
inserted = true
}
result = append(result, arg)
}
if !inserted {
result = append(result, newArgs...)
}
return result
}
func shutdownIfNeeded(bazelPath string) {
bazeliskClean := os.Getenv("BAZELISK_SHUTDOWN")
if len(bazeliskClean) == 0 {
return
}
fmt.Printf("bazel shutdown\n")
exitCode, err := runBazel(bazelPath, []string{"shutdown"})
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run bazel shutdown: %v", err)
}
if exitCode != 0 {
fmt.Printf("Failure: shutdown command failed.\n")
os.Exit(exitCode)
}
}
func cleanIfNeeded(bazelPath string) {
bazeliskClean := os.Getenv("BAZELISK_CLEAN")
if len(bazeliskClean) == 0 {
return
}
fmt.Printf("bazel clean --expunge\n")
exitCode, err := runBazel(bazelPath, []string{"clean", "--expunge"})
fmt.Printf("\n")
if err != nil {
log.Fatalf("failed to run clean: %v", err)
}
if exitCode != 0 {
fmt.Printf("Failure: clean command failed.\n")
os.Exit(exitCode)
}
}
// migrate will run Bazel with each newArgs separately and report which ones are failing.
func migrate(bazelPath string, baseArgs []string, flags map[string]*flagDetails) {
newArgs := getSortedKeys(flags)
// 1. Try with all the flags.
args := insertArgs(baseArgs, newArgs)
fmt.Printf("\n\n--- Running Bazel with all incompatible flags\n\n")
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err := runBazel(bazelPath, args)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
if exitCode == 0 {
fmt.Printf("Success: No migration needed.\n")
os.Exit(0)
}
// 2. Try with no flags, as a sanity check.
args = baseArgs
fmt.Printf("\n\n--- Running Bazel with no incompatible flags\n\n")
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
if exitCode != 0 {
fmt.Printf("Failure: Command failed, even without incompatible flags.\n")
os.Exit(exitCode)
}
// 3. Try with each flag separately.
var passList []string
var failList []string
for _, arg := range newArgs {
args = insertArgs(baseArgs, []string{arg})
fmt.Printf("\n\n--- Running Bazel with %s\n\n", arg)
shutdownIfNeeded(bazelPath)
cleanIfNeeded(bazelPath)
fmt.Printf("bazel %s\n", strings.Join(args, " "))
exitCode, err = runBazel(bazelPath, args)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
if exitCode == 0 {
passList = append(passList, arg)
} else {
failList = append(failList, arg)
}
}
print := func(l []string) {
for _, arg := range l {
fmt.Printf(" %s\n", flags[arg])
}
}
// 4. Print report
fmt.Printf("\n\n+++ Result\n\n")
fmt.Printf("Command was successful with the following flags:\n")
print(passList)
fmt.Printf("\n")
fmt.Printf("Migration is needed for the following flags:\n")
print(failList)
os.Exit(1)
}
func main() {
bazeliskHome := os.Getenv("BAZELISK_HOME")
if len(bazeliskHome) == 0 {
userCacheDir, err := os.UserCacheDir()
if err != nil {
log.Fatalf("could not get the user's cache directory: %v", err)
}
bazeliskHome = filepath.Join(userCacheDir, "bazelisk")
}
err := os.MkdirAll(bazeliskHome, 0755)
if err != nil {
log.Fatalf("could not create directory %s: %v", bazeliskHome, err)
}
bazelVersionString, err := getBazelVersion()
if err != nil {
log.Fatalf("could not get Bazel version: %v", err)
}
bazelPath, err := homedir.Expand(bazelVersionString)
if err != nil {
log.Fatalf("could not expand home directory in path: %v", err)
}
// If the Bazel version is an absolute path to a Bazel binary in the filesystem, we can
// use it directly. In that case, we don't know which exact version it is, though.
resolvedBazelVersion := "unknown"
isCommit := false
// If we aren't using a local Bazel binary, we'll have to parse the version string and
// download the version that the user wants.
if !filepath.IsAbs(bazelPath) {
bazelFork, bazelVersion, err := parseBazelForkAndVersion(bazelVersionString)
if err != nil {
log.Fatalf("could not parse Bazel fork and version: %v", err)
}
resolvedBazelVersion, isCommit, err = resolveVersionLabel(bazeliskHome, bazelFork, bazelVersion)
if err != nil {
log.Fatalf("could not resolve the version '%s' to an actual version number: %v", bazelVersion, err)
}
bazelDirectory := filepath.Join(bazeliskHome, "bin", bazelFork)
err = os.MkdirAll(bazelDirectory, 0755)
if err != nil {
log.Fatalf("could not create directory %s: %v", bazelDirectory, err)
}
bazelPath, err = downloadBazel(bazelFork, resolvedBazelVersion, isCommit, bazelDirectory)
if err != nil {
log.Fatalf("could not download Bazel: %v", err)
}
}
args := os.Args[1:]
// --strict and --migrate must be the first argument.
if len(args) > 0 && (args[0] == "--strict" || args[0] == "--migrate") {
cmd := args[0]
newFlags, err := getIncompatibleFlags(bazeliskHome, resolvedBazelVersion)
if err != nil {
log.Fatalf("could not get the list of incompatible flags: %v", err)
}
if cmd == "--migrate" {
migrate(bazelPath, args[1:], newFlags)
} else {
// When --strict is present, it expands to the list of --incompatible_ flags
// that should be enabled for the given Bazel version.
args = insertArgs(args[1:], getSortedKeys(newFlags))
}
}
// print bazelisk version information if "version" is the first argument
// bazel version is executed after this command
if len(args) > 0 && args[0] == "version" {
// Check if the --gnu_format flag is set, if that is the case,
// the version is printed differently
var gnuFormat bool
for _, arg := range args {
if arg == "--gnu_format" {
gnuFormat = true
break
}
}
if gnuFormat {
fmt.Printf("Bazelisk %s\n", BazeliskVersion)
} else {
fmt.Printf("Bazelisk version: %s\n", BazeliskVersion)
}
}
exitCode, err := runBazel(bazelPath, args)
if err != nil {
log.Fatalf("could not run Bazel: %v", err)
}
os.Exit(exitCode)
}
func getSortedKeys(data map[string]*flagDetails) []string {
result := make([]string, 0)
for key, _ := range data {
result = append(result, key)
}
sort.Strings(result)
return result
}