Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder: fix race condition in terraform-schema SchemaMerger #1086

Merged
merged 2 commits into from
Oct 12, 2022
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/terraform-exec v0.17.3
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/hashicorp/terraform-schema v0.0.0-20220913132140-68f302837af7
github.com/hashicorp/terraform-schema v0.0.0-20221012150949-418ecbd2378f
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.4
github.com/mitchellh/go-homedir v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-schema v0.0.0-20220913132140-68f302837af7 h1:qUNpbYuxTUzl/fZxKJUdtYvbS0h8EwZ0DP3oXDFr0lA=
github.com/hashicorp/terraform-schema v0.0.0-20220913132140-68f302837af7/go.mod h1:y+84rv+azuczq9xn+oQycRyZSMiXO7rYLRZh46zXnEE=
github.com/hashicorp/terraform-schema v0.0.0-20221012150949-418ecbd2378f h1:WTKUS67a+uv2FNadSKSpbAluQEwEnT3AI/jowfNwuYc=
github.com/hashicorp/terraform-schema v0.0.0-20221012150949-418ecbd2378f/go.mod h1:3rsW13O6EbVKChpSLioPONkF1oqYx+yOe5ztEfBaL8A=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
154 changes: 154 additions & 0 deletions internal/decoder/decoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package decoder_test

import (
"bytes"
"compress/gzip"
"context"
"io"
"io/fs"
"log"
"path"
"path/filepath"
"sync"
"testing"
"testing/fstest"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
idecoder "github.com/hashicorp/terraform-ls/internal/decoder"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
)

func TestDecoder_CodeLensesForFile_concurrencyBug(t *testing.T) {
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}

logger := log.New(io.Discard, "", 0)
testCfg := `data "terraform_remote_state" "vpc" { }
`
dirNames := []string{"testdir1", "testdir2"}

mapFs := fstest.MapFS{}
for _, dirName := range dirNames {
mapFs[dirName] = &fstest.MapFile{Mode: fs.ModeDir}
mapFs[path.Join(dirName, "main.tf")] = &fstest.MapFile{Data: []byte(testCfg)}
mapFs[filepath.Join(dirName, "main.tf")] = &fstest.MapFile{Data: []byte(testCfg)}
}

ctx := context.Background()

dataDir := "data"
schemasFs := fstest.MapFS{
dataDir: &fstest.MapFile{Mode: fs.ModeDir},
dataDir + "/terraform.io": &fstest.MapFile{Mode: fs.ModeDir},
dataDir + "/terraform.io/builtin": &fstest.MapFile{Mode: fs.ModeDir},
dataDir + "/terraform.io/builtin/terraform": &fstest.MapFile{Mode: fs.ModeDir},
dataDir + "/terraform.io/builtin/terraform/1.0.0": &fstest.MapFile{Mode: fs.ModeDir},
dataDir + "/terraform.io/builtin/terraform/1.0.0/schema.json.gz": &fstest.MapFile{
Data: gzipCompressBytes(t, []byte(tfSchemaJSON)),
},
}

for _, dirName := range dirNames {
err := ss.Modules.Add(dirName)
if err != nil {
t.Error(err)
}
err = module.ParseModuleConfiguration(ctx, mapFs, ss.Modules, dirName)
if err != nil {
t.Error(err)
}
err = module.LoadModuleMetadata(ctx, ss.Modules, dirName)
if err != nil {
t.Error(err)
}
err = module.PreloadEmbeddedSchema(ctx, logger, schemasFs, ss.Modules, ss.ProviderSchemas, dirName)
}

d := decoder.NewDecoder(&idecoder.PathReader{
ModuleReader: ss.Modules,
SchemaReader: ss.ProviderSchemas,
})

var wg sync.WaitGroup
for _, dirName := range dirNames {
dirName := dirName
wg.Add(1)
go func(t *testing.T) {
defer wg.Done()
_, err := d.CodeLensesForFile(ctx, lang.Path{
Path: dirName,
LanguageID: "terraform",
}, "main.tf")
if err != nil {
t.Error(err)
}
}(t)
}
wg.Wait()
}

func gzipCompressBytes(t *testing.T, b []byte) []byte {
var compressedBytes bytes.Buffer
gw := gzip.NewWriter(&compressedBytes)
_, err := gw.Write(b)
if err != nil {
t.Fatal(err)
}
err = gw.Close()
if err != nil {
t.Fatal(err)
}
return compressedBytes.Bytes()
}

var tfSchemaJSON = `{
"format_version": "1.0",
"provider_schemas": {
"terraform.io/builtin/terraform": {
"data_source_schemas": {
"terraform_remote_state": {
"version": 0,
"block": {
"attributes": {
"backend": {
"type": "string",
"description": "The remote backend to use, e.g. remote or http.",
"description_kind": "markdown",
"required": true
},
"config": {
"type": "dynamic",
"description": "The configuration of the remote backend. Although this is optional, most backends require some configuration.\n\nThe object can use any arguments that would be valid in the equivalent terraform { backend \"\u003cTYPE\u003e\" { ... } } block.",
"description_kind": "markdown",
"optional": true
},
"defaults": {
"type": "dynamic",
"description": "Default values for outputs, in case the state file is empty or lacks a required output.",
"description_kind": "markdown",
"optional": true
},
"outputs": {
"type": "dynamic",
"description": "An object containing every root-level output in the remote state.",
"description_kind": "markdown",
"computed": true
},
"workspace": {
"type": "string",
"description": "The Terraform workspace to use, if the backend supports workspaces.",
"description_kind": "markdown",
"optional": true
}
},
"description_kind": "plain"
}
}
}
}
}
}`
3 changes: 3 additions & 0 deletions internal/schemas/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"path"

"github.com/hashicorp/go-version"
Expand All @@ -32,9 +33,11 @@ func (e SchemaNotAvailable) Error() string {
func FindProviderSchemaFile(filesystem fs.ReadDirFS, pAddr tfaddr.Provider) (*ProviderSchema, error) {
providerPath := path.Join("data", pAddr.Hostname.String(), pAddr.Namespace, pAddr.Type)

log.Printf("looking for dir at %q", providerPath)
entries, err := fs.ReadDir(filesystem, providerPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
log.Printf("dir not exist: %#v", err)
return nil, SchemaNotAvailable{Addr: pAddr}
}
return nil, err
Expand Down