From cee8c91e50c94f2d5ff07cebabec763c2aa89e49 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Thu, 21 Oct 2021 17:50:18 -0700 Subject: [PATCH 1/5] mod: added support for version constraint when fetching a remote version Signed-off-by: Sam Alba --- mod/file.go | 5 +++-- mod/mod.go | 18 ++++++----------- mod/repo.go | 49 +++++++++++++++++++++++++++++++++++---------- mod/repo_test.go | 2 +- mod/require.go | 29 +++++++++++++++------------ mod/require_test.go | 2 +- state/project.go | 18 ++++++++--------- 7 files changed, 74 insertions(+), 49 deletions(-) diff --git a/mod/file.go b/mod/file.go index 947ea583..733d1051 100644 --- a/mod/file.go +++ b/mod/file.go @@ -98,7 +98,8 @@ func read(fMod, fSum io.Reader) (*file, error) { return nil, fmt.Errorf("repos in mod and sum line don't match: %s and %s", modSplit[0], sumSplit[0]) } - require, err := newRequire(modSplit[0]) + // FIXME: if we want to add support for version constraints in the mod file, it would be here + require, err := newRequire(modSplit[0], "") if err != nil { return nil, err } @@ -204,7 +205,7 @@ func (f *file) updateToLatest(req *Require) (*Require, error) { } // checkout the latest tag - latestTag, err := gitRepo.latestTag() + latestTag, err := gitRepo.latestTag(req.versionConstraint) if err != nil { return nil, err } diff --git a/mod/mod.go b/mod/mod.go index 6e7902db..85aec33e 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -6,14 +6,8 @@ import ( "github.com/gofrs/flock" ) -func InstallStdlib(workspace string) error { - _, err := Install(workspace, "alpha.dagger.io@v0.1") - - return err -} - -func Install(workspace, repoName string) (*Require, error) { - require, err := newRequire(repoName) +func Install(workspace, repoName, versionConstraint string) (*Require, error) { + require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err } @@ -51,7 +45,7 @@ func InstallAll(workspace string, repoNames []string) ([]*Require, error) { for _, repoName := range repoNames { var require *Require - if require, err = Install(workspace, repoName); err != nil { + if require, err = Install(workspace, repoName, ""); err != nil { continue } @@ -61,8 +55,8 @@ func InstallAll(workspace string, repoNames []string) ([]*Require, error) { return installedRequires, err } -func Update(workspace, repoName string) (*Require, error) { - require, err := newRequire(repoName) +func Update(workspace, repoName, versionConstraint string) (*Require, error) { + require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err } @@ -100,7 +94,7 @@ func UpdateAll(workspace string, repoNames []string) ([]*Require, error) { for _, repoName := range repoNames { var require *Require - if require, err = Update(workspace, repoName); err != nil { + if require, err = Update(workspace, repoName, ""); err != nil { continue } diff --git a/mod/repo.go b/mod/repo.go index 96182a64..09257054 100644 --- a/mod/repo.go +++ b/mod/repo.go @@ -49,7 +49,7 @@ func clone(require *Require, dir string, privateKeyFile, privateKeyPassword stri } if require.version == "" { - latestTag, err := rr.latestTag() + latestTag, err := rr.latestTag(require.versionConstraint) if err != nil { return nil, err } @@ -85,25 +85,53 @@ func (r *repo) checkout(version string) error { return nil } -func (r *repo) listTags() ([]string, error) { +func (r *repo) listTagVersions(versionConstraint string) ([]string, error) { + if versionConstraint == "" { + versionConstraint = ">= 0" + } + + constraint, err := version.NewConstraint(versionConstraint) + if err != nil { + return nil, err + } + iter, err := r.contents.Tags() if err != nil { return nil, err } var tags []string - if err := iter.ForEach(func(ref *plumbing.Reference) error { - tags = append(tags, ref.Name().Short()) + err = iter.ForEach(func(ref *plumbing.Reference) error { + tagV := ref.Name().Short() + + if !strings.HasPrefix(tagV, "v") { + // ignore wrong formatted tags + return nil + } + + v, err := version.NewVersion(tagV) + if err != nil { + // ignore invalid tag + return nil + } + + if constraint.Check(v) { + // Add tag if it matches the version constraint + tags = append(tags, ref.Name().Short()) + } + return nil - }); err != nil { + }) + + if err != nil { return nil, err } return tags, nil } -func (r *repo) latestTag() (string, error) { - versionsRaw, err := r.listTags() +func (r *repo) latestTag(versionConstraint string) (string, error) { + versionsRaw, err := r.listTagVersions(versionConstraint) if err != nil { return "", err } @@ -115,10 +143,9 @@ func (r *repo) latestTag() (string, error) { } if len(versions) == 0 { - return "", fmt.Errorf("repo doesn't have any tags") + return "", fmt.Errorf("repo doesn't have any tags matching the required version") } - sort.Sort(version.Collection(versions)) - - return versions[len(versions)-1].Original(), nil + sort.Sort(sort.Reverse(version.Collection(versions))) + return versions[0].Original(), nil } diff --git a/mod/repo_test.go b/mod/repo_test.go index cbf01fbd..1f107506 100644 --- a/mod/repo_test.go +++ b/mod/repo_test.go @@ -74,7 +74,7 @@ func TestListTags(t *testing.T) { t.Fatal(err) } - tags, err := r.listTags() + tags, err := r.listTagVersions("") if err != nil { t.Error(err) } diff --git a/mod/require.go b/mod/require.go index fb113692..50f13aba 100644 --- a/mod/require.go +++ b/mod/require.go @@ -15,16 +15,17 @@ type Require struct { cloneRepo string clonePath string - version string - checksum string + version string + versionConstraint string + checksum string } -func newRequire(repoName string) (*Require, error) { +func newRequire(repoName, versionConstraint string) (*Require, error) { switch { case strings.HasPrefix(repoName, "github.com"): - return parseGithubRepoName(repoName) + return parseGithubRepoName(repoName, versionConstraint) case strings.HasPrefix(repoName, "alpha.dagger.io"): - return parseDaggerRepoName(repoName) + return parseDaggerRepoName(repoName, versionConstraint) default: return nil, fmt.Errorf("repo name does not match suported providers") } @@ -32,7 +33,7 @@ func newRequire(repoName string) (*Require, error) { var githubRepoNameRegex = regexp.MustCompile(`(github.com/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`) -func parseGithubRepoName(repoName string) (*Require, error) { +func parseGithubRepoName(repoName, versionConstraint string) (*Require, error) { repoMatches := githubRepoNameRegex.FindStringSubmatch(repoName) if len(repoMatches) < 4 { @@ -40,9 +41,10 @@ func parseGithubRepoName(repoName string) (*Require, error) { } return &Require{ - repo: repoMatches[1], - path: repoMatches[2], - version: repoMatches[3], + repo: repoMatches[1], + path: repoMatches[2], + version: repoMatches[3], + versionConstraint: versionConstraint, cloneRepo: repoMatches[1], clonePath: repoMatches[2], @@ -51,7 +53,7 @@ func parseGithubRepoName(repoName string) (*Require, error) { var daggerRepoNameRegex = regexp.MustCompile(`alpha.dagger.io([a-zA-Z0-9/_.-]*)@?([0-9a-zA-Z.-]*)`) -func parseDaggerRepoName(repoName string) (*Require, error) { +func parseDaggerRepoName(repoName, versionConstraint string) (*Require, error) { repoMatches := daggerRepoNameRegex.FindStringSubmatch(repoName) if len(repoMatches) < 3 { @@ -59,9 +61,10 @@ func parseDaggerRepoName(repoName string) (*Require, error) { } return &Require{ - repo: "alpha.dagger.io", - path: repoMatches[1], - version: repoMatches[2], + repo: "alpha.dagger.io", + path: repoMatches[1], + version: repoMatches[2], + versionConstraint: versionConstraint, cloneRepo: "github.com/dagger/universe", clonePath: path.Join("/stdlib", repoMatches[1]), diff --git a/mod/require_test.go b/mod/require_test.go index e455456e..bdc3857a 100644 --- a/mod/require_test.go +++ b/mod/require_test.go @@ -98,7 +98,7 @@ func TestParseArgument(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - got, err := newRequire(c.in) + got, err := newRequire(c.in, "") if err != nil && c.hasError { return } diff --git a/state/project.go b/state/project.go index 274a4961..5f61d8be 100644 --- a/state/project.go +++ b/state/project.go @@ -12,6 +12,7 @@ import ( "github.com/rs/zerolog/log" "go.dagger.io/dagger/keychain" + "go.dagger.io/dagger/mod" "go.dagger.io/dagger/stdlib" "gopkg.in/yaml.v3" ) @@ -24,12 +25,13 @@ var ( ) const ( - daggerDir = ".dagger" - envDir = "env" - stateDir = "state" - planDir = "plan" - manifestFile = "values.yaml" - computedFile = "computed.json" + daggerDir = ".dagger" + envDir = "env" + stateDir = "state" + planDir = "plan" + manifestFile = "values.yaml" + computedFile = "computed.json" + universeVersionConstraint = ">= 0.1, < 0.2" ) type Project struct { @@ -396,9 +398,7 @@ func vendorUniverse(ctx context.Context, p string) error { } log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe") - // FIXME(samalba): disabled install remote stdlib temporarily - // if err := mod.InstallStdlib(p); err != nil { - if err := stdlib.Vendor(ctx, p); err != nil { + if _, err := mod.Install(p, "alpha.dagger.io", universeVersionConstraint); err != nil { return err } From d34bd86d99685e519e67ee8385f67484679a95d3 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 22 Oct 2021 10:00:21 -0700 Subject: [PATCH 2/5] mod: added unit tests for version constraint support Signed-off-by: Sam Alba --- mod/repo_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mod/repo_test.go b/mod/repo_test.go index 1f107506..efd6c747 100644 --- a/mod/repo_test.go +++ b/mod/repo_test.go @@ -83,3 +83,36 @@ func TestListTags(t *testing.T) { t.Errorf("could not list repo tags") } } + +func TestVersionConstraint(t *testing.T) { + tmpDir, err := ioutil.TempDir("", "clone") + if err != nil { + t.Fatal("error creating tmp dir") + } + defer os.Remove(tmpDir) + + r, err := clone(&Require{ + cloneRepo: "github.com/dagger/universe", + clonePath: "stdlib", + version: "", + }, tmpDir, "", "") + if err != nil { + t.Fatal(err) + } + + tagVersion, err := r.latestTag("<= 0.1.0") + if err != nil { + t.Error(err) + } + + // Make sure we select the right version based on constraint + if tagVersion != "v0.1.0" { + t.Errorf("wrong version: expected 0.1.0, got %v", tagVersion) + } + + // Make sure an invalid constraint (version out of range) returns an error + _, err = r.latestTag("> 99999") + if err == nil { + t.Error("selected wrong version based on constraint") + } +} From 779dda1aca8cf2c233bc2d075889a59924817d34 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 22 Oct 2021 11:02:34 -0700 Subject: [PATCH 3/5] mod: added basic logging + context in sub libs Signed-off-by: Sam Alba --- cmd/dagger/cmd/mod/get.go | 6 +++--- mod/mod.go | 24 ++++++++++++++++-------- state/project.go | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/dagger/cmd/mod/get.go b/cmd/dagger/cmd/mod/get.go index c4cca65b..20df6180 100644 --- a/cmd/dagger/cmd/mod/get.go +++ b/cmd/dagger/cmd/mod/get.go @@ -38,13 +38,13 @@ var getCmd = &cobra.Command{ var err error if update && len(args) == 0 { lg.Info().Msg("updating all installed packages...") - processedRequires, err = mod.UpdateInstalled(project.Path) + processedRequires, err = mod.UpdateInstalled(ctx, project.Path) } else if update && len(args) > 0 { lg.Info().Msg("updating specified packages...") - processedRequires, err = mod.UpdateAll(project.Path, args) + processedRequires, err = mod.UpdateAll(ctx, project.Path, args) } else if !update && len(args) > 0 { lg.Info().Msg("installing specified packages...") - processedRequires, err = mod.InstallAll(project.Path, args) + processedRequires, err = mod.InstallAll(ctx, project.Path, args) } else { lg.Fatal().Msg("unrecognized update/install operation") } diff --git a/mod/mod.go b/mod/mod.go index 85aec33e..57892b7e 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -1,12 +1,17 @@ package mod import ( + "context" "path" "github.com/gofrs/flock" + "github.com/rs/zerolog/log" ) -func Install(workspace, repoName, versionConstraint string) (*Require, error) { +func Install(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { + lg := log.Ctx(ctx) + + lg.Info().Str("name", repoName).Msg("installing module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -38,14 +43,14 @@ func Install(workspace, repoName, versionConstraint string) (*Require, error) { return require, nil } -func InstallAll(workspace string, repoNames []string) ([]*Require, error) { +func InstallAll(ctx context.Context, workspace string, repoNames []string) ([]*Require, error) { installedRequires := make([]*Require, 0, len(repoNames)) var err error for _, repoName := range repoNames { var require *Require - if require, err = Install(workspace, repoName, ""); err != nil { + if require, err = Install(ctx, workspace, repoName, ""); err != nil { continue } @@ -55,7 +60,10 @@ func InstallAll(workspace string, repoNames []string) ([]*Require, error) { return installedRequires, err } -func Update(workspace, repoName, versionConstraint string) (*Require, error) { +func Update(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { + lg := log.Ctx(ctx) + + lg.Info().Str("name", repoName).Msg("updating module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -87,14 +95,14 @@ func Update(workspace, repoName, versionConstraint string) (*Require, error) { return updatedRequire, nil } -func UpdateAll(workspace string, repoNames []string) ([]*Require, error) { +func UpdateAll(ctx context.Context, workspace string, repoNames []string) ([]*Require, error) { updatedRequires := make([]*Require, 0, len(repoNames)) var err error for _, repoName := range repoNames { var require *Require - if require, err = Update(workspace, repoName, ""); err != nil { + if require, err = Update(ctx, workspace, repoName, ""); err != nil { continue } @@ -104,7 +112,7 @@ func UpdateAll(workspace string, repoNames []string) ([]*Require, error) { return updatedRequires, err } -func UpdateInstalled(workspace string) ([]*Require, error) { +func UpdateInstalled(ctx context.Context, workspace string) ([]*Require, error) { modfile, err := readPath(workspace) if err != nil { return nil, err @@ -116,7 +124,7 @@ func UpdateInstalled(workspace string) ([]*Require, error) { repoNames = append(repoNames, require.String()) } - return UpdateAll(workspace, repoNames) + return UpdateAll(ctx, workspace, repoNames) } func Ensure(workspace string) error { diff --git a/state/project.go b/state/project.go index 5f61d8be..adc72da3 100644 --- a/state/project.go +++ b/state/project.go @@ -398,7 +398,7 @@ func vendorUniverse(ctx context.Context, p string) error { } log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe") - if _, err := mod.Install(p, "alpha.dagger.io", universeVersionConstraint); err != nil { + if _, err := mod.Install(ctx, p, "alpha.dagger.io", universeVersionConstraint); err != nil { return err } From 5e6d1261f768a5c3f3af71ba8a98a89c9102bdc9 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 22 Oct 2021 15:37:28 -0700 Subject: [PATCH 4/5] mod: lock the version of universe for updates of package Signed-off-by: Sam Alba --- mod/file.go | 28 +++++++++++++++------------- mod/mod.go | 27 +++++++++++++++++++++++---- mod/repo.go | 38 ++++++++++++++++++++++++++++---------- mod/repo_test.go | 17 +++++++++++------ state/project.go | 15 +++++++-------- 5 files changed, 84 insertions(+), 41 deletions(-) diff --git a/mod/file.go b/mod/file.go index 733d1051..1cd4d89b 100644 --- a/mod/file.go +++ b/mod/file.go @@ -2,6 +2,7 @@ package mod import ( "bytes" + "context" "errors" "fmt" "io" @@ -15,11 +16,13 @@ import ( "github.com/spf13/viper" ) -const modFilePath = "./cue.mod/dagger.mod" -const sumFilePath = "./cue.mod/dagger.sum" -const lockFilePath = "./cue.mod/dagger.lock" -const destBasePath = "./cue.mod/pkg" -const tmpBasePath = "./cue.mod/tmp" +const ( + modFilePath = "./cue.mod/dagger.mod" + sumFilePath = "./cue.mod/dagger.sum" + lockFilePath = "./cue.mod/dagger.lock" + destBasePath = "./cue.mod/pkg" + tmpBasePath = "./cue.mod/tmp" +) // file is the parsed, interpreted form of dagger.mod file. type file struct { @@ -127,20 +130,19 @@ func nonEmptyLines(b []byte) []string { } trimmed = spaceRgx.ReplaceAllString(trimmed, " ") - lines = append(lines, trimmed) } return lines } -func (f *file) install(req *Require) error { +func (f *file) install(ctx context.Context, req *Require) error { // cleaning up possible leftovers tmpPath := path.Join(f.workspacePath, tmpBasePath, req.fullPath()) defer os.RemoveAll(tmpPath) // clone to a tmp directory - r, err := clone(req, tmpPath, viper.GetString("private-key-file"), viper.GetString("private-key-password")) + r, err := clone(ctx, req, tmpPath, viper.GetString("private-key-file"), viper.GetString("private-key-password")) if err != nil { return fmt.Errorf("error downloading package %s: %w", req, err) } @@ -169,7 +171,7 @@ func (f *file) install(req *Require) error { // checkout the cloned repo to that tag, change the version in the existing requirement and // replace the code in the /pkg folder existing.version = req.version - if err = r.checkout(req.version); err != nil { + if err = r.checkout(ctx, req.version); err != nil { return err } @@ -187,7 +189,7 @@ func (f *file) install(req *Require) error { return nil } -func (f *file) updateToLatest(req *Require) (*Require, error) { +func (f *file) updateToLatest(ctx context.Context, req *Require) (*Require, error) { // check if it doesn't exist existing := f.searchInstalledRequire(req) if existing == nil { @@ -199,13 +201,13 @@ func (f *file) updateToLatest(req *Require) (*Require, error) { defer os.RemoveAll(tmpPath) // clone to a tmp directory - gitRepo, err := clone(existing, tmpPath, viper.GetString("private-key-file"), viper.GetString("private-key-password")) + gitRepo, err := clone(ctx, existing, tmpPath, viper.GetString("private-key-file"), viper.GetString("private-key-password")) if err != nil { return nil, fmt.Errorf("error downloading package %s: %w", existing, err) } // checkout the latest tag - latestTag, err := gitRepo.latestTag(req.versionConstraint) + latestTag, err := gitRepo.latestTag(ctx, req.versionConstraint) if err != nil { return nil, err } @@ -220,7 +222,7 @@ func (f *file) updateToLatest(req *Require) (*Require, error) { } existing.version = latestTag - if err = gitRepo.checkout(existing.version); err != nil { + if err = gitRepo.checkout(ctx, existing.version); err != nil { return nil, err } diff --git a/mod/mod.go b/mod/mod.go index 57892b7e..22c841f5 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -3,15 +3,29 @@ package mod import ( "context" "path" + "strings" "github.com/gofrs/flock" "github.com/rs/zerolog/log" ) +const ( + UniverseVersionConstraint = ">= 0.1.0, < 0.2" +) + +func isUniverse(repoName string) bool { + return strings.HasPrefix(strings.ToLower(repoName), "alpha.dagger.io") +} + func Install(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { lg := log.Ctx(ctx) - lg.Info().Str("name", repoName).Msg("installing module") + if isUniverse(repoName) { + // override versionConstraint to lock the version of universe we vendor + versionConstraint = UniverseVersionConstraint + } + + lg.Debug().Str("name", repoName).Msg("installing module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -27,7 +41,7 @@ func Install(ctx context.Context, workspace, repoName, versionConstraint string) return nil, err } - err = modfile.install(require) + err = modfile.install(ctx, require) if err != nil { return nil, err } @@ -63,7 +77,12 @@ func InstallAll(ctx context.Context, workspace string, repoNames []string) ([]*R func Update(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { lg := log.Ctx(ctx) - lg.Info().Str("name", repoName).Msg("updating module") + if isUniverse(repoName) { + // override versionConstraint to lock the version of universe we vendor + versionConstraint = UniverseVersionConstraint + } + + lg.Debug().Str("name", repoName).Msg("updating module") require, err := newRequire(repoName, versionConstraint) if err != nil { return nil, err @@ -79,7 +98,7 @@ func Update(ctx context.Context, workspace, repoName, versionConstraint string) return nil, err } - updatedRequire, err := modfile.updateToLatest(require) + updatedRequire, err := modfile.updateToLatest(ctx, require) if err != nil { return nil, err } diff --git a/mod/repo.go b/mod/repo.go index 09257054..efc894d3 100644 --- a/mod/repo.go +++ b/mod/repo.go @@ -1,12 +1,14 @@ package mod import ( + "context" "fmt" "os" "sort" "strings" "github.com/go-git/go-git/v5/plumbing/transport/ssh" + "github.com/rs/zerolog/log" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" @@ -15,9 +17,10 @@ import ( type repo struct { contents *git.Repository + require *Require } -func clone(require *Require, dir string, privateKeyFile, privateKeyPassword string) (*repo, error) { +func clone(ctx context.Context, require *Require, dir string, privateKeyFile, privateKeyPassword string) (*repo, error) { if err := os.RemoveAll(dir); err != nil { return nil, fmt.Errorf("error cleaning up tmp directory") } @@ -46,10 +49,11 @@ func clone(require *Require, dir string, privateKeyFile, privateKeyPassword stri rr := &repo{ contents: r, + require: require, } if require.version == "" { - latestTag, err := rr.latestTag(require.versionConstraint) + latestTag, err := rr.latestTag(ctx, require.versionConstraint) if err != nil { return nil, err } @@ -57,19 +61,23 @@ func clone(require *Require, dir string, privateKeyFile, privateKeyPassword stri require.version = latestTag } - if err := rr.checkout(require.version); err != nil { + if err := rr.checkout(ctx, require.version); err != nil { return nil, err } return rr, nil } -func (r *repo) checkout(version string) error { +func (r *repo) checkout(ctx context.Context, version string) error { + lg := log.Ctx(ctx) + h, err := r.contents.ResolveRevision(plumbing.Revision(version)) if err != nil { return err } + lg.Debug().Str("repository", r.require.repo).Str("version", version).Str("commit", h.String()).Msg("checkout repo") + w, err := r.contents.Worktree() if err != nil { return err @@ -85,7 +93,12 @@ func (r *repo) checkout(version string) error { return nil } -func (r *repo) listTagVersions(versionConstraint string) ([]string, error) { +func (r *repo) listTagVersions(ctx context.Context, versionConstraint string) ([]string, error) { + lg := log.Ctx(ctx).With(). + Str("repository", r.require.repo). + Str("versionConstraint", versionConstraint). + Logger() + if versionConstraint == "" { versionConstraint = ">= 0" } @@ -105,19 +118,22 @@ func (r *repo) listTagVersions(versionConstraint string) ([]string, error) { tagV := ref.Name().Short() if !strings.HasPrefix(tagV, "v") { - // ignore wrong formatted tags + lg.Debug().Str("tag", tagV).Msg("tag version ignored, wrong format") return nil } v, err := version.NewVersion(tagV) if err != nil { - // ignore invalid tag + lg.Debug().Str("tag", tagV).Err(err).Msg("tag version ignored, parsing error") return nil } if constraint.Check(v) { // Add tag if it matches the version constraint tags = append(tags, ref.Name().Short()) + lg.Debug().Str("tag", tagV).Msg("version added") + } else { + lg.Debug().Str("tag", tagV).Msg("tag version ignored, does not satisfy constraint") } return nil @@ -130,8 +146,8 @@ func (r *repo) listTagVersions(versionConstraint string) ([]string, error) { return tags, nil } -func (r *repo) latestTag(versionConstraint string) (string, error) { - versionsRaw, err := r.listTagVersions(versionConstraint) +func (r *repo) latestTag(ctx context.Context, versionConstraint string) (string, error) { + versionsRaw, err := r.listTagVersions(ctx, versionConstraint) if err != nil { return "", err } @@ -147,5 +163,7 @@ func (r *repo) latestTag(versionConstraint string) (string, error) { } sort.Sort(sort.Reverse(version.Collection(versions))) - return versions[0].Original(), nil + version := versions[0].Original() + + return version, nil } diff --git a/mod/repo_test.go b/mod/repo_test.go index efd6c747..54603037 100644 --- a/mod/repo_test.go +++ b/mod/repo_test.go @@ -1,6 +1,7 @@ package mod import ( + "context" "io/ioutil" "os" "testing" @@ -50,7 +51,7 @@ func TestClone(t *testing.T) { defer os.Remove(tmpDir) - _, err = clone(&c.require, tmpDir, c.privateKeyFile, c.privateKeyPassword) + _, err = clone(context.TODO(), &c.require, tmpDir, c.privateKeyFile, c.privateKeyPassword) if err != nil { t.Error(err) } @@ -65,7 +66,9 @@ func TestListTags(t *testing.T) { } defer os.Remove(tmpDir) - r, err := clone(&Require{ + ctx := context.TODO() + + r, err := clone(ctx, &Require{ cloneRepo: "github.com/dagger/universe", clonePath: "stdlib", version: "", @@ -74,7 +77,7 @@ func TestListTags(t *testing.T) { t.Fatal(err) } - tags, err := r.listTagVersions("") + tags, err := r.listTagVersions(ctx, "") if err != nil { t.Error(err) } @@ -91,7 +94,9 @@ func TestVersionConstraint(t *testing.T) { } defer os.Remove(tmpDir) - r, err := clone(&Require{ + ctx := context.TODO() + + r, err := clone(ctx, &Require{ cloneRepo: "github.com/dagger/universe", clonePath: "stdlib", version: "", @@ -100,7 +105,7 @@ func TestVersionConstraint(t *testing.T) { t.Fatal(err) } - tagVersion, err := r.latestTag("<= 0.1.0") + tagVersion, err := r.latestTag(ctx, "<= 0.1.0") if err != nil { t.Error(err) } @@ -111,7 +116,7 @@ func TestVersionConstraint(t *testing.T) { } // Make sure an invalid constraint (version out of range) returns an error - _, err = r.latestTag("> 99999") + _, err = r.latestTag(ctx, "> 99999") if err == nil { t.Error("selected wrong version based on constraint") } diff --git a/state/project.go b/state/project.go index adc72da3..de208ea2 100644 --- a/state/project.go +++ b/state/project.go @@ -25,13 +25,12 @@ var ( ) const ( - daggerDir = ".dagger" - envDir = "env" - stateDir = "state" - planDir = "plan" - manifestFile = "values.yaml" - computedFile = "computed.json" - universeVersionConstraint = ">= 0.1, < 0.2" + daggerDir = ".dagger" + envDir = "env" + stateDir = "state" + planDir = "plan" + manifestFile = "values.yaml" + computedFile = "computed.json" ) type Project struct { @@ -398,7 +397,7 @@ func vendorUniverse(ctx context.Context, p string) error { } log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe") - if _, err := mod.Install(ctx, p, "alpha.dagger.io", universeVersionConstraint); err != nil { + if _, err := mod.Install(ctx, p, "alpha.dagger.io", ""); err != nil { return err } From 994a8034c5166b678c49da263ede9a7c903283f0 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 22 Oct 2021 15:41:14 -0700 Subject: [PATCH 5/5] state: force use of stdlib embed - temporarily until universe repo is ready Signed-off-by: Sam Alba --- state/project.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/state/project.go b/state/project.go index de208ea2..6176def6 100644 --- a/state/project.go +++ b/state/project.go @@ -12,7 +12,6 @@ import ( "github.com/rs/zerolog/log" "go.dagger.io/dagger/keychain" - "go.dagger.io/dagger/mod" "go.dagger.io/dagger/stdlib" "gopkg.in/yaml.v3" ) @@ -397,7 +396,9 @@ func vendorUniverse(ctx context.Context, p string) error { } log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe") - if _, err := mod.Install(ctx, p, "alpha.dagger.io", ""); err != nil { + if err := stdlib.Vendor(ctx, p); err != nil { + // FIXME(samalba): disabled install remote stdlib temporarily + // if _, err := mod.Install(ctx, p, "alpha.dagger.io", ""); err != nil { return err }