Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

nil pointer when file names are in constants #137

Open
joefrancia opened this issue Dec 14, 2020 · 2 comments
Open

nil pointer when file names are in constants #137

joefrancia opened this issue Dec 14, 2020 · 2 comments

Comments

@joefrancia
Copy link

I was trying to run pkger to bundle my files and getting a "runtime error: invalid memory address or nil pointer dereference".

I had the file names stored in constants for the pkger.Open() calls. I changed all pkger.Open() calls to use a string literal and no longer get the error.

Is this due to the parser not being able to reference constants as noted in issue #46 ?

@aviddiviner
Copy link

Also ran into this just now.

@lkingland
Copy link

lkingland commented Aug 12, 2021

tl;dr: Use primitives or constants defined in the same file: static analysis can't run the code; just read it.

Assuming a program consisting of two source files a.go which defines a constant and b.go which uses the constant as a name for pkger.Open, a nil reference will be generated:

a.go

package p

const path = "/files"

b.go

package p

import "github.com/markbates/pkger"

func init() {
  pkger.Open(path)
}
> pkger
2021/08/12 15:33:02 runtime error: invalid memory address or nil pointer dereference

Moving the constant into b.go, or using a hard-coded string as the parameter sent to pkger.Open both will avoid the error.

The ast.Ident object used inParsedSource.valueIdent yields the error. The Node in this situation has value:
&ast.Ident{NamePos:113, Name:"path", Obj:(*ast.Object)(nil)}

An informative error would be better than a nil pointer reference, but I am not sure the best place to put that check, nor of the best wording, or I would submit a PR.

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

No branches or pull requests

3 participants