Skip to content

Commit

Permalink
cmd/cue/cmd: allow fmt to pass with errors
Browse files Browse the repository at this point in the history
Also fixes use of internal `*` to match all packages.

Fixes #653
Fixes #644

Change-Id: I3f507c8a7d8013372972cbb11c9eeac01a50ce9f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8301
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jan 28, 2021
1 parent 726d93e commit 257486c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
11 changes: 10 additions & 1 deletion cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ type config struct {
loadCfg *load.Config
}

func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err error) {
func newBuildPlan(cmd *Command, args []string, cfg *config) (p *buildPlan, err error) {
if cfg == nil {
cfg = &defaultConfig
}
Expand All @@ -378,6 +378,15 @@ func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err erro

cfg.loadCfg.Tags = flagInject.StringArray(cmd)

return p, nil
}

func parseArgs(cmd *Command, args []string, cfg *config) (p *buildPlan, err error) {
p, err = newBuildPlan(cmd, args, cfg)
if err != nil {
return nil, err
}

builds := loadFromArgs(cmd, args, cfg.loadCfg)
if builds == nil {
return nil, errors.Newf(token.NoPos, "invalid args")
Expand Down
20 changes: 16 additions & 4 deletions cmd/cue/cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/encoding"
"cuelang.org/go/tools/fix"
)
Expand All @@ -32,14 +34,19 @@ func newFmtCmd(c *Command) *cobra.Command {
Long: `Fmt formats the given files or the files for the given packages in place
`,
RunE: mkRunE(c, func(cmd *Command, args []string) error {
plan, err := parseArgs(cmd, args, &config{loadCfg: &load.Config{
plan, err := newBuildPlan(cmd, args, &config{loadCfg: &load.Config{
Tests: true,
Tools: true,
AllCUEFiles: true,
Package: "*",
}})
exitOnErr(cmd, err, true)

builds := loadFromArgs(cmd, args, plan.cfg.loadCfg)
if builds == nil {
exitOnErr(cmd, errors.Newf(token.NoPos, "invalid args"), true)
}

opts := []format.Option{}
if flagSimplify.Bool(cmd) {
opts = append(opts, format.Simplify())
Expand All @@ -49,10 +56,15 @@ func newFmtCmd(c *Command) *cobra.Command {
cfg.Format = opts
cfg.Force = true

for _, inst := range plan.insts {
for _, inst := range builds {
if inst.Err != nil {
exitOnErr(cmd, inst.Err, false)
continue
var p *load.PackageError
switch {
case errors.As(inst.Err, &p):
default:
exitOnErr(cmd, inst.Err, false)
continue
}
}
for _, file := range inst.BuildFiles {
files := []*ast.File{}
Expand Down
23 changes: 23 additions & 0 deletions cmd/cue/cmd/testdata/script/fmt_err.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ignore certain errors for cue fmt
cue fmt x.cue

# Issue #644
cue fmt ./...

cmp x.cue out/x_cue
-- cue.mod/module.cue --
module: "example.com/x"
-- x.cue --
package x

import "blah.com/rubbish"

x: 5
y: unresolved
-- out/x_cue --
package x

import "blah.com/rubbish"

x: 5
y: unresolved
10 changes: 8 additions & 2 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (c *Config) newRelInstance(pos token.Pos, path, pkgName string) *build.Inst
"non-canonical import path: %q should be %q", path, pathpkg.Clean(path)))
}

if importPath, e := c.importPathFromAbsDir(fsPath(dir), path, c.Package); e != nil {
if importPath, e := c.importPathFromAbsDir(fsPath(dir), path); e != nil {
// Detect later to keep error messages consistent.
} else {
p.ImportPath = string(importPath)
Expand Down Expand Up @@ -358,7 +358,7 @@ type importPath string

type fsPath string

func (c *Config) importPathFromAbsDir(absDir fsPath, key, name string) (importPath, errors.Error) {
func (c *Config) importPathFromAbsDir(absDir fsPath, key string) (importPath, errors.Error) {
if c.ModuleRoot == "" {
return "", errors.Newf(token.NoPos,
"cannot determine import path for %q (root undefined)", key)
Expand Down Expand Up @@ -394,6 +394,12 @@ func (c *Config) importPathFromAbsDir(absDir fsPath, key, name string) (importPa
pkg = c.Module + pkg
}

name := c.Package
switch name {
case "_", "*":
name = ""
}

return addImportQualifier(importPath(pkg), name)
}

Expand Down
5 changes: 4 additions & 1 deletion cue/load/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {

if !found {
return retErr(
errors.Newf(token.NoPos, "cannot find package %q", p.DisplayPath))
&PackageError{
Message: errors.NewMessage("cannot find package %q",
[]interface{}{p.DisplayPath}),
})
}

// This algorithm assumes that multiple directories within cue.mod/*/
Expand Down

0 comments on commit 257486c

Please sign in to comment.