Skip to content

Commit

Permalink
refactor: drop golang.org/x/tools/go/vcs dependency (elastic#40016)
Browse files Browse the repository at this point in the history
* refactor: drop golang.org/x/tools/go/vcs dependency

golang.org/x/tools/go/vcs is deprecated and was initially developed before module
support was added.
However we are not using vcs.Cmd at all, we're using one method to retrieve the
vcs being used. As a quick fix, we can copy the method since it's short and
self-contained and we don't need the whole package.

* notice: regenerate notice file

* lint: replace deprecated ioutil package

* lint: preallocate srcDirs

* lint: replace deprecated strings.Title

* lint: more obvious preallocation to solve linting errors

* docs: add comment about fromDir origin

* fix: use cases.Title correctly to resolve template errors

* fix: run caser with NoLower option to maintain the same behaviour
  • Loading branch information
kruskall authored Jul 8, 2024
1 parent f0401c6 commit a7bd3bb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 51 deletions.
37 changes: 0 additions & 37 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24826,43 +24826,6 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : golang.org/x/tools/go/vcs
Version: v0.1.0-deprecated
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/golang.org/x/tools/go/[email protected]/LICENSE:

Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


--------------------------------------------------------------------------------
Dependency : google.golang.org/api
Version: v0.128.0
Expand Down
86 changes: 75 additions & 11 deletions dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -32,7 +31,8 @@ import (
"time"

"github.com/magefile/mage/sh"
"golang.org/x/tools/go/vcs"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)
Expand Down Expand Up @@ -85,6 +85,8 @@ var (
versionQualified bool
versionQualifier string

caser = cases.Title(language.English, cases.NoLower)

FuncMap = map[string]interface{}{
"beat_doc_branch": BeatDocBranch,
"beat_version": BeatQualifiedVersion,
Expand All @@ -94,7 +96,7 @@ var (
"elastic_beats_dir": ElasticBeatsDir,
"go_version": GoVersion,
"repo": GetProjectRepoInfo,
"title": strings.Title,
"title": caser.String,
"tolower": strings.ToLower,
"contains": strings.Contains,
}
Expand Down Expand Up @@ -491,7 +493,7 @@ func (s *BuildVariableSources) GetBeatVersion() (string, error) {
return "", err
}

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("failed to read beat version file=%v: %w", file, err)
}
Expand All @@ -509,7 +511,7 @@ func (s *BuildVariableSources) GetGoVersion() (string, error) {
return "", err
}

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("failed to read go version file=%v: %w", file, err)
}
Expand All @@ -527,7 +529,7 @@ func (s *BuildVariableSources) GetDocBranch() (string, error) {
return "", err
}

data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("failed to read doc branch file=%v: %w", file, err)
}
Expand Down Expand Up @@ -688,7 +690,7 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
}

for _, srcDir := range srcDirs {
_, root, err := vcs.FromDir(cwd, srcDir)
root, err := fromDir(cwd, srcDir)
if err != nil {
// Try the next gopath.
errs = append(errs, err.Error())
Expand Down Expand Up @@ -721,6 +723,66 @@ func getProjectRepoInfoUnderGopath() (*ProjectRepoInfo, error) {
}, nil
}

var vcsList = []string{
"hg",
"git",
"svn",
"bzr",
}

// this method has been adapted from vcs.FromDir from golang.org/x/tools/go/vcs
//
// the body of the method was kept as is but the extra return value
// has been removed since it was unused.
func fromDir(dir, srcRoot string) (root string, err error) {
// Clean and double-check that dir is in (a subdirectory of) srcRoot.
dir = filepath.Clean(dir)
srcRoot = filepath.Clean(srcRoot)
if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {
return "", fmt.Errorf("directory %q is outside source root %q", dir, srcRoot)
}

var vcsRet string
var rootRet string

origDir := dir
for len(dir) > len(srcRoot) {
for _, vcs := range vcsList {
if _, err := os.Stat(filepath.Join(dir, "."+vcs)); err == nil {
root := filepath.ToSlash(dir[len(srcRoot)+1:])
// Record first VCS we find, but keep looking,
// to detect mistakes like one kind of VCS inside another.
if vcsRet == "" {
vcsRet = vcs
rootRet = root
continue
}
// Allow .git inside .git, which can arise due to submodules.
if vcsRet == vcs && vcs == "git" {
continue
}
// Otherwise, we have one VCS inside a different VCS.
return "", fmt.Errorf("directory %q uses %s, but parent %q uses %s",
filepath.Join(srcRoot, rootRet), vcsRet, filepath.Join(srcRoot, root), vcs)
}
}

// Move to parent.
ndir := filepath.Dir(dir)
if len(ndir) >= len(dir) {
// Shouldn't happen, but just in case, stop.
break
}
dir = ndir
}

if vcsRet != "" {
return rootRet, nil
}

return "", fmt.Errorf("directory %q is not using a known version control system", origDir)
}

func extractCanonicalRootImportPath(rootImportPath string) string {
// In order to be compatible with go modules, the root import
// path of any module at major version v2 or higher must include
Expand All @@ -734,12 +796,14 @@ func extractCanonicalRootImportPath(rootImportPath string) string {
}

func listSrcGOPATHs() ([]string, error) {
gopaths := filepath.SplitList(build.Default.GOPATH)

var (
cwd = CWD()
errs []string
srcDirs []string
cwd = CWD()
errs []string
)
for _, gopath := range filepath.SplitList(build.Default.GOPATH) {
srcDirs := make([]string, 0, len(gopaths))
for _, gopath := range gopaths {
gopath = filepath.Clean(gopath)

if !strings.HasPrefix(cwd, gopath) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ require (
go.elastic.co/apm/module/apmhttp/v2 v2.6.0
go.elastic.co/apm/v2 v2.6.0
go.mongodb.org/mongo-driver v1.5.1
golang.org/x/tools/go/vcs v0.1.0-deprecated
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2223,8 +2223,6 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools/go/vcs v0.1.0-deprecated h1:cOIJqWBl99H1dH5LWizPa+0ImeeJq3t3cJjaeOWUAL4=
golang.org/x/tools/go/vcs v0.1.0-deprecated/go.mod h1:zUrvATBAvEI9535oC0yWYsLsHIV4Z7g63sNPVMtuBy8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down

0 comments on commit a7bd3bb

Please sign in to comment.