Skip to content

Commit

Permalink
cue/load: clean up some var names and code organization
Browse files Browse the repository at this point in the history
This CL:

- improves naming of a few methods
- removes an unnecessary method
- moves some var declarations closer to their usage

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Ia94b73088355565a94461db95806232eb7842fa3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193882
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
NoamTD authored and mvdan committed Apr 26, 2024
1 parent 35af5a1 commit 41063b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
13 changes: 6 additions & 7 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (c Config) complete() (cfg *Config, err error) {
if c.ModuleRoot == "" {
// Only consider the current directory by default
c.ModuleRoot = c.Dir
if root := c.findRoot(c.Dir); root != "" {
if root := c.findModRoot(c.Dir); root != "" {
c.ModuleRoot = root
}
} else if !filepath.IsAbs(c.ModuleRoot) {
Expand Down Expand Up @@ -442,19 +442,18 @@ func (c *Config) loadModule() error {
return nil
}

func (c Config) isRoot(dir string) bool {
fs := &c.fileSystem
func (c Config) isModRoot(dir string) bool {
// Note: cue.mod used to be a file. We still allow both to match.
_, err := fs.stat(filepath.Join(dir, modDir))
_, err := c.fileSystem.stat(filepath.Join(dir, modDir))
return err == nil
}

// findRoot returns the module root that's ancestor
// findModRoot 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 {
func (c Config) findModRoot(absDir string) string {
abs := absDir
for {
if c.isRoot(abs) {
if c.isModRoot(abs) {
return abs
}
d := filepath.Dir(abs)
Expand Down
8 changes: 2 additions & 6 deletions cue/load/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ func (fs *fileSystem) init(cwd string, overlay map[string]Source) error {
return nil
}

func (fs *fileSystem) joinPath(elem ...string) string {
return filepath.Join(elem...)
}

func (fs *fileSystem) makeAbs(path string) string {
if filepath.IsAbs(path) {
return path
Expand Down Expand Up @@ -270,7 +266,7 @@ func (fs *fileSystem) walkRec(path string, entry iofs.DirEntry, f walkFunc) erro
}

for _, entry := range dir {
filename := fs.joinPath(path, entry.Name())
filename := filepath.Join(path, entry.Name())
err = fs.walkRec(filename, entry, f)
if err != nil {
if !entry.IsDir() || err != skipDir {
Expand Down Expand Up @@ -335,7 +331,7 @@ func (fs *ioFS) ReadDir(name string) ([]iofs.DirEntry, error) {
return fs.fs.readDir(fpath)
}

// ReadDir implements [io/fs.ReadFileFS].
// ReadFile implements [io/fs.ReadFileFS].
func (fs *ioFS) ReadFile(name string) ([]byte, error) {
fpath, err := fs.absPathFromFSPath(name)
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
// have the same module scope and that there are no invalid modules.
inModule := false // if pkg == "_"
for _, d := range dirs {
if l.cfg.findRoot(d[1]) != "" {
if l.cfg.findModRoot(d[1]) != "" {
inModule = true
break
}
Expand All @@ -156,7 +156,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
fp.add(dir, &bf, importComment)
}

if p.PkgName == "" || !inModule || l.cfg.isRoot(dir) || dir == d[0] {
if p.PkgName == "" || !inModule || l.cfg.isModRoot(dir) || dir == d[0] {
break
}

Expand Down Expand Up @@ -259,23 +259,20 @@ func (l *loader) newRelInstance(pos token.Pos, path, pkgName string) *build.Inst
panic(fmt.Errorf("non-relative import path %q passed to newRelInstance", path))
}

var err errors.Error
dir := path

p := l.cfg.Context.NewInstance(path, l.loadFunc)
p.PkgName = pkgName
p.DisplayPath = filepath.ToSlash(path)
// p.ImportPath = string(dir) // compute unique ID.
p.Root = l.cfg.ModuleRoot
p.Module = l.cfg.Module

dir = filepath.Join(l.cfg.Dir, filepath.FromSlash(path))

var err errors.Error
if path != cleanImport(path) {
err = errors.Append(err, l.errPkgf(nil,
"non-canonical import path: %q should be %q", path, pathpkg.Clean(path)))
}

dir := filepath.Join(l.cfg.Dir, filepath.FromSlash(path))
if importPath, e := l.importPathFromAbsDir(fsPath(dir), path); e != nil {
// Detect later to keep error messages consistent.
} else {
Expand Down

0 comments on commit 41063b7

Please sign in to comment.