Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cue: cmd input resolution logic does not match imports resolution in the presence of multiple packages #1138

Closed
myitcv opened this issue Jul 26, 2021 · 4 comments
Assignees

Comments

@myitcv
Copy link
Member

myitcv commented Jul 26, 2021

What version of CUE are you using (cue version)?

$ cue version
cue version +ecb17c94 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Discussion on the back of #1136.

# 001 - the x directory contains a single package x
#       that matches the package path mod.com/x
#
# Import resolution: ok; resolves to x
# cmd/cue resolution: ok; resolves to x
cd $WORK/001_single_package_matching_path_element
exec cue eval root.cue
exec cue eval ./x

# 002 - the x directory contains a single package y
#       that does not match the package path mod.com/x
#
# Import resolution: ok; fails to resolve a package as expected
# cmd/cue resolution: NOT ok; unexpectedly resolves to package x
cd $WORK/002_single_package_not_matching_path_element
! exec cue eval root.cue
cmp stderr root_stderr.golden
! exec cue eval ./x
cmp stderr cmd_stderr.golden

# 003 - the x directory contains packages x and y,
#       such that one matches the package path mod.com/x
#
# Import resolution: ok; resolves to x as expected
# cmd/cue resolution: NOT ok; fails to resolve to package x with error
#                     found packages "x" (x.cue) and y (y.cue)
cd $WORK/003_multiple_packages_one_matching_path_element
exec cue eval root.cue
exec cue eval ./x

# 004 - the x directory contains packages y and z,
#       such that neither matches the package path mod.com/x
#
# Import resolution: ok; fails to resolve a package as expected
# cmd/cue resolution: ok; fails to resolve a package as expected
#                     found packages "x" (x.cue) and y (y.cue)
cd $WORK/004_multiple_packages_neither_matching_path_element
! exec cue eval root.cue
cmpenv stderr root_stderr.golden
! exec cue eval ./x
cmpenv stderr cmd_stderr.golden


-- 001_single_package_matching_path_element/cue.mod/module.cue --
module: "mod.com"
-- 001_single_package_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 001_single_package_matching_path_element/x/x.cue --
package x

x: 5
-- 002_single_package_not_matching_path_element/cue.mod/module.cue --
module: "mod.com"
-- 002_single_package_not_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 002_single_package_not_matching_path_element/x/y.cue --
package y

y: 5
-- 002_single_package_not_matching_path_element/root_stderr.golden --
import failed: build constraints exclude all CUE files in mod.com/x:
    root.cue: package is root, want x
    x/y.cue: package is y, want x:
    ./root.cue:3:8
-- 002_single_package_not_matching_path_element/root_stderr.golden --
import failed: build constraints exclude all CUE files in mod.com/x:
    root.cue: package is root, want x
    x/y.cue: package is y, want x:
    ./root.cue:3:8
-- 003_multiple_packages_one_matching_path_element/cue.mod/module.cue --
module: "mod.com"
-- 003_multiple_packages_one_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 003_multiple_packages_one_matching_path_element/x/x.cue --
package x

x: 5
-- 003_multiple_packages_one_matching_path_element/x/y.cue --
package y

y: 5
-- 004_multiple_packages_neither_matching_path_element/cue.mod/module.cue --
module: "mod.com"
-- 004_multiple_packages_neither_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 004_multiple_packages_neither_matching_path_element/x/y.cue --
package y

y: 5
-- 004_multiple_packages_neither_matching_path_element/x/z.cue --
package z

z: 5
-- 004_multiple_packages_neither_matching_path_element/root_stderr.golden --
import failed: build constraints exclude all CUE files in mod.com/x:
    root.cue: package is root, want x
    x/y.cue: package is y, want x
    x/z.cue: package is z, want x:
    ./root.cue:3:8
-- 004_multiple_packages_neither_matching_path_element/cmd_stderr.golden --
found packages "y" (y.cue) and z (z.cue) in "$WORK/004_multiple_packages_neither_matching_path_element/x"

What did you expect to see?

A passing test.

What did you see instead?

For case 002:

> cd $WORK/002_single_package_not_matching_path_element
$WORK/002_single_package_not_matching_path_element
> ! exec cue eval root.cue
[stderr]
import failed: build constraints exclude all CUE files in mod.com/x:
    root.cue: package is root, want x
    x/y.cue: package is y, want x:
    ./root.cue:3:8
[exit status 1]
> cmp stderr root_stderr.golden
> ! exec cue eval ./x
[stdout]
y: 5
FAIL: /tmp/testscript4096994401/repro.txt/script.txt:18: unexpected command success

i.e. cmd/cue's input resolution logic is more relaxed than the import path resolution logic when a directory contains a single package where the package name does not match the last element of the package path.

For case 003:

> cd $WORK/003_multiple_packages_one_matching_path_element
$WORK/003_multiple_packages_one_matching_path_element
> exec cue eval root.cue
[stdout]
x: 5
> exec cue eval ./x
[stderr]
found packages "x" (x.cue) and y (y.cue) in "$WORK/003_multiple_packages_one_matching_path_element/x"
[exit status 1]
FAIL: /tmp/testscript3540557554/repro.txt/script.txt:29: unexpected command failure
error running repro.txt in /tmp/testscript3540557554/repro.txt

i.e. import resolution logic is more relaxed about the presence of multiple packages in a directory, allowing the specification of path without an explicit package selector, e.g. :x, in case one of those package names matches the last element of the package path. cmd/cue on the other hand always requires a package selector in the presence of multiple packages.

@verdverm
Copy link

My understanding is that the import example is loading a package, which happens to have the same name as the dir, so all goes well. It is also trying to load a package, where as the cli is loading a dir, which always requires the package specifier if more than on package. I suspect the difference may be tied to the eval using load, and import using a different function (?)

I can see the inconsistency being confusing. Changing the import mechanics could break a lot of existing code. I would think having the CLI change would be more ideal, but only in the case there is more than one package and one of them does match the directory name?

@myitcv
Copy link
Member Author

myitcv commented Jul 27, 2021

Changing the import mechanics could break a lot of existing code

Just to be clear, that is not my proposal. We cannot break the import behaviour nor do I think we need/want to.

I would think having the CLI change would be more ideal, but only in the case there is more than one package and one of them does match the directory name?

Exactly, i.e. following the same semantics as the import resolution.

@myitcv myitcv added the Discuss Requires maintainer discussion label Jul 29, 2021
@mpvl mpvl added the embed label Nov 24, 2021
@myitcv myitcv added zzz Discuss and removed Discuss Requires maintainer discussion labels Jan 29, 2022
@myitcv myitcv added Discuss Requires maintainer discussion and removed zzz Discuss labels Feb 9, 2022
@mpvl
Copy link
Member

mpvl commented May 18, 2022

If there are two packages in a directory and one of the packages has the package name of the directory, it should be picked up by the command line.

@mpvl mpvl added NeedsFix and removed Discuss Requires maintainer discussion labels May 18, 2022
@myitcv myitcv changed the title cmd/cue: handle ambiguous packages like imports? cmd/cue: cmd input resolution does not match imports resolution in the presence of multiple packages May 19, 2022
@myitcv myitcv changed the title cmd/cue: cmd input resolution does not match imports resolution in the presence of multiple packages cmd/cue: cmd input resolution logic does not match imports resolution in the presence of multiple packages May 19, 2022
@myitcv myitcv added the zGarden label Jun 15, 2023
@mvdan mvdan removed the zGarden label Feb 8, 2024
cueckoo pushed a commit that referenced this issue May 24, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
cueckoo pushed a commit that referenced this issue May 26, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
cueckoo pushed a commit that referenced this issue May 26, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
cueckoo pushed a commit that referenced this issue May 26, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
cueckoo pushed a commit that referenced this issue May 27, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

 For #3155

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
cueckoo pushed a commit that referenced this issue May 27, 2024
This adds some test cases listed in issue #1138 to make some of the
package resolution semantics explicit in the tests.

 For #3155

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I935e91658b84f419e040c7d9ee845944e8b542e9
@myitcv myitcv self-assigned this May 30, 2024
@myitcv
Copy link
Member Author

