-
Notifications
You must be signed in to change notification settings - Fork 131
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
Support single files and parent (non-Terraform) folders #32
Comments
Potentially of note: terragrunt users usually have the I have probably a minority use case, but I actually use pre init hooks in terragrunt to set up my backend configs. The way I have things set up, |
Thanks for sharing that use case @amasover - that's a useful insight! I am not that familiar with Terragrunt and this kind of workflow where If it's 1-to-N, would this functionally be the same as setting a custom plugin cache? In theory we could traverse up a few levels to lookup parent That said we could initially allow setting a static plugin cache path per #24 and potentially make it project-specific settings via some kind of standardised configs? FWIW the only reason the LS needs access to |
The workflow is something more like this: terraform-live repository:
terraform-modules repository:
(No The
Terragrunt then pulls down the module and runs the terraform command that you want against it. The best example of how this works is Terragrunt's example repos: In local development usually you reference a local source. So a typical loop would be, make a change in a module, and try it out from a terraform-live environment via I think the problem here is that it's more like an N-to-1 mapping where multiple copies of modules can exist, for different environments in Ultimately though, when you edit a module, you're doing it in the terraform-modules directory, not in the terraform-live directory. So your editor should see the project root being somewhere in terraform-modules, and look there for the I think static plugin cache with standardized configs would also be great in this situation -- then you could check the configs into source control, and it should work for everyone who needs to edit modules. My understanding is, if the LS supports that, then we wouldn't need to directly init in the terraform-modules directory, because the LS can get schema info directly from the plugin cache? I tried to set up juliosueiras's terraform-lsp in this way, but I was only ever able to get language server features to work after running |
Different scenarios (will start to populate these with examples): Repo / Directory ScenariosSingle root module / repoNo subdirectory modules, all modules referenced from external sources (registry, etc). Repo root is Terraform root. Nested submoduleRepo root is terraform root, subdirectories are modules sourced from the root. Example:Our internal Nested root moduleRepo root is module, sub directories can contain multiple root Terraform modules. Example:Open at repo root: https://github.com/terraform-aws-modules/terraform-aws-security-group Multiple Root TerraformRepo root is not a Terraform module, subfolders are root Terraform modules. This is probably easier solved on the editor side, but opening each subfolder as an additional directory in a VS Code "workspace" (or Sublime "project"), etc. and running multiple language servers for each one. Submodule only / No Terraform rootIf someone is working on a submodule in a repository without any test root Terraform modules, there may be no way to acquire schema information / initialize. Mostly listing this for completeness, we could potentially try to guess at the providers/schema. Its possible you could walk up the tree and see if there was an initialized TF directory somewhere above the repo? Single File ScenariosI think for the most part we should skip all but the single file in a repo root scenario in the near term. Single file in Terraform rootThis is relatively straightforward, probably follows the same path as repo root being Terraform root. Single file not in Terraform rootSimilar to the Submodule only scenario above, you could potentially walk up the tree, but otherwise not sure what else you could do. |
I'm using the Main Module Approach with CI, QA, and Prod environments. The CI environments includes the main module as a local module (`source = "../../main"), while the QA and Prod environments use git module source. This allows me to pin QA and Prod to a specific commit or tag. It would be helpful to be able to select the directory containing the terraform root. |
Just an update for anyone following this issue, this is our primary focus at the moment, #167 introduced some refactoring to make this simpler. The intended approach here has two primary things we are looking to add
|
Just throwing my setup out there since @radeksimko mentioned this was the correct issue... Under @paultyng's categories, I'm using "Multiple Root Terraform." If possible [eventually], it would be nice to keep opening the root folder in VScode since then I can use workspace history to switch between them. Sub-projects might have very similar names like
In this case subproject one and two might have different providers. It would be nice for the language server to work up the folder tree for the currently active file and find where its parent project providers are initialized for suggesting the appropriate autocompletes, etc. I have different git branches for different environments, but they are really more like pointers along a linear git history. The custom variables derivation sourced by Thanks for all your work making the extension/language server officially maintained! |
We use Atlantis to apply our multi project repo. There is a subdirectory per project and we reference each project in the Here's an example of our atlantis.yaml file: version: 3
automerge: true
projects:
- name: projecta
dir: projecta
workflow: default
autoplan:
enabled: true
apply_requirements: [approved, mergeable]
- name: projectb
dir: terraform/projectb
workflow: default
autoplan:
enabled: true
apply_requirements: [approved, mergeable]
- name: projectc
dir: terraform/projectc
workflow: default
autoplan:
enabled: true
apply_requirements: [approved, mergeable]
workflows:
default:
plan:
steps:
- init
- plan:
extra_args: ["-var-file", "atlantis.tfvars"] The repo layout is something like:
|
Thank you for the examples you have all posted so far! These are useful. #176 (once released) will introduce support for a number of different hierarchies. Generally all scenarios where there is only a single matching root (even if nested) should be supported fully, i.e. case where you are editing a local submodule which is only references by a single There's some more work to be done around picking the right candidates when there's more than 1, some ideas on how to do that are outlined in #179 and #180 - any feedback to these, or alternative ideas are welcomed! With that in mind: @danieladams456 I believe this hierarchy is covered by https://github.com/hashicorp/terraform-ls/tree/master/internal/terraform/rootmodule/testdata/multi-root-no-modules @billyshambrook your hierarchy looks similar/same as @danieladams456 's, but you would need to @magne @amasover both "main module approach" and Terragrunt should work, but it's highly likely that a wrong root module will be matched, if all of your environments are |
@radeksimko Actually multi-root-local-modules-down comes into play for me as well. I had just omitted it for brevity assuming it would be the same since it was self contained. You all are doing great work!!! |
This should support the following hierarchies https://github.com/hashicorp/terraform-ls/tree/master/internal/terraform/rootmodule/testdata with the caveats that wrong root module may be picked up when there is multiple candidates (see #179 and #180 ) and these root modules have to be Keep in mind there is also #128 which you may still run into - workaround is mentioned in the thread. |
To add some context around closing:
|
This functionality has been released in v0.27.0 of the language server. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
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:
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:
initialize
via$PATH
)initialize
)init
ed providers (happens duringinitialize
)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
'ssource
is path in a local filesystem) may fall into this category as such modules tend to be notinit
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
~
home dir)rootUri
is empty (single file was open) and store a flag to say "LS is in single file mode"terraform-ls/internal/langserver/handlers/did_change_workspace_folders.go
Lines 62 to 77 in 99e30e1
The text was updated successfully, but these errors were encountered: