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

Commit bacd8dc

Browse files
authored
Merge pull request #1883 from sdboyer/explicit-root
dep: Allow explicitly setting the project root via an env var
2 parents c2af771 + 5e1f5d4 commit bacd8dc

File tree

6 files changed

+124
-19
lines changed

6 files changed

+124
-19
lines changed

CHANGELOG.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22

33
NEW FEATURES:
44

5-
* Add CI tests against go1.10. Drop support for go1.8. ([#1620](https://github.com/golang/dep/pull/1620))
6-
* Added `install.sh` script. ([#1533](https://github.com/golang/dep/pull/1533))
7-
* List out of date projects in dep status. ([#1553](https://github.com/golang/dep/pull/1553)).
8-
* Enabled opt-in persistent caching via $DEPCACHEAGE env var. ([#1711](https://github.com/golang/dep/pull/1711))
5+
* Add CI tests against go1.10. Drop support for go1.8. ([#1620](https://github.com/golang/dep/pull/1620)).
6+
* Added `install.sh` script. ([#1533](https://github.com/golang/dep/pull/1533)).
7+
* List out of date projects in dep status ([#1553](https://github.com/golang/dep/pull/1553)).
8+
* Enabled opt-in persistent caching via `DEPCACHEAGE` env var. ([#1711](https://github.com/golang/dep/pull/1711)).
9+
* Allow `DEPPROJECTROOT` [environment variable](https://golang.github.io/dep/docs/env-vars.html#depprojectroot) to supersede GOPATH deduction and explicitly set the current project's [root](https://golang.github.io/dep/docs/glossary.html#project-root) ([#1883](https://github.com/golang/dep/pull/1883)).
910

1011
BUG FIXES:
1112

1213
* Excise certain git-reltaed environment variables. ([#1872](https://github.com/golang/dep/pull/1872))
1314

1415
IMPROVEMENTS:
1516

16-
* Add template operations support in dep status template output.
17-
([#1549](https://github.com/golang/dep/pull/1549)).
18-
* Reduce network access by trusting local source information and only pulling
19-
from upstream when necessary ([#1250](https://github.com/golang/dep/pull/1250)).
17+
* Add template operations support in dep status template output ([#1549](https://github.com/golang/dep/pull/1549)).
18+
* Reduce network access by trusting local source information and only pulling from upstream when necessary ([#1250](https://github.com/golang/dep/pull/1250)).
2019
* Update our dependency on Masterminds/semver to follow upstream again now that [Masterminds/semver#67](https://github.com/Masterminds/semver/pull/67) is merged([#1792](https://github.com/golang/dep/pull/1792)).
2120

2221
WIP:
23-
* Enable importing external configuration from dependencies during init (#1277). This
24-
is feature flagged and disabled by default.
22+
* Enable importing external configuration from dependencies during init (#1277). This is feature flagged and disabled by default.
2523

2624
# v0.4.1
2725

context.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Ctx struct {
3838
WorkingDir string // Where to execute.
3939
GOPATH string // Selected Go path, containing WorkingDir.
4040
GOPATHs []string // Other Go paths.
41+
ExplicitRoot string // An explicitly-set path to use as the project root.
4142
Out, Err *log.Logger // Required loggers.
4243
Verbose bool // Enables more verbose logging.
4344
DisableLocking bool // When set, no lock file will be created to protect against simultaneous dep processes.
@@ -63,6 +64,8 @@ func (c *Ctx) SetPaths(wd string, GOPATHs ...string) error {
6364

6465
c.GOPATHs = append(c.GOPATHs, GOPATHs...)
6566

67+
c.ExplicitRoot = os.Getenv("DEPPROJECTROOT")
68+
6669
return nil
6770
}
6871

@@ -137,11 +140,15 @@ func (c *Ctx) LoadProject() (*Project, error) {
137140
return nil, err
138141
}
139142

140-
ip, err := c.ImportForAbs(p.AbsRoot)
141-
if err != nil {
142-
return nil, errors.Wrap(err, "root project import")
143+
if c.ExplicitRoot != "" {
144+
p.ImportRoot = gps.ProjectRoot(c.ExplicitRoot)
145+
} else {
146+
ip, err := c.ImportForAbs(p.AbsRoot)
147+
if err != nil {
148+
return nil, errors.Wrap(err, "root project import")
149+
}
150+
p.ImportRoot = gps.ProjectRoot(ip)
143151
}
144-
p.ImportRoot = gps.ProjectRoot(ip)
145152

146153
mp := filepath.Join(p.AbsRoot, ManifestName)
147154
mf, err := os.Open(mp)
@@ -202,9 +209,14 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
202209
return "", errors.New("project AbsRoot and ResolvedAbsRoot must be set to detect GOPATH")
203210
}
204211

212+
if c.ExplicitRoot != "" {
213+
// If an explicit root is set, just use the first GOPATH in the list.
214+
return c.GOPATHs[0], nil
215+
}
216+
205217
pGOPATH, perr := c.detectGOPATH(p.AbsRoot)
206218

207-
// If p.AbsRoot is a not symlink, attempt to detect GOPATH for p.AbsRoot only.
219+
// If p.AbsRoot is a not a symlink, attempt to detect GOPATH for p.AbsRoot only.
208220
if equal, _ := fs.EquivalentPaths(p.AbsRoot, p.ResolvedAbsRoot); equal {
209221
return pGOPATH, perr
210222
}

context_test.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,69 @@ func TestLoadProject(t *testing.T) {
152152
}
153153
}
154154

155+
func TestExplicitRootProject(t *testing.T) {
156+
h := test.NewHelper(t)
157+
defer h.Cleanup()
158+
159+
h.TempDir(filepath.Join("src", "test1", "sub"))
160+
h.TempFile(filepath.Join("src", "test1", ManifestName), "")
161+
h.TempFile(filepath.Join("src", "test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
162+
h.TempDir(filepath.Join("src", "test2", "sub"))
163+
h.TempFile(filepath.Join("src", "test2", ManifestName), "")
164+
h.Setenv("DEP_PROJECT_ROOT", "github.com/user/module")
165+
166+
type tcase struct {
167+
name string
168+
lock bool
169+
wd string
170+
}
171+
var testcases = []tcase{
172+
{"direct", true, filepath.Join("src", "test1")},
173+
{"ascending", true, filepath.Join("src", "test1", "sub")},
174+
{"without lock", false, filepath.Join("src", "test2")},
175+
{"ascending without lock", false, filepath.Join("src", "test2", "sub")},
176+
}
177+
178+
tf := func(withGOPATH bool, tc tcase, t *testing.T) func(t *testing.T) {
179+
return func(t *testing.T) {
180+
ctx := &Ctx{
181+
Out: discardLogger(),
182+
Err: discardLogger(),
183+
}
184+
185+
var err error
186+
if withGOPATH {
187+
err = ctx.SetPaths(h.Path(tc.wd), h.Path("."))
188+
} else {
189+
err = ctx.SetPaths(h.Path(tc.wd))
190+
}
191+
if err != nil {
192+
t.Fatalf("%+v", err)
193+
}
194+
ctx.ExplicitRoot = "github.com/user/module"
195+
196+
p, err := ctx.LoadProject()
197+
switch {
198+
case err != nil:
199+
t.Fatalf("%s: LoadProject failed: %+v", tc.wd, err)
200+
case p.Manifest == nil:
201+
t.Fatalf("%s: Manifest file didn't load", tc.wd)
202+
case tc.lock && p.Lock == nil:
203+
t.Fatalf("%s: Lock file didn't load", tc.wd)
204+
case !tc.lock && p.Lock != nil:
205+
t.Fatalf("%s: Non-existent Lock file loaded", tc.wd)
206+
}
207+
}
208+
}
209+
210+
for _, tc := range testcases {
211+
t.Run(tc.name, func(t *testing.T) {
212+
t.Run("within-GOPATH", tf(true, tc, t))
213+
t.Run("outside-GOPATH", tf(false, tc, t))
214+
})
215+
}
216+
}
217+
155218
func TestLoadProjectNotFoundErrors(t *testing.T) {
156219
tg := test.NewHelper(t)
157220
defer tg.Cleanup()
@@ -313,9 +376,9 @@ func TestLoadProjectGopkgFilenames(t *testing.T) {
313376
}
314377
}
315378

316-
// TestCaseInsentitive is test for Windows. This should work even though set
379+
// TestCaseInsensitive is test for Windows. This should work even though set
317380
// difference letter cases in GOPATH.
318-
func TestCaseInsentitiveGOPATH(t *testing.T) {
381+
func TestCaseInsensitiveGOPATH(t *testing.T) {
319382
if runtime.GOOS != "windows" {
320383
t.Skip("skip this test on non-Windows")
321384
}

docs/env-vars.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
id: env-vars
3+
title: Environment Variables
4+
---
5+
6+
dep's behavior can be modified by some environment variables:
7+
8+
* [`DEPCACHEDIR`](#depcachedir)
9+
* [`DEPPROJECTROOT`](#depprojectroot)
10+
* [`DEPNOLOCK`](#depnolock)
11+
12+
Environment variables are passed through to subcommands, and therefore can be used to affect vcs (e.g. `git`) behavior.
13+
14+
---
15+
16+
### `DEPCACHEDIR`
17+
18+
Allows the user to specify a custom directory for dep's [local cache](glossary.md#local-cache) of pristine VCS source repositories. Defaults to `$GOPATH/pkg/dep`.
19+
20+
### `DEPPROJECTROOT`
21+
22+
If set, the value of this variable will be treated as the [project root](glossary.md#project-root) of the [current project](glossary.md#current-project), superseding GOPATH-based inference.
23+
24+
This is primarily useful if you're not using the standard `go` toolchain as a compiler (for example, with Bazel), as there otherwise isn't much use to operating outside of GOPATH.
25+
26+
### `DEPNOLOCK`
27+
28+
By default, dep creates an `sm.lock` file at `$DEPCACHEDIR/sm.lock` in order to
29+
prevent multiple dep processes from interacting with the [local
30+
cache](glossary.md#local-cache) simultaneously. Setting this variable will
31+
bypass that protection; no file will be created. This can be useful on certain
32+
filesystems; VirtualBox shares in particular are known to misbehave.

docs/glossary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Stands for "Go packaging solver", it is [a subtree of library-style packages wit
7575

7676
dep maintains its own, pristine set of upstream sources (so, generally, git repository clones). This is kept separate from `$GOPATH/src` so that there is no obligation to maintain disk state within `$GOPATH`, as dep frequently needs to change disk state in order to do its work.
7777

78-
By default, the local cache lives at `$GOPATH/pkg/dep`. If you have multiple `$GOPATH` entries, dep will use whichever is the logical parent of the process' working directory. Alternatively, the location can be forced via the `DEPCACHEDIR` environment variable.
78+
By default, the local cache lives at `$GOPATH/pkg/dep`. If you have multiple `$GOPATH` entries, dep will use whichever is the logical parent of the process' working directory. Alternatively, the location can be forced via the [`DEPCACHEDIR` environment variable](env-vars.md#depcachedir).
7979

8080
### Lock
8181

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"docs": {
33
"Guides": ["introduction", "installation", "new-project", "migrating", "daily-dep"],
4-
"References": ["ensure-mechanics", "failure-modes", "the-solver", "deduction", "Gopkg.toml", "Gopkg.lock", "FAQ", "glossary"]
4+
"References": ["ensure-mechanics", "failure-modes", "the-solver", "deduction", "Gopkg.toml", "Gopkg.lock", "FAQ", "env-vars", "glossary"]
55
}
66
}

0 commit comments

Comments
 (0)