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

Commit

Permalink
Merge pull request #1883 from sdboyer/explicit-root
Browse files Browse the repository at this point in the history
dep: Allow explicitly setting the project root via an env var
  • Loading branch information
sdboyer authored Jul 2, 2018
2 parents c2af771 + 5e1f5d4 commit bacd8dc
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 19 deletions.
18 changes: 8 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@

NEW FEATURES:

* Add CI tests against go1.10. Drop support for go1.8. ([#1620](https://github.com/golang/dep/pull/1620))
* Added `install.sh` script. ([#1533](https://github.com/golang/dep/pull/1533))
* List out of date projects in dep status. ([#1553](https://github.com/golang/dep/pull/1553)).
* Enabled opt-in persistent caching via $DEPCACHEAGE env var. ([#1711](https://github.com/golang/dep/pull/1711))
* Add CI tests against go1.10. Drop support for go1.8. ([#1620](https://github.com/golang/dep/pull/1620)).
* Added `install.sh` script. ([#1533](https://github.com/golang/dep/pull/1533)).
* List out of date projects in dep status ([#1553](https://github.com/golang/dep/pull/1553)).
* Enabled opt-in persistent caching via `DEPCACHEAGE` env var. ([#1711](https://github.com/golang/dep/pull/1711)).
* 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)).

BUG FIXES:

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

IMPROVEMENTS:

* Add template operations support in dep status template output.
([#1549](https://github.com/golang/dep/pull/1549)).
* Reduce network access by trusting local source information and only pulling
from upstream when necessary ([#1250](https://github.com/golang/dep/pull/1250)).
* Add template operations support in dep status template output ([#1549](https://github.com/golang/dep/pull/1549)).
* Reduce network access by trusting local source information and only pulling from upstream when necessary ([#1250](https://github.com/golang/dep/pull/1250)).
* 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)).

WIP:
* Enable importing external configuration from dependencies during init (#1277). This
is feature flagged and disabled by default.
* Enable importing external configuration from dependencies during init (#1277). This is feature flagged and disabled by default.

# v0.4.1

Expand Down
22 changes: 17 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Ctx struct {
WorkingDir string // Where to execute.
GOPATH string // Selected Go path, containing WorkingDir.
GOPATHs []string // Other Go paths.
ExplicitRoot string // An explicitly-set path to use as the project root.
Out, Err *log.Logger // Required loggers.
Verbose bool // Enables more verbose logging.
DisableLocking bool // When set, no lock file will be created to protect against simultaneous dep processes.
Expand All @@ -63,6 +64,8 @@ func (c *Ctx) SetPaths(wd string, GOPATHs ...string) error {

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

c.ExplicitRoot = os.Getenv("DEPPROJECTROOT")

return nil
}

Expand Down Expand Up @@ -137,11 +140,15 @@ func (c *Ctx) LoadProject() (*Project, error) {
return nil, err
}

ip, err := c.ImportForAbs(p.AbsRoot)
if err != nil {
return nil, errors.Wrap(err, "root project import")
if c.ExplicitRoot != "" {
p.ImportRoot = gps.ProjectRoot(c.ExplicitRoot)
} else {
ip, err := c.ImportForAbs(p.AbsRoot)
if err != nil {
return nil, errors.Wrap(err, "root project import")
}
p.ImportRoot = gps.ProjectRoot(ip)
}
p.ImportRoot = gps.ProjectRoot(ip)

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

if c.ExplicitRoot != "" {
// If an explicit root is set, just use the first GOPATH in the list.
return c.GOPATHs[0], nil
}

pGOPATH, perr := c.detectGOPATH(p.AbsRoot)

// If p.AbsRoot is a not symlink, attempt to detect GOPATH for p.AbsRoot only.
// If p.AbsRoot is a not a symlink, attempt to detect GOPATH for p.AbsRoot only.
if equal, _ := fs.EquivalentPaths(p.AbsRoot, p.ResolvedAbsRoot); equal {
return pGOPATH, perr
}
Expand Down
67 changes: 65 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,69 @@ func TestLoadProject(t *testing.T) {
}
}

func TestExplicitRootProject(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

h.TempDir(filepath.Join("src", "test1", "sub"))
h.TempFile(filepath.Join("src", "test1", ManifestName), "")
h.TempFile(filepath.Join("src", "test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
h.TempDir(filepath.Join("src", "test2", "sub"))
h.TempFile(filepath.Join("src", "test2", ManifestName), "")
h.Setenv("DEP_PROJECT_ROOT", "github.com/user/module")

type tcase struct {
name string
lock bool
wd string
}
var testcases = []tcase{
{"direct", true, filepath.Join("src", "test1")},
{"ascending", true, filepath.Join("src", "test1", "sub")},
{"without lock", false, filepath.Join("src", "test2")},
{"ascending without lock", false, filepath.Join("src", "test2", "sub")},
}

tf := func(withGOPATH bool, tc tcase, t *testing.T) func(t *testing.T) {
return func(t *testing.T) {
ctx := &Ctx{
Out: discardLogger(),
Err: discardLogger(),
}

var err error
if withGOPATH {
err = ctx.SetPaths(h.Path(tc.wd), h.Path("."))
} else {
err = ctx.SetPaths(h.Path(tc.wd))
}
if err != nil {
t.Fatalf("%+v", err)
}
ctx.ExplicitRoot = "github.com/user/module"

p, err := ctx.LoadProject()
switch {
case err != nil:
t.Fatalf("%s: LoadProject failed: %+v", tc.wd, err)
case p.Manifest == nil:
t.Fatalf("%s: Manifest file didn't load", tc.wd)
case tc.lock && p.Lock == nil:
t.Fatalf("%s: Lock file didn't load", tc.wd)
case !tc.lock && p.Lock != nil:
t.Fatalf("%s: Non-existent Lock file loaded", tc.wd)
}
}
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Run("within-GOPATH", tf(true, tc, t))
t.Run("outside-GOPATH", tf(false, tc, t))
})
}
}

func TestLoadProjectNotFoundErrors(t *testing.T) {
tg := test.NewHelper(t)
defer tg.Cleanup()
Expand Down Expand Up @@ -313,9 +376,9 @@ func TestLoadProjectGopkgFilenames(t *testing.T) {
}
}

// TestCaseInsentitive is test for Windows. This should work even though set
// TestCaseInsensitive is test for Windows. This should work even though set
// difference letter cases in GOPATH.
func TestCaseInsentitiveGOPATH(t *testing.T) {
func TestCaseInsensitiveGOPATH(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("skip this test on non-Windows")
}
Expand Down
32 changes: 32 additions & 0 deletions docs/env-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
id: env-vars
title: Environment Variables
---

dep's behavior can be modified by some environment variables:

* [`DEPCACHEDIR`](#depcachedir)
* [`DEPPROJECTROOT`](#depprojectroot)
* [`DEPNOLOCK`](#depnolock)

Environment variables are passed through to subcommands, and therefore can be used to affect vcs (e.g. `git`) behavior.

---

### `DEPCACHEDIR`

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`.

### `DEPPROJECTROOT`

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.

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.

### `DEPNOLOCK`

By default, dep creates an `sm.lock` file at `$DEPCACHEDIR/sm.lock` in order to
prevent multiple dep processes from interacting with the [local
cache](glossary.md#local-cache) simultaneously. Setting this variable will
bypass that protection; no file will be created. This can be useful on certain
filesystems; VirtualBox shares in particular are known to misbehave.
2 changes: 1 addition & 1 deletion docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Stands for "Go packaging solver", it is [a subtree of library-style packages wit

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.

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.
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).

### Lock

Expand Down
2 changes: 1 addition & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"docs": {
"Guides": ["introduction", "installation", "new-project", "migrating", "daily-dep"],
"References": ["ensure-mechanics", "failure-modes", "the-solver", "deduction", "Gopkg.toml", "Gopkg.lock", "FAQ", "glossary"]
"References": ["ensure-mechanics", "failure-modes", "the-solver", "deduction", "Gopkg.toml", "Gopkg.lock", "FAQ", "env-vars", "glossary"]
}
}

0 comments on commit bacd8dc

Please sign in to comment.