Skip to content

Commit a8f9383

Browse files
committed
cue/load: make MultiplePackageError support relative dir paths
cue/errors already knew how to adjust Error.Msg arguments of type token.Pos so that their filenames are relative. However, cue/load.MultiplePackageError reported the directory argument as simply a string, which we cannot safely know is a filename. Shove the directory name as a token.Position filename, which ends up working as the line and column numbers are omitted when left empty. This is mild abuse of the type, hence the note. However, being able to point to directory "positions" seems useful. Then teach cue/errors to make both token.Pos and token.Position argument filenames relative. Fixes #3703. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I500b343a4f2b2c681a4649d73c3668d09fc2ecf0 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1214888 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 4f3a215 commit a8f9383

6 files changed

+22
-14
lines changed

cmd/cue/cmd/testdata/script/load_pkg.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cmp stdout stdout-pkg
1616

1717
# don't allow mixing two differently named packages
1818
! exec cue eval kube.cue foo/kube2.cue
19-
cmpenv stderr different-packages.stderr
19+
cmp stderr different-packages.stderr
2020

2121
# When passing multiple CUE files in different directories,
2222
# cue/load will sort the filenames so that children come later.
@@ -35,7 +35,7 @@ cmp stdout stdout-sort
3535
cd ..
3636

3737
-- different-packages.stderr --
38-
found packages "kube" (kube.cue) and "kube2" (kube2.cue) in "${WORK@R}"
38+
found packages "kube" (kube.cue) and "kube2" (kube2.cue) in "."
3939
-- data.cue --
4040
foo:3
4141
-- t.cue --

cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_build_attr.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exec cue eval ./...
55
cmp stdout eval-all.golden
66

77
! exec cue eval -t something ./...
8-
stderr '^found packages "x" \(x.cue\) and "y" \(y.cue\) in ".*x"$'
8+
stderr '^found packages "x" \(x.cue\) and "y" \(y.cue\) in "x"$'
99

1010
# Test that a non-wildcard pattern similarly respects
1111
# build attributes.

cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_no_matching_path_element.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmp stderr import_stderr.golden
88
cmp stderr absolute_stderr.golden
99
! exec cue eval ./x
1010
# TODO: it would be nice if the error output was similar for this case as the others.
11-
stderr 'found packages "y" \(y.cue\) and "z" \(z.cue\) in ".*"'
11+
stderr 'found packages "y" \(y.cue\) and "z" \(z.cue\) in "x"'
1212

1313
-- cue.mod/module.cue --
1414
module: "mod.com"

cmd/cue/cmd/testdata/script/pkg_resolution_multiple_packages_one_matching_path_element.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exec cue eval mod.com/x
88
cmp stdout stdout.golden
99
# TODO: the following command fails unexpectedly although it should be consistent with the above.
1010
! exec cue eval ./x
11-
stderr 'found packages "x" \(x.cue\) and "y" \(y.cue\) in ".*script-pkg_resolution_multiple_packages_one_matching_path_element.*"'
11+
stderr 'found packages "x" \(x.cue\) and "y" \(y.cue\) in "\./x"'
1212

1313
-- stdout.golden --
1414
x: 5

cue/errors/errors.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,21 @@ func writeErr(w io.Writer, err Error, cfg *Config) {
543543
// so we make a copy if we need to replace any arguments.
544544
didCopy := false
545545
for i, arg := range args {
546-
if arg, ok := arg.(token.Pos); ok {
547-
if !didCopy {
548-
args = slices.Clone(args)
549-
didCopy = true
550-
}
551-
pos := arg.Position()
552-
pos.Filename = relPath(pos.Filename, cfg)
553-
args[i] = pos
546+
var pos token.Position
547+
switch arg := arg.(type) {
548+
case token.Pos:
549+
pos = arg.Position()
550+
case token.Position:
551+
pos = arg
552+
default:
553+
continue
554554
}
555+
if !didCopy {
556+
args = slices.Clone(args)
557+
didCopy = true
558+
}
559+
pos.Filename = relPath(pos.Filename, cfg)
560+
args[i] = pos
555561
}
556562

557563
n, _ := fmt.Fprintf(w, msg, args...)

cue/load/errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ func (e *MultiplePackageError) Msg() (string, []interface{}) {
131131
e.Files[0],
132132
e.Packages[1],
133133
e.Files[1],
134-
e.Dir,
134+
// To make sure [cue/errors] prints this directory name as relative,
135+
// use a [token.Position] even though it's really only meant for regular source files.
136+
token.Position{Filename: e.Dir},
135137
}
136138
}
137139

0 commit comments

Comments
 (0)