Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,22 @@ func MakeContentsRaw(bytes []byte) Contents {
// It also verifies that the content pointer is the same for two foundAt values.
type importCache struct {
foundAtVerification map[string]Contents
astCache map[string]ast.Node
astCache map[string]astCacheEntry
codeCache map[string]potentialValue
importer Importer
}

type astCacheEntry struct {
node ast.Node
err error
}

// makeImportCache creates an importCache using an Importer.
func makeImportCache(importer Importer) *importCache {
return &importCache{
importer: importer,
foundAtVerification: make(map[string]Contents),
astCache: make(map[string]ast.Node),
astCache: make(map[string]astCacheEntry),
codeCache: make(map[string]potentialValue),
}
}
Expand Down Expand Up @@ -137,11 +142,11 @@ func (cache *importCache) importAST(importedFrom, importedPath string) (ast.Node
if err != nil {
return nil, "", err
}
if cachedNode, isCached := cache.astCache[foundAt]; isCached {
return cachedNode, foundAt, nil
if entry, isCached := cache.astCache[foundAt]; isCached {
return entry.node, foundAt, entry.err
}
node, err := program.SnippetToAST(ast.DiagnosticFileName(foundAt), foundAt, contents.String())
cache.astCache[foundAt] = node
cache.astCache[foundAt] = astCacheEntry{node: node, err: err}
return node, foundAt, err
}

Expand Down
17 changes: 17 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,20 @@ func TestEvalUnusualFilenames(t *testing.T) {
})
}
}

func TestEvalCachedErrorTwice(t *testing.T) {
// Evaluate the same erroneous file twice in the same VM.
// See https://github.com/google/go-jsonnet/issues/822
vm := MakeVM()
_, err1 := vm.EvaluateFile("testdata/syntax_error.jsonnet")
if err1 == nil {
t.Fatal("expected an error")
}
_, err2 := vm.EvaluateFile("testdata/syntax_error.jsonnet")
if err2 == nil {
t.Fatal("expected an error")
}
if err1.Error() != err2.Error() {
t.Fatalf("different error on the second run:\n1st: %q\n2nd: %q", err1, err2)
}
}