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

tk fmt panic when encountering unescaped regex in string #566

Closed
mihaitodor opened this issue Jun 3, 2021 · 5 comments
Closed

tk fmt panic when encountering unescaped regex in string #566

mihaitodor opened this issue Jun 3, 2021 · 5 comments

Comments

@mihaitodor
Copy link
Contributor

mihaitodor commented Jun 3, 2021

Given this invalid jsonnet:

{
  x: {
    data: '([^:]+)(?::\d+)?',
  },
}

running ./tk fmt test.jsonnet (latest main - 774c664) panics with the following stack:

panic: Badly formatted string, should have been caught in lexer.

goroutine 1 [running]:
github.com/google/go-jsonnet/internal/formatter.(*EnforceStringStyle).LiteralString(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0001b8370, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/enforce_string_style.go:45 +0x1e5
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002ce170, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:431 +0x297
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectField(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002ce0f0, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:160 +0x193
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectFields(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0001b8490, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:191 +0x76
github.com/google/go-jsonnet/internal/pass.(*Base).Object(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0001b8420, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:331 +0x66
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002ce350, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:435 +0x8a3
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectField(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002ce2d0, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:160 +0x193
github.com/google/go-jsonnet/internal/pass.(*Base).ObjectFields(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0001b8540, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:191 +0x76
github.com/google/go-jsonnet/internal/pass.(*Base).Object(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0001b84d0, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:331 +0x66
github.com/google/go-jsonnet/internal/pass.(*Base).Visit(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002b0200, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:435 +0x8a3
github.com/google/go-jsonnet/internal/pass.(*Base).File(0xc0002a63f0, 0x1580280, 0xc0002a63f0, 0xc0002b0200, 0xc00000e9d8)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/pass/pass.go:461 +0x88
github.com/google/go-jsonnet/internal/formatter.visitFile(...)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/jsonnetfmt.go:133
github.com/google/go-jsonnet/internal/formatter.Format(0x7ffeefbff9e1, 0xc, 0xc0002a6390, 0x2f, 0x2, 0x2, 0x1, 0x1, 0x1010001, 0x0, ...)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/internal/formatter/jsonnetfmt.go:168 +0x5f6
github.com/google/go-jsonnet/formatter.Format(...)
	/Users/ntodor/Projects/go/pkg/mod/github.com/google/go-jsonnet@v0.17.0/formatter/formatter.go:47
github.com/grafana/tanka/pkg/tanka.Format(0x7ffeefbff9e1, 0xc, 0xc0002a6390, 0x2f, 0x2f, 0x0, 0x0, 0x1)
	/Users/ntodor/Projects/oss/tanka/pkg/tanka/format.go:89 +0xeb
github.com/grafana/tanka/pkg/tanka.FormatFiles(0xc0002b0090, 0x1, 0x1, 0xc00014dd48, 0x0, 0x7cb1fb8, 0xc0002b01d0, 0x0, 0x0)
	/Users/ntodor/Projects/oss/tanka/pkg/tanka/format.go:66 +0x2cc
main.fmtCmd.func2(0xc0001e2460, 0xc0002b0090, 0x1, 0x1, 0x0, 0x0)
	/Users/ntodor/Projects/oss/tanka/cmd/tk/fmt.go:72 +0x22c
github.com/go-clix/cli.(*Command).execute(0xc0001e2460, 0xc0002b0070, 0x1, 0x1, 0xc0001e2460, 0xc0002b0070)
	/Users/ntodor/Projects/go/pkg/mod/github.com/go-clix/cli@v0.1.1/command.go:118 +0x291
github.com/go-clix/cli.(*Command).Execute(0xc0001b0d20, 0x0, 0x0)
	/Users/ntodor/Projects/go/pkg/mod/github.com/go-clix/cli@v0.1.1/command.go:74 +0x157
main.main()
	/Users/ntodor/Projects/oss/tanka/cmd/tk/main.go:58 +0x2eb

LE: I tried updating go-jsonnet to the latest version with go get -u github.com/google/go-jsonnet@12bd29d, but I still see the same issue. Please let me know if you wish me to open an issue on the go-jsonnet side too.

@stale
Copy link

stale bot commented Jul 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 8, 2021
@mihaitodor
Copy link
Contributor Author

mihaitodor commented Jul 8, 2021

@sbarzowski I think this might be of interest to you. Please let me know if you'd like me to open an issue for it on the go-jsonnet repo.

@stale stale bot removed the stale label Jul 8, 2021
@Duologic
Copy link
Member

I think you just have to escape the slash:

{
  x: {
    data: '([^:]+)(?::\\d+)?',
  },
}

I'm not an expert on this but I'm pretty sure this is part of the JSON spec, the \ is a reserved character: https://datatracker.ietf.org/doc/html/rfc8259#section-7

@mihaitodor
Copy link
Contributor Author

I agree, but I assume panicking might not be the ideal behaviour here. I'll see if I can repro it on the go-jsonnet side and open a bug there.

@Duologic
Copy link
Member

Good point, feel free to reopen if this panic is Tanka specific

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants