Description
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
init
ed providers (happens duringinitialize
) - 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 init
ed 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