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

panic when parsing an empty CSV as a datasource #311

Closed
blimmer opened this issue May 2, 2018 · 6 comments
Closed

panic when parsing an empty CSV as a datasource #311

blimmer opened this issue May 2, 2018 · 6 comments
Assignees
Labels

Comments

@blimmer
Copy link

blimmer commented May 2, 2018

I sometimes have an empty or missing datasource that a script tries to pass through as a datasource.

eval "cat ./scripts/$template | ARTIFACT_VERSION=$artifact_version ARTIFACT_NAME=$BUILD_PACKAGE GRPC_VERSION=$GRPC_VERSION PROTOC_VERSION=$PROTOC_VERSION gomplate -d dependencies=$ARTIFACT_DEPENDENCY_CSV > $PACKAGE_DIR/build/$BUILD_LANG/$output_file"

(the important part there is the -d dependencies)

and then that's used in this section of my template:

  {{- range $row := ( datasource "dependencies" ) }}
  spec.add_dependency '{{ index $row 0 }}', '>= {{ index $row 1 }}', '< {{ index $row 2 }}' {{ "\n" }}
  {{- end }}

If the datasource file is not present, gomplate fails out with this error:

2018/05/02 11:15:06 Can't stat /Users/blimmer/code/foo/dependencies.csv: &os.PathError{Op:"stat", Path:"/Users/blimmer/code/foo/dependencies.csv", Err:0x2}

If the datasource is empty, I get this error:

2018/05/02 11:19:42 No value found for [] from datasource 'dependencies'

If the datasource has any contents:

echo "" > $ARTIFACT_DEPENDENCY_CSV

I get this crash:

panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 1 [running]:
text/template.errRecover(0xc42019bc08)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:137 +0x1d4
panic(0x19a8e00, 0x21f9660)
	/usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
github.com/hairyhenderson/gomplate/data.parseCSV(0xc42019ae78, 0x1, 0x1, 0x2583d90, 0x0, 0x1, 0x600, 0xc, 0xc4201501a0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/data/data.go:90 +0x5ed
github.com/hairyhenderson/gomplate/data.CSV(0xc42019ae78, 0x1, 0x1, 0x600, 0xc42016c078, 0x1)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/data/data.go:117 +0x4f
github.com/hairyhenderson/gomplate/data.(*Data).Datasource(0xc420308640, 0xc4200cd157, 0xc, 0x222b1e8, 0x0, 0x0, 0xc42019af70, 0x101144c)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/data/datasource.go:247 +0x59d
github.com/hairyhenderson/gomplate/data.(*Data).Datasource-fm(0xc4200cd157, 0xc, 0x222b1e8, 0x0, 0x0, 0x0, 0x0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/funcs/data.go:22 +0x5c
reflect.Value.call(0x1973a80, 0xc420308660, 0x13, 0x1b1edc5, 0x4, 0xc420154300, 0x2, 0x1, 0x1b148c0, 0x1, ...)
	/usr/local/Cellar/go/1.10/libexec/src/reflect/value.go:447 +0x969
reflect.Value.Call(0x1973a80, 0xc420308660, 0x13, 0xc420154300, 0x1, 0x1, 0x1bf95c0, 0xc42028cf40, 0x18f7dc0)
	/usr/local/Cellar/go/1.10/libexec/src/reflect/value.go:308 +0xa4
text/template.(*state).evalCall(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0x1973a80, 0xc420308660, 0x13, 0x1bf9340, 0xc42030aba0, 0xc4200cd14b, ...)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:667 +0x4f1
text/template.(*state).evalFunction(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc42030abd0, 0x1bf9340, 0xc42030aba0, 0xc4203067a0, 0x2, 0x2, ...)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:535 +0x176
text/template.(*state).evalCommand(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc42030aba0, 0x0, 0x0, 0x0, 0xc4200313e0, 0xc42019b710, ...)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:432 +0x516
text/template.(*state).evalPipeline(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc4200a3130, 0x1b509d8, 0xc42019b7f0, 0xc42016c060)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:405 +0x10f
text/template.(*state).evalCommand(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc42030ab70, 0x0, 0x0, 0x0, 0xc4200313e0, 0x0, ...)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:435 +0x79a
text/template.(*state).evalPipeline(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc4200a30e0, 0xc4200a2068, 0xc42019b988, 0x108916e)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:405 +0x10f
text/template.(*state).walkRange(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0xc420158200)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:314 +0xe9
text/template.(*state).walk(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0x1bf9580, 0xc420158200)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:242 +0x447
text/template.(*state).walk(0xc42019bb88, 0x1981ac0, 0x222b1e8, 0x16, 0x1bf9480, 0xc42030a720)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:239 +0x11d
text/template.(*Template).execute(0xc42028cd40, 0x1bf21e0, 0xc42000c018, 0x1981ac0, 0x222b1e8, 0x0, 0x0)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:194 +0x1e6
text/template.(*Template).Execute(0xc42028cd40, 0x1bf21e0, 0xc42000c018, 0x1981ac0, 0x222b1e8, 0x1838e01, 0x1a19ba0)
	/usr/local/Cellar/go/1.10/libexec/src/text/template/exec.go:177 +0x53
main.(*Gomplate).RunTemplate(0xc42030a5d0, 0xc42030a600, 0x0, 0x0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/gomplate.go:30 +0xb6
main.runTemplate(0x220af80, 0x0, 0x0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/gomplate.go:55 +0x1c4
main.newGomplateCmd.func1(0xc420310000, 0xc4203065a0, 0x0, 0x2, 0x0, 0x0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/main.go:70 +0x70
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).execute(0xc420310000, 0xc42001c0d0, 0x2, 0x2, 0xc420310000, 0xc42001c0d0)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:698 +0x46d
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0xc420310000, 0x2, 0x1b1e668, 0x2)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:783 +0x2e4
github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra.(*Command).Execute(0xc420310000, 0x0, 0x183c151)
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/vendor/github.com/spf13/cobra/command.go:736 +0x2b
main.main()
	/private/tmp/gomplate-20180305-32889-aixq7p/gomplate-2.4.0/src/github.com/hairyhenderson/gomplate/main.go:99 +0x42

This might be related to #286 , but any suggestions on how to work around this scenario?

@hairyhenderson
Copy link
Owner

If the datasource file is not present, gomplate fails out with this error:

that's expected, and could be protected by the new function suggested in #286

If the datasource is empty, I get this error:

gomplate explicitly checks here that there's more than 0 bytes in the datasource. That was part of a fix for #200. This could also be protected by the new function suggested in #286

If the datasource has any contents:
[...]
I get this crash:

So... the crash is ugly, and needs to be fixed (I'm on it). The irony is that because you're trying to reference columns that don't exist, gomplate will error with index out of range anyway, just in a somewhat prettier way 😉.

@hairyhenderson
Copy link
Owner

@blimmer I'm going to rename this issue and once the panic is fixed I'll close this, since it's otherwise a duplicate of #286

@hairyhenderson hairyhenderson changed the title Difficult to handle empty / not present datasources panic when parsing an empty CSV as a datasource May 2, 2018
@hairyhenderson hairyhenderson self-assigned this May 2, 2018
@hairyhenderson
Copy link
Owner

As a side-note, the same crash can be achieved with:

$ gomplate -i '{{ data.CSV "" }}'

@blimmer
Copy link
Author

blimmer commented May 3, 2018

Thank you!

@hairyhenderson
Copy link
Owner

🙇

@hairyhenderson
Copy link
Owner

If the datasource is empty, I get this error:
gomplate explicitly checks here that there's more than 0 bytes in the datasource. That was part of a fix for #200. This could also be protected by the new function suggested in #286

@blimmer just FYI, I've relaxed that check since an empty CSV really shouldn't cause gomplate to error out. That's going in #316

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

2 participants