cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.14k stars 294 forks source link

cmd/cue: executing the `import` command twice does not give the same result #850

Open cueckoo opened 3 years ago

cueckoo commented 3 years ago

Originally opened by @juanjoku in https://github.com/cuelang/cue/issues/850

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

It can be reproduced with cue-v0.2.2 and cue-v0.3.0beta.7.

Does this issue reproduce with the latest release?

Yes

What did you do?

We have found a strange behavior in the command cue import.

Suppose this directory structure:

├── dir1
│   ├── dir2
│   │   └── file3.json
│   └── file2.json
└── file1.json

The first time cue import is executed, only file3.json is converted:

$ cue import -f -p mypackage ./dir1/dir2
$ tree
.
├── dir1
│   ├── dir2
│   │   ├── file3.cue
│   │   └── file3.json
│   └── file2.json
└── file1.json

But, if the same command is executed again, file1.json and file2.json are converted !!

$ cue import -f -p mypackage ./dir1/dir2
$ tree
.
├── dir1
│   ├── dir2
│   │   ├── file3.cue
│   │   └── file3.json
│   ├── file2.cue
│   └── file2.json
├── file1.cue
└── file1.json

The workaround to avoid this behaviour is specify "files" instead of "directory" as source for cue import:

$ cue import -f -p mypackage ./dir1/dir2/* 
$ cue import -f -p mypackage ./dir1/dir2/*  # twice
$ tree
.
├── dir1
│   ├── dir2
│   │   ├── file3.cue  # only file3.json has been converted
│   │   └── file3.json
│   └── file2.json
└── file1.json

What did you expect to see?

The same result in both executions (it is not clear to me which is expected)

cueckoo commented 3 years ago

Original reply by @myitcv in https://github.com/cuelang/cue/issues/850#issuecomment-805069233

Hi @juanjoku - thanks for raising your first issue in the CUE project!

Just as an FYI, there are some tips on how to create clean reproducers in https://github.com/cuelang/cue/wiki/Creating-test-or-performance-reproducers. The txtar format is particularly good when it comes to multi-file repros, as is the case here.

For example, here is my reproducer based on the above:

# Run 1
exec cue import -f -p mypackage ./dir1/dir2
! exists file1.cue
! exists dir1/file2.cue
exists dir1/dir2/file3.cue

# Run 2
exec cue import -f -p mypackage ./dir1/dir2
! exists file1.cue
! exists dir1/file2.cue
exists dir1/dir2/file3.cue

-- cue.mod/module.cue --
module: "mod.com"
-- dir1/dir2/file3.json --
{
    "File3": true
}
-- dir1/file2.json --
{
    "File2": true
}
-- file1.json --
{
    "File1": true
}

What's interesting to note is this requires the -p flag, because the following "succeeds" (but is generally useless because the .cue files do not have package clauses):

# Run 1
exec cue import -f ./dir1/dir2
! exists file1.cue
! exists dir1/file2.cue
exists dir1/dir2/file3.cue

# Run 2
exec cue import -f ./dir1/dir2
! exists file1.cue
! exists dir1/file2.cue
exists dir1/dir2/file3.cue

-- cue.mod/module.cue --
module: "mod.com"
-- dir1/dir2/file3.json --
{
    "File3": true
}
-- dir1/file2.json --
{
    "File2": true
}
-- file1.json --
{
    "File1": true
}

So what you have observed is indeed a bug. Confirmed at tip (27d305c2)

cueckoo commented 3 years ago

Original reply by @juanjoku in https://github.com/cuelang/cue/issues/850#issuecomment-805669121

OK! (thx)

cueckoo commented 3 years ago

Original reply by @myitcv in https://github.com/cuelang/cue/issues/850#issuecomment-805773755

Note this also failed in v0.2.2 - so we will fix this after the release of v0.3.0 (so as not to delay that any longer)

juanjoku commented 1 year ago

I think this issue has been resolved, or at least I have not reproduced it with CUE v0.4.3.

myitcv commented 1 year ago

Dropping the now meaningless v0.4.x milestone. Marking for the CUE team to verify.