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

Complete module source and version #1024

Merged
merged 11 commits into from
Aug 11, 2022
Merged

Complete module source and version #1024

merged 11 commits into from
Aug 11, 2022

Conversation

dbanck
Copy link
Member

@dbanck dbanck commented Aug 2, 2022

Closes hashicorp/vscode-terraform#672


This PR adds three hooks for completing module source and version.

CompleteLocalModuleSources

Uses the local indexed modules and offers relative path suggestions, when completing module sources.

2022-08-09 17 51 38

CompleteRegistryModuleSources

Searches the Terraform registry modules via Algolia and fetches the full source address and module description.

2022-08-09 17 55 08

For this to work, we now need to pass main.algoliaAppID and main.algoliaAPIKey via ldflags on build.

CompleteRegistryModuleVersions

2022-08-09 17 55 32

Fetches the available module versions from the Terraform registry.

Deferred work

Completion of required inputs was moved to #1029

@dbanck dbanck added the enhancement New feature or request label Aug 2, 2022
@dbanck dbanck self-assigned this Aug 2, 2022
@radeksimko radeksimko added this to the v0.29.0 milestone Aug 5, 2022
@dbanck dbanck force-pushed the f-complete-source-version branch from a2ea679 to 83a1835 Compare August 8, 2022 14:44
@dbanck dbanck force-pushed the f-complete-source-version branch from 83a1835 to 67d0f41 Compare August 8, 2022 16:18
@dbanck dbanck force-pushed the f-complete-source-version branch from 8897ba4 to ff56bd0 Compare August 9, 2022 13:08
@dbanck dbanck marked this pull request as ready for review August 9, 2022 15:59
@dbanck dbanck requested a review from a team as a code owner August 9, 2022 15:59
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works great for me! I just left some implementation-related questions and suggestions in-line.

internal/cmd/serve_command.go Outdated Show resolved Hide resolved
Comment on lines 168 to 178
func WithAlgoliaCredentials(ctx context.Context, algoliaAppID, algoliaAPIKey string) context.Context {
return context.WithValue(ctx, ctxAlgoliaCredentials, algolia.AlgoliaCredentials{
AlgoliaAppID: algoliaAppID,
AlgoliaAPIKey: algoliaAPIKey,
})
}

func AlgoliaCredentials(ctx context.Context) (algolia.AlgoliaCredentials, bool) {
credentials, ok := ctx.Value(ctxAlgoliaCredentials).(algolia.AlgoliaCredentials)
return credentials, ok
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the context package was/is used quite a lot for various things, but it actually represents some technical debt in that a better pattern is keeping the context helpers With* and FromContext within the package which produces the data we end up adding to the context - i.e. within algolia package, so that it's more self-contained.

}

for _, mod := range modules {
if strings.Contains(mod.Path, datadir.DataDirName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this also match anything.terraformanything? i.e. do we not need to match the filepath.Separator on both sides here?

continue
}
if mod.Path == path.Path {
// Exclude the current module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Exclude the current module
// Exclude the module we're providing completion in
// to avoid cyclic references

Comment on lines 37 to 39
if !strings.HasPrefix(relPath, ".") {
relPath = fmt.Sprintf("./%s", relPath)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary here? 🤔

internal/hooks/module_version_test.go Show resolved Hide resolved
}
})
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to also test early cancellation of the request via context.


for _, mod := range modules {
if strings.Contains(mod.Path, datadir.DataDirName) {
// Skip anything from the data directory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Skip anything from the data directory
// Skip installed module copies in cache directories

var algoliaAppID = ""

// Algolia API key which should be used for searching Terraform registry modules
var algoliaAPIKey = ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind updating the GoReleaser config as well to pass these?

I have just added the secrets to the repo as ALGOLIA_API_KEY and ALGOLIA_APP_ID.


modules, err := h.fetchModulesFromAlgolia(ctx, prefix)
if err != nil {
return candidates, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we ignore these errors from hooks upstream https://github.com/hashicorp/hcl-lang/blob/b66451713db4e6e3bcf6cde7d70bd8cb51cc2729/decoder/expression_candidates.go#L278

is it worth logging it here? e.g. by plumbing logger to Hooks and using it here.

@dbanck dbanck requested a review from radeksimko August 11, 2022 11:55
Copy link
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

Just one minor (non-blocking) note.

ctx.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources
credentials, ok := algolia.CredentialsFromContext(s.srvCtx)
if ok {
h.AlgoliaClient = search.NewClient(credentials.AppID, credentials.APIKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I suppose we could also avoid registering the completion hook if the credentials aren't available?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. I think the code looks a bit cleaner if we always register the hook and have it exiting early. So it's up to the hook to decide if all requirements are met for a run.

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Completion of module source and version
2 participants