diff --git a/imports.go b/imports.go index 56e79b59e..93ca92046 100644 --- a/imports.go +++ b/imports.go @@ -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), } } @@ -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 } diff --git a/main_test.go b/main_test.go index faf08d881..37fd5193a 100644 --- a/main_test.go +++ b/main_test.go @@ -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) + } +}