Skip to content

Commit b03789f

Browse files
committed
cue/load: remove import comment parsing
Import comments were introduced in Go 1.4 (see background document [1]) but as far as I am aware, have never been used or supported in CUE: they were only useful in Go before the `go.mod` file made it possible to declare a module name, and CUE has had `module.cue` files for a long time. In any case, CUE generally uses attributes for this kind of thing rather than comments. So remove the import comment parsing and deprecate the `cue/build.Instance.ImportComment` field. Also in passing remove the unused `fsPath` type. [1] https://docs.google.com/document/d/1jVFkZTcYbNLaTxXD9OcGfn7vYv5hWtPx9--lTx1gPMs/edit Signed-off-by: Roger Peppe <[email protected]> Change-Id: I48aefec1a37c73c5f6ddef50367799d9cff276eb Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197524 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 59fe2e4 commit b03789f

File tree

5 files changed

+10
-124
lines changed

5 files changed

+10
-124
lines changed

cmd/cue/cmd/testdata/script/load_underscore.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmp stderr want-stderr
88
! exec cue export other.example/m:_
99
cmp stderr want-stderr-2
1010
-- want-stderr --
11-
test.example/foo/foo@v0: import failed: cannot find package "other.example/m": _ is not a valid import path qualifier in "other.example/m:_":
11+
test.example/foo/foo@v0: import failed: _ is not a valid import path qualifier in "other.example/m:_":
1212
./foo/foo.cue:8:8
1313
-- want-stderr-2 --
1414
invalid import path qualifier _ in "other.example/m:_"

