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

cue/ast/astutil: Sanitize should fail if it cannot produce desired result #3604

Open
rogpeppe opened this issue Nov 29, 2024 · 0 comments
Open
Labels

Comments

@rogpeppe
Copy link
Member

rogpeppe commented Nov 29, 2024

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

$ cue version
v0.11.0

Does this issue reproduce with the latest stable release?

Yes

What did you do?

exec go mod tidy
! exec go run .
stderr 'cannot sanitize'
-- go.mod --
module test

require cuelang.org/go v0.11.0
-- main.go --
package main

import (
	"fmt"
	"log"

	"cuelang.org/go/cue/ast"
	"cuelang.org/go/cue/ast/astutil"
	"cuelang.org/go/cue/format"
)

func main() {
	strValue := ast.NewString("something")
	f := &ast.File{
		Decls: []ast.Decl{
			&ast.Field{
				Label: ast.NewIdent("bar"),
				Value: ast.NewStruct(
					&ast.Field{
						Label: ast.NewIdent("foo"),
						Value: &ast.Ident{
							Name: "bar",
							Node: strValue,
						},
					},
				),
			},
			&ast.Field{
				Label: ast.NewIdent("foo"),
				Value: ast.NewStruct(
					&ast.Field{
						Label: ast.NewIdent("bar"),
						Value: strValue,
					},
				),
			},
		},
	}
	err := astutil.Sanitize(f)
	if err != nil {
		log.Fatal(err)
	}
	data, err := format.Node(f)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", data)
}

What did you expect to see?

A passing test. The sanitization logic is unable to make the bar identifier refer to its associated target node, and the result refers to a different result than intended. This logic can also generate dangling references (eg. if the top level field is not named bar, the result will refer to a non-existent bar).

It would be better if Sanitize returned an error when this happens, as it almost certainly indicates an error in the generating code, rather than returning subtly wrong code.

What did you see instead?

> exec go mod tidy
> ! exec go run .
[stdout]
bar: {
	foo: bar_1
}
foo: {
	bar: "something"
}

let bar_1 = bar

FAIL: /tmp/z.txtar:2: unexpected command success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant