Skip to content

Commit

Permalink
cue/load: remove support for cue.mod files
Browse files Browse the repository at this point in the history
We transitioned to a cue.mod directory with a cue.mod/module.cue file
all the way back in 2019: https://cue-review.googlesource.com/c/cue/+/3880

We continued supporting cue.mod files as if they were cue.mod/module.cue
during a transition period, to not break user's modules straight away.
It has been nearly five years now, and the fix is simple enough:

    mv cue.mod cue.mod-backup
    mkdir cue.mod
    mv cue.mod-backup cue.mod/module.cue

Remove support for the legacy file mode for good,
as well as a bit of code in `cue mod init` which did the rewrite above.
Note that a few tests still used it; update those accordingly.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I0a26f6153d58b9b60165e3060099f1d8eed2d4c0
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1188428
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Mar 28, 2024
1 parent 65b948a commit 2887786
Show file tree
Hide file tree
Showing 38 changed files with 41 additions and 89 deletions.
12 changes: 4 additions & 8 deletions cmd/cue/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ func runFixAll(cmd *Command, args []string) error {
args = []string{"./..."}

for {
if fi, err := os.Stat(filepath.Join(dir, "cue.mod")); err == nil {
if fi.IsDir() {
args = appendDirs(args, filepath.Join(dir, "cue.mod", "gen"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "pkg"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "usr"))
} else {
args = appendDirs(args, filepath.Join(dir, "pkg"))
}
if _, err := os.Stat(filepath.Join(dir, "cue.mod")); err == nil {
args = appendDirs(args, filepath.Join(dir, "cue.mod", "gen"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "pkg"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "usr"))
break
}

Expand Down
50 changes: 3 additions & 47 deletions cmd/cue/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -119,19 +118,10 @@ func runModInit(cmd *Command, args []string) (err error) {
}

mod := filepath.Join(cwd, "cue.mod")

info, err := os.Stat(mod)

// Detect old setups and backport it if requested.
if err == nil && !info.IsDir() {
// This path backports
if !flagForce.Bool(cmd) {
return fmt.Errorf("detected old-style config file; use --force to upgrade")
if info, err := os.Stat(mod); err == nil {
if !info.IsDir() {
return fmt.Errorf("cue.mod files are no longer supported; use cue.mod/module.cue")
}
return backport(mod, cwd)
}

if err == nil {
return fmt.Errorf("cue.mod directory already exists")
}
mf := &modfile.File{
Expand Down Expand Up @@ -166,40 +156,6 @@ func runModInit(cmd *Command, args []string) (err error) {
return err
}

// backport backports an old cue.mod setup to a new one.
func backport(mod, cwd string) error {
tmp := filepath.Join(cwd, fmt.Sprintf("_%x_cue.mod", rand.Int()))
err := os.Rename(mod, tmp)
if err != nil {
return err
}

err = os.Mkdir(filepath.Join(cwd, "cue.mod"), 0755)
if err != nil {
os.Rename(tmp, mod)
return err
}

err = os.Rename(tmp, filepath.Join(cwd, "cue.mod", "module.cue"))
if err != nil {
return err
}

err = os.Rename(filepath.Join(cwd, "pkg"), filepath.Join(cwd, "cue.mod", "gen"))
if err != nil && !os.IsNotExist(err) {
return err
}

if err = os.Mkdir(filepath.Join(mod, "usr"), 0755); err != nil {
return err
}
if err = os.Mkdir(filepath.Join(mod, "pkg"), 0755); err != nil {
return err
}

return nil
}

func versionForModFile() string {
version := cueVersion()
if gomodule.IsPseudoVersion(version) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_after.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ command: after: {
-- task.cue --
package home

-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_concurrent.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ command: runtwo: {
text: "foo: \(foo.stdout)\nbar: \(bar.stdout)"
}
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_dep.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ command: do: {
}
}

-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_dir.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ command: ls: {
}
}

-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_import.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ command: pkg: {
}
}

-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_mkdirtmp.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ command: mkdirtmp: {
success: true
}
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_print.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ command: print: {
}
-- task.cue --
package home
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_ref.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ command: ref: {

-- task.cue --
package home
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_stdin.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ command: noreads: {
cmd: ["echo", "not reading any stdin"]
}
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/def_jsonschema.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ age: conflicting values "twenty" and int (mismatched types string and int):
age: conflicting values "twenty" and int (mismatched types string and int):
./data.yaml:1:6
./schema.json:18:7
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/def_proto.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ message Timestamp {
int32 nanos = 2;
}

-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/eval_context.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ cmp stdout expect-stdout
"kind": "Service",
"@name": "elem3"
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/export.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ package hello

$type: "demo"
message: "Hello \(#who)!" // who declared in data.cue
-- hello/cue.mod --
-- hello/cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/export_err.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ a: b: [0, 1, {c: int}, 3]
// Issue #553
b: c: "hello"
out: "d is \(b.d)"
-- exporterr/cue.mod --
-- exporterr/cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/export_list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ cmp stdout expect-stdout
"name": "supplement\nfoo",
"json": "[1, 2]"
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/export_yaml.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ test: {
command: ["foo", "bar"]
}
}
-- hello/cue.mod --
-- hello/cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/fmt.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import a.b "foo"

a: 2
bb: 3
-- fmt/cue.mod --
-- fmt/cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/fmt_stdin.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ a: {b: 3} // a comment
-- expect-stdout --
foo: 2
a: {b: 3} // a comment
-- fmt/cue.mod --
-- fmt/cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/get_go_types.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ go 1.21
module mod.test

go 1.21
-- cue.mod --
-- cue.mod/module.cue --
module: "mod.test"
-- pkg1/alias.go --
package pkg1
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/help_cmd.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exec cue help cmd
cmp stdout expect-stdout

-- cue.mod --
-- cue.mod/module.cue --
-- task_tool.cue --
package home

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/help_cmd_flags.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmp stdout expect-stdout
exec cue cmd --help
cmp stdout expect-stdout

-- cue.mod --
-- cue.mod/module.cue --
-- task_tool.cue --
package home

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/help_hello.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exec cue help cmd hello
cmp stdout expect-stdout

-- cue.mod --
-- cue.mod/module.cue --
-- task_tool.cue --
package home

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/import_context.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ cmp stdout expect-stdout
"kind": "Service",
"@name": "elem3"
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/import_files.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ json: "[1, 2]"
"name": "supplement\nfoo",
"json": "[1, 2]"
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/import_list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ deployment: [{
[{"b": 2}]
-- import3/data.json --
[1]
-- cue.mod --
-- cue.mod/module.cue --

-- issue2721/empty.yaml --
-- issue2721/empty.cue.golden --
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/import_path.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ service: "supplement\nfoo": {
"name": "supplement\nfoo",
"json": "[1, 2]"
}
-- cue.mod --
-- cue.mod/module.cue --
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/merge_interaction.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ command: dump: {
text: yaml.MarshalStream(objects)
}
}
-- cue.mod --
-- cue.mod/module.cue --

-- expect-stdout --
spec: {}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/trim.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ group: {

comp: baz: {} // TODO: remove: implied by comprehension above
}
-- cue.mod --
-- cue.mod/module.cue --
8 changes: 4 additions & 4 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package load

import (
"context"
"fmt"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -407,11 +408,10 @@ func (c *Config) loadModule() error {
if cerr != nil {
return nil
}
// TODO remove support for legacy non-directory module.cue file
// by returning an error if info.IsDir is false.
if info.IsDir() {
mod = filepath.Join(mod, moduleFile)
if !info.IsDir() {
return fmt.Errorf("cue.mod files are no longer supported; use cue.mod/module.cue")
}
mod = filepath.Join(mod, moduleFile)
f, cerr := c.fileSystem.openFile(mod)
if cerr != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func TestOverlays(t *testing.T) {
c := &Config{
Overlay: map[string]Source{
// Not necessary, but nice to add.
abs("cue.mod"): FromString(`module: "mod.test"`),
abs("cue.mod/module.cue"): FromString(`module: "mod.test"`),

abs("dir/top.cue"): FromBytes([]byte(`
package top
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion encoding/gocode/testdata/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
}

for _, d := range dirs {
if !d.IsDir() {
if !d.IsDir() || d.Name() == "cue.mod" {
continue
}
dir := filepath.Join(cwd, "testdata")
Expand Down
File renamed without changes.

0 comments on commit 2887786

Please sign in to comment.