version.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright 2018 The Bazel Authors. All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package main
  13. import (
  14. "io/ioutil"
  15. "log"
  16. "path/filepath"
  17. "regexp"
  18. "github.com/bazelbuild/bazel-gazelle/internal/config"
  19. "github.com/bazelbuild/bazel-gazelle/internal/repos"
  20. "github.com/bazelbuild/bazel-gazelle/internal/version"
  21. )
  22. var minimumRulesGoVersion = version.Version{0, 13, 0}
  23. // checkRulesGoVersion checks whether a compatible version of rules_go is
  24. // being used in the workspace. A message will be logged if an incompatible
  25. // version is found.
  26. //
  27. // Note that we can't always determine the version of rules_go in use. Also,
  28. // if we find an incompatible version, we shouldn't bail out since the
  29. // incompatibility may not matter in the current workspace.
  30. func checkRulesGoVersion(repoRoot string) {
  31. const message = `Gazelle may not be compatible with this version of rules_go.
  32. Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
  33. rulesGoPath, err := repos.FindExternalRepo(repoRoot, config.RulesGoRepoName)
  34. if err != nil {
  35. return
  36. }
  37. defBzlPath := filepath.Join(rulesGoPath, "go", "def.bzl")
  38. defBzlContent, err := ioutil.ReadFile(defBzlPath)
  39. if err != nil {
  40. return
  41. }
  42. versionRe := regexp.MustCompile(`(?m)^RULES_GO_VERSION = ['"]([0-9.]*)['"]`)
  43. match := versionRe.FindSubmatch(defBzlContent)
  44. if match == nil {
  45. log.Printf("RULES_GO_VERSION not found in @%s//go:def.bzl.\n%s", config.RulesGoRepoName, message)
  46. return
  47. }
  48. vstr := string(match[1])
  49. v, err := version.ParseVersion(vstr)
  50. if err != nil {
  51. log.Printf("RULES_GO_VERSION %q could not be parsed in @%s//go:def.bzl.\n%s", vstr, config.RulesGoRepoName, message)
  52. }
  53. if v.Compare(minimumRulesGoVersion) < 0 {
  54. log.Printf("Found RULES_GO_VERSION %s. Minimum compatible version is %s.\n%s", v, minimumRulesGoVersion, message)
  55. }
  56. }