@@ -102,12 +102,29 @@ func NewEmbeddedFS(data []byte) fs.ReadDirFS {
102102	efs  :=  & embeddedFS {data : data , files : make (map [string ]* embeddedFileInfo )}
103103	efs .meta  =  sync .OnceValue (func () * EmbeddedMeta  {
104104		var  meta  EmbeddedMeta 
105+ 
106+ 		// look for the separator newline between binary data and JSON metadata 
107+ 		// jsonv2 may end with an extra newline 
105108		p  :=  bytes .LastIndexByte (data , '\n' )
106109		if  p  <  0  {
107110			return  & meta 
108111		}
109- 		if  err  :=  json .Unmarshal (data [p + 1 :], & meta ); err  !=  nil  {
110- 			panic ("embedded file is not valid" )
112+ 
113+ 		// if the data ends with a newline, look for the previous newline 
114+ 		// to find the real separator 
115+ 		if  p  ==  len (data )- 1  {
116+ 			p  =  bytes .LastIndexByte (data [:p ], '\n' )
117+ 			if  p  <  0  {
118+ 				return  & meta 
119+ 			}
120+ 		}
121+ 
122+ 		jsonData  :=  data [p + 1 :]
123+ 		if  err  :=  json .Unmarshal (jsonData , & meta ); err  !=  nil  {
124+ 			panic ("embedded file is not valid: "  +  err .Error ())
125+ 		}
126+ 		if  meta .Root  ==  nil  {
127+ 			panic ("embedded file metadata has nil root" )
111128		}
112129		return  & meta 
113130	})
@@ -150,9 +167,15 @@ func (e *embeddedFS) getFileInfo(fullName string) (*embeddedFileInfo, error) {
150167
151168	fields  :=  strings .Split (fullName , "/" )
152169	fi  =  e .meta ().Root 
170+ 	if  fi  ==  nil  {
171+ 		return  nil , fs .ErrNotExist 
172+ 	}
153173	if  fullName  !=  "."  {
154174		found  :=  true 
155175		for  _ , field  :=  range  fields  {
176+ 			if  fi .Children  ==  nil  {
177+ 				return  nil , fs .ErrNotExist 
178+ 			}
156179			for  _ , child  :=  range  fi .Children  {
157180				if  found  =  child .BaseName  ==  field ; found  {
158181					fi  =  child 
0 commit comments