Skip to content

Support single files and parent (non-Terraform) folders #32

Closed
@radeksimko

Description

@radeksimko

Single files

A restriction is currently in place that prevents the Language Server from communicating with an IDE (client) that opened a single file. As a result it enforces the user to open whole folders.

This is done by failing initialization with the following error:

Editing a single file is not yet supported. Please open a directory.

Why

The restriction prevents the LS from having to deal with the potentially increased complexity in any logic that concerns the relationship between a file and its plugins (providers).

The LS has responsibility for:

  • finding compatible Terraform binary (happens during initialize via $PATH)
  • finding and storing Terraform version (happens during initialize)
  • retrieving and caching schema for inited providers (happens during initialize)
  • invalidating the cache and retrieving it again when providers change (happens any time via watching the lock file in a plugin folder)

The initialize method is called only once for every opened folder, which is mainly where the lower complexity comes from.

Parent (non-Terraform) folders

This also affects users who wish to open a folder higher up the filesystem hierarchy, e.g. a folder with all Terraform workspaces, as opposed to opening workspaces individually. We do not support such case today either, but don't actually raise any error in this case (yet?).

The same reasoning and complexity scope applies here too though and it's likely that both use cases have the same (or very similar) solution.

Local Modules

Users with locally stored modules (where module's source is path in a local filesystem) may fall into this category as such modules tend to be not inited within their own folder (i.e. such modules often don't have their own .terraform folder). Supporting them will rely heavily on how their provider inheritance is set up - i.e. we need to understand where can we get the provider schemas from for every module.

Future

If we support single files, we need to consider the above happening at different times (probably during didOpen) and significantly more often. Hence we need to ensure this scales well in terms of resource consumption - e.g. by ensuring that we never watch/refresh the same plugin directory twice within the context of a running server process. We may also need to decouple the Terraform binary discovery and version handling, to ensure we only do it once per workspace.

Related

This limitation also helps us avoid bugs similar to juliosueiras/terraform-lsp#58

Proposal

  • walker: introduce more limited walking mode, such that it doesn't descend into lower directories (to prevent unexpected outcomes if user opens ~ home dir)
  • initialize handler: avoid indexing if rootUri is empty (single file was open) and store a flag to say "LS is in single file mode"
  • didOpen handler: index on-demand if "LS is in single file mode" - basically just call
    modPath, err := uri.PathFromURI(added.URI)
    if err != nil {
    jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
    Type: lsp.Warning,
    Message: fmt.Sprintf("Ignoring new workspace folder %s: %s."+
    " This is most likely bug, please report it.", added.URI, err),
    })
    continue
    }
    err = watcher.AddModule(modPath)
    if err != nil {
    svc.logger.Printf("failed to add module to watcher: %s", err)
    continue
    }
    walker.EnqueuePath(modPath)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestmodulesFunctionality related to the module block and modules generally

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions