Handle package import paths that are in subfolders of Go module path

This commit is contained in:
Matthew Holt 2020-06-16 15:08:00 -06:00
parent 823a072ae1
commit 8266ddc40e
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
2 changed files with 20 additions and 4 deletions

View file

@ -21,6 +21,7 @@ import (
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
@ -192,11 +193,20 @@ func runDev(ctx context.Context, args []string) error {
})
}
// reconcile remaining path segments; for example if a module foo/a
// is rooted at directory path /home/foo/a, but the current directory
// is /home/foo/a/b, then the package to import should be foo/a/b
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to determine current directory: %v", err)
}
importPath := path.Join(currentModule, strings.TrimPrefix(cwd, filepath.ToSlash(moduleDir)))
// build caddy with this module plugged in
builder := xcaddy.Builder{
CaddyVersion: caddyVersion,
Plugins: []xcaddy.Dependency{
{ModulePath: currentModule},
{ModulePath: importPath},
},
Replacements: replacements,
RaceDetector: raceDetector,

View file

@ -131,10 +131,16 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
if err != nil {
return nil, err
}
nextPlugin:
for _, p := range b.Plugins {
// if module is locally available; do not "go get" it
if replaced[p.ModulePath] != "" {
continue
// if module is locally available, do not "go get" it;
// also note that we iterate and check prefixes, because
// a plugin package may be a subfolder of a module, i.e.
// foo/a/plugin is within module foo/a.
for repl := range replaced {
if strings.HasPrefix(p.ModulePath, repl) {
continue nextPlugin
}
}
err = env.execGoGet(ctx, p.ModulePath, p.Version)
if err != nil {