Skip to content

go/解决dep无法访问golang.org等域名的问题 #35

@kaybinwong

Description

@kaybinwong

最近在使用dep管理go的依赖时,发现无法正常使用,错误信息如下:

	cygwin-msys2: unable to deduce repository and source type for "golang.org/x/sys/unix": unable to read metadata: unable to fetch raw metadata: failed HTTP request to URL "http://golang.org/x/sys/unix?go-get=1": Get http://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:80: i/o timeout

发现原来又是被墙导致无法访问golang.org等域名,导致最终无法拉取x/net等依赖。既然发现问题,那么解决问题的办法总是有的:
1、ss、蓝灯等代理
2、手动拉取连接路径
3、使用其他工具
4、hack源码

对于解决办法1,公司不允许使用翻墙工具,那么就算了。

对于解决办法2,虽然可以临时解决问题,但是一旦需要更新是也同样会访问不了。

对于解决办法3,这个可以从根本上解决问题,但是从更新次数以及社区活跃远不如dep,所以只能想办法来hack了。

经过一番折腾后,发现golang.org的所有包都在github上有存档,那么问题就好办了,我们只需在拉取golang.org的包之前去github上拉取即可,具体步骤如下:
1、拉取dep源码

$ go get -v -u github.com/golang/dep
$ cd ~/go/src/github.com/golang/dep
$ git checkout ba8d7503e18e9022a759016d051d375ba1852d94

2、在gps/deduce.go中增加如下方法

func (dc *deductionCoordinator) deduceMirrorPaths(path string, uri *url.URL) (pd pathDeduction, ok bool) {
	schemes := []string{"https", "http"}
	mirrors := map[string]string {
		// golang.org/x
		"golang.org/x/blog": "github.com/golang/blog",
		"golang.org/x/crypto": "github.com/golang/crypto",
		"golang.org/x/exp": "github.com/golang/exp",
		"golang.org/x/image": "github.com/golang/image",
		"golang.org/x/mobile": "github.com/golang/mobile",
		"golang.org/x/net": "github.com/golang/net",
		"golang.org/x/sys": "github.com/golang/sys",
		"golang.org/x/talks": "github.com/golang/talks",
		"golang.org/x/text": "github.com/golang/text",
		"golang.org/x/tools": "github.com/golang/tools",
		// google.golang.org
		"google.golang.org/grpc": "github.com/grpc/grpc-go",
		"google.golang.org/genproto": "github.com/google/go-genproto",
	}

	for root, source := range mirrors {
		if strings.HasPrefix(path, root) {
			ok = true
			i := strings.Index(source, "/")
			uri.Host = source[:i]
			uri.Path = source[i:]
			mb := make(maybeSources, len(schemes))
			for k, scheme := range schemes {
				u := *uri
				u.Scheme = scheme
				mb[k] = maybeGitSource{url: &u}
			}
			pd = pathDeduction{
				root: root,
				mb:   mb,
			}
		}
	}
	return
}

3、修改方法deduceKnownPaths

func (dc *deductionCoordinator) deduceKnownPaths(path string) (pathDeduction, error) {
	u, path, err := normalizeURI(path)
	if err != nil {
		return pathDeduction{}, err
	}

	// First, try the root path-based matches
	if _, mtch, has := dc.deducext.LongestPrefix(path); has {
		root, err := mtch.deduceRoot(path)
		if err != nil {
			return pathDeduction{}, err
		}
		mb, err := mtch.deduceSource(path, u)
		if err != nil {
			return pathDeduction{}, err
		}

		return pathDeduction{
			root: root,
			mb:   mb,
		}, nil
	}
	
	// Try mirrors
	pd, ok := dc.deduceMirrorPaths(path, u)
	if ok {
		return pd, nil
	}
	
	// Next, try the vcs extension-based (infix) matcher
	exm := vcsExtensionDeducer{regexp: vcsExtensionRegex}
	if root, err := exm.deduceRoot(path); err == nil {
		mb, err := exm.deduceSource(path, u)
		if err != nil {
			return pathDeduction{}, err
		}

		return pathDeduction{
			root: root,
			mb:   mb,
		}, nil
	}

	return pathDeduction{}, errNoKnownPathMatch
}

另外,我已fork了一份代码,你也可以直接使用以下命令直接替换。

$ rm gps/deduce.go
$ wget https://raw.githubusercontent.com/kaybinwong/dep/master/gps/deduce.go -O gps/deduce.go
  1. 安装dep
$ go get -u github.com/golang/dep/cmd/dep

dep默认安装到~/go/bin目录下。

  1. 加入path

拷贝到/usr/bin/,或者/usr/local/bin目录下。

  1. 使用dep
$ dep init -v

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions