Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 537b95b

Browse files
committed
Add -gopath flag to init
- Separate network mode and GOPATH mode of operations. - In Network mode, do not check GOPATH or any ondisk projects. Solve the dependency constraints completely over network.
1 parent 9e747df commit 537b95b

File tree

10 files changed

+108
-11
lines changed

10 files changed

+108
-11
lines changed

cmd/dep/init.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/golang/dep"
14+
fb "github.com/golang/dep/internal/feedback"
1415
"github.com/golang/dep/internal/fs"
1516
"github.com/golang/dep/internal/gps"
1617
"github.com/golang/dep/internal/gps/paths"
@@ -30,14 +31,19 @@ disable this behavior. The following external tools are supported: glide.
3031
Any dependencies that are not constrained by external configuration use the
3132
GOPATH analysis below.
3233
33-
The version of each dependency will reflect the current state of the GOPATH. If
34-
a dependency doesn't exist in the GOPATH, a version will be selected from the
35-
versions available from the upstream source per the following algorithm:
34+
By default, the dependencies are resolved over the network. A version will be
35+
selected from the versions available from the upstream source per the following
36+
algorithm:
3637
3738
- Tags conforming to semver (sorted by semver rules)
3839
- Default branch(es) (sorted lexicographically)
3940
- Non-semver tags (sorted lexicographically)
4041
42+
An alternate mode can be activated by passing -gopath. In this mode, the version
43+
of each dependency will reflect the current state of the GOPATH. If a dependency
44+
doesn't exist in the GOPATH, a version will be selected based on the above
45+
network version selection algorithm.
46+
4147
A Gopkg.toml file will be written with inferred version constraints for all
4248
direct dependencies. Gopkg.lock will be written with precise versions, and
4349
vendor/ will be populated with the precise versions written to Gopkg.lock.
@@ -52,11 +58,13 @@ func (cmd *initCommand) Hidden() bool { return false }
5258
func (cmd *initCommand) Register(fs *flag.FlagSet) {
5359
fs.BoolVar(&cmd.noExamples, "no-examples", false, "don't include example in Gopkg.toml")
5460
fs.BoolVar(&cmd.skipTools, "skip-tools", false, "skip importing configuration from other dependency managers")
61+
fs.BoolVar(&cmd.gopath, "gopath", false, "search in GOPATH for dependencies")
5562
}
5663

5764
type initCommand struct {
5865
noExamples bool
5966
skipTools bool
67+
gopath bool
6068
}
6169

6270
func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
@@ -119,10 +127,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
119127
if err != nil {
120128
return err
121129
}
130+
122131
gs := newGopathScanner(ctx, directDeps, sm)
123-
err = gs.InitializeRootManifestAndLock(m, l)
124-
if err != nil {
125-
return err
132+
if cmd.gopath {
133+
err = gs.InitializeRootManifestAndLock(m, l)
134+
if err != nil {
135+
return err
136+
}
126137
}
127138

128139
rootAnalyzer.skipTools = true // Don't import external config during solve for now
@@ -152,7 +163,20 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
152163
l = dep.LockFromSolution(soln)
153164

154165
rootAnalyzer.FinalizeRootManifestAndLock(m, l)
155-
gs.FinalizeRootManifestAndLock(m, l)
166+
if cmd.gopath {
167+
gs.FinalizeRootManifestAndLock(m, l)
168+
} else {
169+
// Pick the direct dependencies from the above solution and add to manifest
170+
for _, x := range l.Projects() {
171+
pr := x.Ident().ProjectRoot
172+
if _, ok := directDeps[string(pr)]; ok {
173+
m.Constraints[pr] = getProjectPropertiesFromVersion(x.Version())
174+
feedback(x.Version(), pr, fb.DepTypeDirect, ctx.Err)
175+
} else {
176+
feedback(x.Version(), pr, fb.DepTypeTransitive, ctx.Err)
177+
}
178+
}
179+
}
156180

157181
// Run gps.Prepare with appropriate constraint solutions from solve run
158182
// to generate the final lock memo.

cmd/dep/testdata/harness_tests/init/case1/testcase.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case2/testcase.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case3/testcase.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptest"
4+
version = "1.0.0"
5+
6+
[[constraint]]
7+
name = "github.com/sdboyer/deptestdos"
8+
version = "2.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foo
6+
7+
import "github.com/sdboyer/deptest"
8+
9+
func Foo() deptest.Foo {
10+
var y deptest.Foo
11+
12+
return y
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/golang/notexist/foo"
11+
"github.com/sdboyer/deptestdos"
12+
)
13+
14+
func main() {
15+
var x deptestdos.Bar
16+
y := foo.FooFunc()
17+
18+
fmt.Println(x, y)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"commands": [
3+
["init", "-no-examples"]
4+
],
5+
"gopath-initial": {
6+
"github.com/sdboyer/deptestdos": "a0196baa11ea047dd65037287451d36b861b00ea"
7+
},
8+
"vendor-final": [
9+
"github.com/sdboyer/deptest",
10+
"github.com/sdboyer/deptestdos"
11+
]
12+
}

cmd/dep/testdata/harness_tests/init/glide/case2/testcase.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
["init", "-no-examples", "-skip-tools"]
3+
["init", "-no-examples", "-skip-tools", "-gopath"]
44
],
55
"error-expected": "",
66
"gopath-initial": {

0 commit comments

Comments
 (0)