cue/build/instance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ type Instance struct {
9393
// NOTICE: the below tags may change in the future.
9494

9595
// ImportComment is the path in the import comment on the package statement.
96+
//
97+
// Deprecated: CUE has never needed or supported import comments.
9698
ImportComment string `api:"alpha"`
9799

98100
// AllTags are the build tags that can influence file selection in this

cue/load/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ func (c *Config) stdin() io.Reader {
309309

310310
type importPath string
311311

312-
type fsPath string
313-
314312
func addImportQualifier(pkg importPath, name string) (importPath, error) {
315313
if name == "" {
316314
return pkg, nil

cue/load/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (l *loader) importPkg(pos token.Pos, p *build.Instance) []*build.Instance {
162162
ExcludeReason: errors.Newf(token.NoPos, "unknown filetype"),
163163
})
164164
} else {
165-
fp.add(dir, file, importComment)
165+
fp.add(dir, file, 0)
166166
}
167167
}
168168
if p.PkgName == "" || !inModule || l.cfg.isModRoot(dir) || dir == d[0] {
@@ -377,7 +377,7 @@ func (l *loader) newInstance(pos token.Pos, p importPath) *build.Instance {
377377
i.PkgName = parts.Qualifier
378378
if i.PkgName == "" {
379379
i.Err = errors.Append(i.Err, l.errPkgf([]token.Pos{pos}, "cannot determine package name for %q; set it explicitly with ':'", p))
380-
} else if i.PkgName == "" {
380+
} else if i.PkgName == "_" {
381381
i.Err = errors.Append(i.Err, l.errPkgf([]token.Pos{pos}, "_ is not a valid import path qualifier in %q", p))
382382
}
383383
i.DisplayPath = string(p)

cue/load/loader_common.go

Lines changed: 5 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
package load
1616

1717
import (
18-
"bytes"
1918
"cmp"
2019
pathpkg "path"
2120
"path/filepath"
2221
"slices"
2322
"sort"
2423
"strconv"
2524
"strings"
26-
"unicode"
27-
"unicode/utf8"
2825

2926
"cuelang.org/go/cue/build"
3027
"cuelang.org/go/cue/errors"
@@ -36,13 +33,7 @@ import (
3633
type importMode uint
3734

3835
const (
39-
// If importComment is set, parse import comments on package statements.
40-
// Import returns an error if it finds a comment it cannot understand
41-
// or finds conflicting comments in multiple source files.
42-
// See golang.org/s/go14customimport for more information.
43-
importComment importMode = 1 << iota
44-
45-
allowAnonymous
36+
allowAnonymous = 1 << iota
4637
allowExcludedFiles
4738
)
4839

@@ -99,11 +90,10 @@ func (s *importStack) Copy() []string {
9990
}
10091

10192
type fileProcessor struct {
102-
firstFile string
103-
firstCommentFile string
104-
imported map[string][]token.Pos
105-
ignoreOther bool // ignore files from other packages
106-
allPackages bool
93+
firstFile string
94+
imported map[string][]token.Pos
95+
ignoreOther bool // ignore files from other packages
96+
allPackages bool
10797

10898
c *fileProcessorConfig
10999
tagger *tagger
@@ -283,21 +273,6 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) (ad
283273
isTest := strings.HasSuffix(base, "_test"+cueSuffix)
284274
isTool := strings.HasSuffix(base, "_tool"+cueSuffix)
285275

286-
if mode&importComment != 0 {
287-
qcom, line := findImportComment(data)
288-
if line != 0 {
289-
com, err := strconv.Unquote(qcom)
290-
if err != nil {
291-
badFile(errors.Newf(pos, "%s:%d: cannot parse import comment", fullPath, line))
292-
} else if p.ImportComment == "" {
293-
p.ImportComment = com
294-
fp.firstCommentFile = base
295-
} else if p.ImportComment != com {
296-
badFile(errors.Newf(pos, "found import comments %q (%s) and %q (%s) in %s", p.ImportComment, fp.firstCommentFile, com, base, p.Dir))
297-
}
298-
}
299-
}
300-
301276
for _, spec := range pf.Imports {
302277
quoted := spec.Path.Value
303278
path, err := strconv.Unquote(quoted)
@@ -334,95 +309,6 @@ func (fp *fileProcessor) add(root string, file *build.File, mode importMode) (ad
334309
return true
335310
}
336311

337-
func findImportComment(data []byte) (s string, line int) {
338-
// expect keyword package
339-
word, data := parseWord(data)
340-
if string(word) != "package" {
341-
return "", 0
342-
}
343-
344-
// expect package name
345-
_, data = parseWord(data)
346-
347-
// now ready for import comment, a // comment
348-
// beginning and ending on the current line.
349-
for len(data) > 0 && (data[0] == ' ' || data[0] == '\t' || data[0] == '\r') {
350-
data = data[1:]
351-
}
352-
353-
var comment []byte
354-
switch {
355-
case bytes.HasPrefix(data, slashSlash):
356-
i := bytes.Index(data, newline)
357-
if i < 0 {
358-
i = len(data)
359-
}
360-
comment = data[2:i]
361-
}
362-
comment = bytes.TrimSpace(comment)
363-
364-
// split comment into `import`, `"pkg"`
365-
word, arg := parseWord(comment)
366-
if string(word) != "import" {
367-
return "", 0
368-
}
369-
370-
line = 1 + bytes.Count(data[:cap(data)-cap(arg)], newline)
371-
return strings.TrimSpace(string(arg)), line
372-
}
373-
374-
var (
375-
slashSlash = []byte("//")
376-
newline = []byte("\n")
377-
)
378-
379-
// skipSpaceOrComment returns data with any leading spaces or comments removed.
380-
func skipSpaceOrComment(data []byte) []byte {
381-
for len(data) > 0 {
382-
switch data[0] {
383-
case ' ', '\t', '\r', '\n':
384-
data = data[1:]
385-
continue
386-
case '/':
387-
if bytes.HasPrefix(data, slashSlash) {
388-
i := bytes.Index(data, newline)
389-
if i < 0 {
390-
return nil
391-
}
392-
data = data[i+1:]
393-
continue
394-
}
395-
}
396-
break
397-
}
398-
return data
399-
}
400-
401-
// parseWord skips any leading spaces or comments in data
402-
// and then parses the beginning of data as an identifier or keyword,
403-
// returning that word and what remains after the word.
404-
func parseWord(data []byte) (word, rest []byte) {
405-
data = skipSpaceOrComment(data)
406-
407-
// Parse past leading word characters.
408-
rest = data
409-
for {
410-
r, size := utf8.DecodeRune(rest)
411-
if unicode.IsLetter(r) || '0' <= r && r <= '9' || r == '_' {
412-
rest = rest[size:]
413-
continue
414-
}
415-
break
416-
}
417-
418-
word = data[:len(data)-len(rest)]
419-
if len(word) == 0 {
420-
return nil, nil
421-
}
422-
423-
return word, rest
424-
}
425-
426312
func cleanImports(m map[string][]token.Pos) ([]string, map[string][]token.Pos) {
427313
all := make([]string, 0, len(m))
428314
for path := range m {

0 commit comments

Comments
 (0)