Skip to content

Commit

Permalink
interp: Fix file leak and always cache for now
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Oct 26, 2021
1 parent 826c8bd commit 80a6997
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,15 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam

pathPrefixes := []struct {
prefix string
cache bool
fn func(filename string) (io.Reader, error)
fn func(filename string) (io.ReadCloser, error)
}{
{
"@builtin/", true, func(filename string) (io.Reader, error) {
"@builtin/", func(filename string) (io.ReadCloser, error) {
return builtinFS.Open(filename)
},
},
{
"@config/", false, func(filename string) (io.Reader, error) {
"@config/", func(filename string) (io.ReadCloser, error) {
configDir, err := i.os.ConfigDir()
if err != nil {
return nil, err
Expand All @@ -583,7 +582,7 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
},
},
{
"", false, func(filename string) (io.Reader, error) {
"", func(filename string) (io.ReadCloser, error) {
// TODO: jq $ORIGIN

if filepath.IsAbs(filename) {
Expand All @@ -606,10 +605,8 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
continue
}

if p.cache {
if q, ok := ni.includeCache[filename]; ok {
return q, nil
}
if q, ok := ni.includeCache[filename]; ok {
return q, nil
}

filenamePart := strings.TrimPrefix(filename, p.prefix)
Expand All @@ -618,8 +615,9 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
if !isTry {
return nil, err
}
f = &bytes.Buffer{}
f = io.NopCloser(&bytes.Buffer{})
}
defer f.Close()

b, err := io.ReadAll(f)
if err != nil {
Expand Down Expand Up @@ -691,9 +689,7 @@ func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilenam
i.ImportPath = rewritePath(basePath, i.ImportPath)
}

if p.cache {
i.includeCache[filename] = q
}
i.includeCache[filename] = q

return q, nil
}
Expand Down

0 comments on commit 80a6997

Please sign in to comment.