Skip to content

Commit

Permalink
wip - chicken and egg problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Oct 1, 2024
1 parent 89238e4 commit 180223e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
16 changes: 1 addition & 15 deletions internal/features/rootmodules/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"path/filepath"

"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/eventbus"
"github.com/hashicorp/terraform-ls/internal/features/rootmodules/ast"
"github.com/hashicorp/terraform-ls/internal/features/rootmodules/jobs"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/datadir"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
Expand Down Expand Up @@ -94,9 +92,6 @@ func (f *RootModulesFeature) didOpen(ctx context.Context, dir document.DirHandle
return jobs.ParseTerraformSources(ctx, f.fs, f.Store, dir.Path())
},
Type: op.OpTypeParseTerraformSources.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
return f.indexTerraformSourcesDirs(ctx, dir) // TODO: find out why this does not exist for mod manifest
},
})
if err != nil {
return ids, err
Expand Down Expand Up @@ -246,16 +241,7 @@ func (f *RootModulesFeature) indexTerraformSourcesDirs(ctx context.Context, dir

for _, subDir := range f.Store.TerraformSourcesDirectories(dir.Path()) {
dh := document.DirHandleFromPath(filepath.Join(dir.Path(), subDir))

// notify the event bus that a module has been opened
// to trigger decoding of the module and its providers
// this is done differently for declared module calls in normal TF code
spawnedIds := f.eventbus.DidOpen(eventbus.DidOpenEvent{
Context: ctx,
Dir: dh,
LanguageID: lsp.Terraform.String(),
})
jobIds = append(jobIds, spawnedIds...)
f.stateStore.WalkerPaths.EnqueueDir(ctx, dh)
}

return jobIds, nil
Expand Down
1 change: 0 additions & 1 deletion internal/features/rootmodules/state/root_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ func (s *RootStore) InstalledModuleCalls(path string) (map[string]tfmod.Installe
}
}
}
// TODO: support TerraformSources as well here

return installed, err
}
Expand Down
26 changes: 18 additions & 8 deletions internal/features/stacks/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast"
"github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/module"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func (f *StacksFeature) discover(path string, files []string) error {
Expand Down Expand Up @@ -216,7 +216,7 @@ func (f *StacksFeature) decodeStack(ctx context.Context, dir document.DirHandle,
f.logger.Printf("loading module metadata returned error: %s", jobErr)
}

componentIds, err := loadStackComponentSources(ctx, f.store, f.bus, path)
componentIds, err := f.decodeStackComponentSources(ctx, f.store, f.bus, path)
deferIds = append(deferIds, componentIds...)
if err != nil {
f.logger.Printf("loading stack component sources returned error: %s", err)
Expand Down Expand Up @@ -329,8 +329,8 @@ func (f *StacksFeature) removeIndexedStack(rawPath string) {
}
}

// loadStackComponentSources will trigger parsing the local terraform modules for a stack in the ModulesFeature
func loadStackComponentSources(ctx context.Context, stackStore *state.StackStore, bus *eventbus.EventBus, stackPath string) (job.IDs, error) {
// decodeStackComponentSources will trigger parsing the local terraform modules for a stack in the ModulesFeature
func (f *StacksFeature) decodeStackComponentSources(ctx context.Context, stackStore *state.StackStore, bus *eventbus.EventBus, stackPath string) (job.IDs, error) {
ids := make(job.IDs, 0)

record, err := stackStore.StackRecordByPath(stackPath)
Expand All @@ -348,12 +348,22 @@ func loadStackComponentSources(ctx context.Context, stackStore *state.StackStore
var fullPath string
// detect if component.Source is a local module
switch component.SourceAddr.(type) {
case module.LocalSourceAddr:
case tfmod.LocalSourceAddr:
fullPath = filepath.Join(stackPath, filepath.FromSlash(component.Source))
// For registry modules, we need to find the local installation path (if installed)
case tfaddr.Module:
continue
case module.RemoteSourceAddr:
continue
installedDir, ok := f.rootFeature.InstalledModulePath(stackPath, component.SourceAddr.String())
if !ok {
continue
}
fullPath = filepath.Join(stackPath, filepath.FromSlash(installedDir))
// For other remote modules, we need to find the local installation path (if installed)
case tfmod.RemoteSourceAddr:
installedDir, ok := f.rootFeature.InstalledModulePath(stackPath, component.SourceAddr.String())
if !ok {
continue
}
fullPath = filepath.Join(stackPath, filepath.FromSlash(installedDir))
default:
// Unknown source address, we can't resolve the path
continue
Expand Down

0 comments on commit 180223e

Please sign in to comment.