From 332899098d1d6f1f61ad9b956f3df2f6f770bb60 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Wed, 20 Mar 2024 16:21:53 +0000 Subject: [PATCH] cue/load: deprecate Config.StdRoot This field has not had the intended effect ever since commit 96ef9aad2385bdf9282e35e0f304dcae9bda266e, and it seems dubious in principle anyway, so deprecate it. Also factor out the "is standard library" functionality to its own function and add a TODO in passing. Signed-off-by: Roger Peppe Change-Id: Idf055a8139393908acdaf35820a005d8241ec4a0 --- cue/load/config.go | 3 ++- cue/load/import.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cue/load/config.go b/cue/load/config.go index 073ae5c4a..d79e9fa3e 100644 --- a/cue/load/config.go +++ b/cue/load/config.go @@ -252,7 +252,7 @@ type Config struct { DataFiles bool // StdRoot specifies an alternative directory for standard libraries. - // This is mostly used for bootstrapping. + // Deprecated: this has no effect. StdRoot string // ParseFile is called to read and parse each file when preparing a @@ -310,6 +310,7 @@ type fsPath string func addImportQualifier(pkg importPath, name string) (importPath, errors.Error) { if name != "" { + // TODO use module.ParseImportPath s := string(pkg) if i := strings.LastIndexByte(s, '/'); i >= 0 { s = s[i+1:] diff --git a/cue/load/import.go b/cue/load/import.go index e074c2b6e..8524e2249 100644 --- a/cue/load/import.go +++ b/cue/load/import.go @@ -213,13 +213,8 @@ func (l *loader) _loadFunc(pos token.Pos, path string) *build.Instance { return l.cfg.newErrInstance(errors.Newf(pos, "relative import paths not allowed (%q)", path)) } - // is it a builtin? - if strings.IndexByte(strings.Split(path, "/")[0], '.') == -1 { - if l.cfg.StdRoot != "" { - p := l.newInstance(pos, impPath) - _ = l.importPkg(pos, p) - return p - } + if isStdlibPackage(path) { + // It looks like a builtin. return nil } @@ -408,3 +403,10 @@ func absPathForSourceLoc(loc module.SourceLoc) (string, error) { } return filepath.Join(osPath, loc.Dir), nil } + +// isStdlibPackage reports whether pkgPath looks like +// an import from the standard library. +func isStdlibPackage(pkgPath string) bool { + firstElem, _, _ := strings.Cut(pkgPath, "/") + return strings.IndexByte(firstElem, '.') == -1 +}