indexer: Ensure declared module calls get decoded #1395
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related: hashicorp/terraform-schema#254
The PR is not strictly hard dependency for this one but it is helpful in testing, to verify that the race condition is no longer present.
Context
Some time ago, we discovered a race condition, which may not have been as well understood at the time. We needed to make sure that go-to-definition and go-to-references works on first load for module inputs.
Module inputs are represented as reference origins and as with everything else, the attribute schema is what instructs the job collecting the reference origins how and whether to collect those origins. Obtaining the schema for modules is basically the job of
LoadModuleMetadata
- in particular we generate the schema based on parsed variables & outputs.Prior to this PR, we never processed "module calls" directly from
didOpen
ordidChange
and only relied on the walker to get to them by the time we needed them. This would usually work fine if no documents were open at the time of initial walking. Often times though the walking may take a lot of time for larger workspaces and users may open files very early. This meant submodule data was unavailable when the user opened the module which calls those submodules, making the reference origin collection job "clue-less" about those module inputs.We worked around that race condition by inferring the inputs - basically assuming all declared inputs have their corresponding variable declarations. As with most workarounds, this eventually caught up with us 😅 . In particular, validation (as implemented in #1368) was also acting based on this inferred (potentially incorrect) schema and worse - the inferred schema was assumed to be relevant for all modules with the same
source
.Therefore, hashicorp/terraform-schema#254 removes the workaround and this PR addresses the race condition.
Technically this may result in more jobs getting scheduled but the overall performance impact should be relatively minimal because:
Testing
I am frankly not sure how to best test this (in code) as the problem is usually only visible with high enough number of jobs / large workspaces.
I did test it manually in one of our large repositories and can confirm the "go-to-definition" works cca 10 out of 10 times on continuous reload, whereas previously it would end up broken on every ~5th reload.