github.com/wolfd/bazel-gazelle@v0.14.0/cmd/gazelle/version.go (about)

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