From e81d18160728bbc047dd8cb1f3488d44faede883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 7 Dec 2023 11:58:27 +0000 Subject: [PATCH] cue/load: drop support for legacy pkg directories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cue/load was changed back in 2019 to transition to cue.mod being a directory rather than a file, so that it could contain the CUE declaring the module as cue.mod/module.cue, as well as other directories like cue.mod/pkg/ rather than pkg/. The logic to find the module root by walking parent directories as well as the logic to import a package still supported a "legacy" mode where the pkg directory exists outside of the cue.mod directory. This "legacy" mode was added over four years ago now, and we are now evolving CUE modules to add dependency management, so it seems like a good time to remove the legacy mode support. Any users still having a pkg directory outside of cue.mod should be able to fix their module by simply: mv pkg cue.mod/ This is precisely what we do in the encoding/openapi package, while also moving away from cue.mod being a regular file. Note that we still support cue.mod being a regular file itself, rather than a directory holding cue.mod/module.cue. Dropping support for that other "legacy" mode is left for another CL. For #2726. Signed-off-by: Daniel Martí Change-Id: I9b72944b66231051dd49dc9b163bf2a4b16ec036 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173271 TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine Reviewed-by: Paul Jolly Reviewed-by: Roger Peppe --- cue/load/config.go | 19 +------------ cue/load/import.go | 28 ++++++------------- cue/load/search.go | 4 +-- encoding/openapi/testdata/cue.mod | 0 encoding/openapi/testdata/cue.mod/module.cue | 1 + .../pkg/mod.test/blocks/blocks.cue | 0 internal/internal.go | 12 -------- 7 files changed, 11 insertions(+), 53 deletions(-) delete mode 100644 encoding/openapi/testdata/cue.mod create mode 100644 encoding/openapi/testdata/cue.mod/module.cue rename encoding/openapi/testdata/{ => cue.mod}/pkg/mod.test/blocks/blocks.cue (100%) diff --git a/cue/load/config.go b/cue/load/config.go index 8d131dcf5..280232371 100644 --- a/cue/load/config.go +++ b/cue/load/config.go @@ -33,7 +33,6 @@ const ( cueSuffix = ".cue" modDir = "cue.mod" moduleFile = "module.cue" - pkgDir = "pkg" ) // FromArgsUsage is a partial usage message that applications calling @@ -384,8 +383,6 @@ func (c Config) isRoot(dir string) bool { // findRoot returns the module root that's ancestor // of the given absolute directory path, or "" if none was found. func (c Config) findRoot(absDir string) string { - fs := &c.fileSystem - abs := absDir for { if c.isRoot(abs) { @@ -398,21 +395,7 @@ func (c Config) findRoot(absDir string) string { return "" } if len(d) >= len(abs) { - break // reached top of file system, no cue.mod - } - abs = d - } - abs = absDir - - // TODO(legacy): remove this capability at some point. - for { - info, err := fs.stat(filepath.Join(abs, pkgDir)) - if err == nil && info.IsDir() { - return abs - } - d := filepath.Dir(abs) - if len(d) >= len(abs) { - return "" // reached top of file system, no pkg dir. + return "" // reached top of file system, no cue.mod } abs = d } diff --git a/cue/load/import.go b/cue/load/import.go index 2842d48c8..1ab8f31b0 100644 --- a/cue/load/import.go +++ b/cue/load/import.go @@ -97,20 +97,16 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance { genDir := GenPath(cfg.ModuleRoot) if strings.HasPrefix(p.Dir, genDir) { dirs = append(dirs, [2]string{genDir, p.Dir}) - // TODO(legacy): don't support "pkg" // && p.PkgName != "_" - if filepath.Base(genDir) != "pkg" { - for _, sub := range []string{"pkg", "usr"} { - rel, err := filepath.Rel(genDir, p.Dir) - if err != nil { - // should not happen - return retErr( - errors.Wrapf(err, token.NoPos, "invalid path")) - } - base := filepath.Join(cfg.ModuleRoot, modDir, sub) - dir := filepath.Join(base, rel) - dirs = append(dirs, [2]string{base, dir}) + for _, sub := range []string{"pkg", "usr"} { + rel, err := filepath.Rel(genDir, p.Dir) + if err != nil { + // should not happen + return retErr(errors.Wrapf(err, token.NoPos, "invalid path")) } + base := filepath.Join(cfg.ModuleRoot, modDir, sub) + dir := filepath.Join(base, rel) + dirs = append(dirs, [2]string{base, dir}) } } else { dirs = append(dirs, [2]string{cfg.ModuleRoot, p.Dir}) @@ -299,14 +295,6 @@ func (l *loader) importPathFromAbsDir(absDir fsPath, key string) (importPath, er "invalid package %q (root of %s)", key, modDir) } - // TODO(legacy): remove. - case strings.HasPrefix(pkg, "/pkg/"): - pkg = pkg[len("/pkg/"):] - if pkg == "" { - return "", errors.Newf(token.NoPos, - "invalid package %q (root of %s)", key, pkgDir) - } - case l.cfg.Module == "": return "", errors.Newf(token.NoPos, "cannot determine import path for %q (no module)", key) diff --git a/cue/load/search.go b/cue/load/search.go index c821d1b79..acf80e966 100644 --- a/cue/load/search.go +++ b/cue/load/search.go @@ -150,14 +150,12 @@ func (l *loader) matchPackagesInFS(pattern, pkgName string) *match { } pkgDir := filepath.Join(root, modDir) - // TODO(legacy): remove - pkgDir2 := filepath.Join(root, "pkg") _ = c.fileSystem.walk(root, func(path string, entry fs.DirEntry, err errors.Error) errors.Error { if err != nil || !entry.IsDir() { return nil } - if path == pkgDir || path == pkgDir2 { + if path == pkgDir { return skipDir } diff --git a/encoding/openapi/testdata/cue.mod b/encoding/openapi/testdata/cue.mod deleted file mode 100644 index e69de29bb..000000000 diff --git a/encoding/openapi/testdata/cue.mod/module.cue b/encoding/openapi/testdata/cue.mod/module.cue new file mode 100644 index 000000000..f8af9cef9 --- /dev/null +++ b/encoding/openapi/testdata/cue.mod/module.cue @@ -0,0 +1 @@ +module: "" diff --git a/encoding/openapi/testdata/pkg/mod.test/blocks/blocks.cue b/encoding/openapi/testdata/cue.mod/pkg/mod.test/blocks/blocks.cue similarity index 100% rename from encoding/openapi/testdata/pkg/mod.test/blocks/blocks.cue rename to encoding/openapi/testdata/cue.mod/pkg/mod.test/blocks/blocks.cue diff --git a/internal/internal.go b/internal/internal.go index 9c85d0f71..cba333ca7 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -22,7 +22,6 @@ package internal // import "cuelang.org/go/internal" import ( "bufio" "fmt" - "os" "path/filepath" "strings" @@ -435,17 +434,6 @@ func IsEllipsis(x ast.Decl) bool { // GenPath reports the directory in which to store generated files. func GenPath(root string) string { - info, err := os.Stat(filepath.Join(root, "cue.mod")) - if os.IsNotExist(err) || !info.IsDir() { - // Try legacy pkgDir mode - pkgDir := filepath.Join(root, "pkg") - if err == nil && !info.IsDir() { - return pkgDir - } - if info, err := os.Stat(pkgDir); err == nil && info.IsDir() { - return pkgDir - } - } return filepath.Join(root, "cue.mod", "gen") } -- 2.51.2