Skip to content

Commit efdb072

Browse files
committed
cue/load: avoid one stat call when loading a valid module
We would first stat cue.mod to see whether it is a regular file, to give a clear human-friendly error, and then open cue.mod/module.cue to actually parse the module file declaring the module to be loaded. In most cases, cue.mod is a directory, so we can open cue.mod/module.cue directly and a nil error already tells us that cue.mod is a directory. Only stat cue.mod if the cue.mod/module.cue file open fails. This reduces one syscall when loading a valid module, which isn't much, but still an easy improvement to make. Finally, as Matthew suggests, add a godoc line about our behavior when a cue.mod/module.cue file is not present. Signed-off-by: Daniel Martí <[email protected]> Change-Id: If387adcc860c13bd80d04c4b5cde69385934cf2d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198065 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 28c6219 commit efdb072

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

cue/load/config.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -400,19 +400,21 @@ func (c Config) complete() (cfg *Config, err error) {
400400
// loadModule loads the module file, resolves and downloads module
401401
// dependencies. It sets c.Module if it's empty or checks it for
402402
// consistency with the module file otherwise.
403+
//
404+
// Note that this function is a no-op if a module file does not exist,
405+
// as it is still possible to load CUE without a module.
403406
func (c *Config) loadModule() error {
404407
// TODO: also make this work if run from outside the module?
405-
mod := filepath.Join(c.ModuleRoot, modDir)
406-
info, cerr := c.fileSystem.stat(mod)
407-
if cerr != nil {
408-
return nil
409-
}
410-
if !info.IsDir() {
411-
return fmt.Errorf("cue.mod files are no longer supported; use cue.mod/module.cue")
412-
}
413-
mod = filepath.Join(mod, moduleFile)
414-
f, cerr := c.fileSystem.openFile(mod)
408+
modDir := filepath.Join(c.ModuleRoot, modDir)
409+
modFile := filepath.Join(modDir, moduleFile)
410+
f, cerr := c.fileSystem.openFile(modFile)
415411
if cerr != nil {
412+
// If we could not load cue.mod/module.cue, check whether the reason was
413+
// a legacy cue.mod file and give the user a clear error message.
414+
info, cerr2 := c.fileSystem.stat(modDir)
415+
if cerr2 == nil && !info.IsDir() {
416+
return fmt.Errorf("cue.mod files are no longer supported; use cue.mod/module.cue")
417+
}
416418
return nil
417419
}
418420
defer f.Close()
@@ -431,7 +433,7 @@ func (c *Config) loadModule() error {
431433
// module files have been discovered in the wild.
432434
parseModFile = modfile.FixLegacy
433435
}
434-
mf, err := parseModFile(data, mod)
436+
mf, err := parseModFile(data, modFile)
435437
if err != nil {
436438
return err
437439
}

0 commit comments

Comments
 (0)