myitcv commented May 30, 2024

Updating on the back of changes as of 7ab8002. The following passes as expected:

# 001 - the x directory contains a single package x
#       that matches the package path mod.com/x
#
# Import resolution: ok; resolves to x
# cmd/cue resolution: ok; resolves to x
cd $WORK/001_single_package_matching_path_element
exec cue eval root.cue
exec cue eval ./x
stdout 'x: 5'

# 002 - the x directory contains a single package y
#       that does not match the package path mod.com/x
#
# Import resolution: ok; fails to resolve a package as expected
# cmd/cue resolution: ok; resolves to package x
cd $WORK/002_single_package_not_matching_path_element
! exec cue eval root.cue
cmp stderr root_stderr.golden
exec cue eval ./x
stdout 'y: 5'

# 003 - the x directory contains packages x and y,
#       such that one matches the package path mod.com/x
#
# Import resolution: ok; resolves to x as expected
# cmd/cue resolution: ok; fails to resolve to package x with error
#                     found packages "x" (x.cue) and y (y.cue)
cd $WORK/003_multiple_packages_one_matching_path_element
exec cue eval root.cue
! exec cue eval ./x
stderr 'found packages "x" \(x.cue\) and "y" \(y.cue\)'

# 004 - the x directory contains packages y and z,
#       such that neither matches the package path mod.com/x
#
# Import resolution: ok; fails to resolve a package as expected
# cmd/cue resolution: ok; fails to resolve a package as expected
#                     found packages "x" (x.cue) and y (y.cue)
cd $WORK/004_multiple_packages_neither_matching_path_element
! exec cue eval root.cue
cmp stderr root_stderr.golden
! exec cue eval ./x
stderr 'found packages "y" \(y.cue\) and "z" \(z.cue\)'

-- 001_single_package_matching_path_element/cue.mod/module.cue --
module: "mod.com"
language: version: "v0.9.0"
-- 001_single_package_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 001_single_package_matching_path_element/x/x.cue --
package x

x: 5
-- 002_single_package_not_matching_path_element/cue.mod/module.cue --
module: "mod.com"
language: version: "v0.9.0"
-- 002_single_package_not_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 002_single_package_not_matching_path_element/x/y.cue --
package y

y: 5
-- 002_single_package_not_matching_path_element/root_stderr.golden --
import failed: cannot find package "mod.com/x": no files in package directory with package name "x":
    ./root.cue:3:8
-- 002_single_package_not_matching_path_element/root_stderr.golden --
import failed: cannot find package "mod.com/x": no files in package directory with package name "x":
    ./root.cue:3:8
-- 003_multiple_packages_one_matching_path_element/cue.mod/module.cue --
module: "mod.com"
language: version: "v0.9.0"
-- 003_multiple_packages_one_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 003_multiple_packages_one_matching_path_element/x/x.cue --
package x

x: 5
-- 003_multiple_packages_one_matching_path_element/x/y.cue --
package y

y: 5
-- 004_multiple_packages_neither_matching_path_element/cue.mod/module.cue --
module: "mod.com"
language: version: "v0.9.0"
-- 004_multiple_packages_neither_matching_path_element/root.cue --
package root

import "mod.com/x"

x
-- 004_multiple_packages_neither_matching_path_element/x/y.cue --
package y

y: 5
-- 004_multiple_packages_neither_matching_path_element/x/z.cue --
package z

z: 5
-- 004_multiple_packages_neither_matching_path_element/root_stderr.golden --
import failed: cannot find package "mod.com/x": no files in package directory with package name "x":
    ./root.cue:3:8

We have raised #3184 to more precisely capture the different semantics that now intentionally exist between the command line and imports.

@myitcv myitcv closed this as completed May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: v0.9.0-rc.1
Development

No branches or pull requests

5 participants