No root module found?

I am using Visual Studio Code

Version: 1.46.1
Commit: cd9ea6488829f560dc949a8b2fb789f3cdc05f5d
Date: 2020-06-17T21:17:14.222Z
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 18.7.0

The Terraform extension is v2.0.2

When I open a terraform module, I get this message…

No root module found for main.tf functionality may be limited

It does not provide any details that are actionable and am confused about what I am supposed to do with this information.

Hi @radag87
You are right that we don’t provide enough actionable details and that’s something I’d like to fix.

The expectation of the language server (and in turn the VS Code extension) is that there is a root module somewhere in the hierarchy of the folder opened in VS Code. Root module in this context basically means a directory of *.tf files has been terraform init-ed.

This warning message in particular comes from the language server and I suppose it could suggest you to run terraform init. Would that be helpful?

Ideally we’d like to go further and let VS Code prompt you to terraform init a directory, but that means we need to understand what directories are the right candidates for init-able root modules. i.e. we don’t know yet where to terraform init. What the server can see is a hierarchy of many folders, many of which may have *.tf files in them, so in theory any of them are init-able.

I have just raised a PR to modify the message to be more actionable: https://github.com/hashicorp/terraform-ls/pull/205

1 Like

Thanks for the feedback. The issue does not seem to be related to the absence or presence of a .terraform directory. Even after I run terraform init, the message appears. At most it is an annoyance. Everything else seems to be working.

@daps-us That certainly isn’t intention and shouldn’t be happening, so I would consider that a bug.

Do you mind sharing some details about the hierarchy of the folder you open in the editor? e.g. tree -a -d . output would be useful, and/or log output as per our Troubleshooting instructions.

Feel free to PGP encrypt either of those via our key as per https://www.hashicorp.com/security#secure-communications if you don’t feel comfortable sharing it in plain-text to a wider audience.

Is there a way to just suppress these warning messages in VScode?
I cloned a terragrunt repo and when navigating between files, it causes one of these popups every time i open a new .tf file.

10011 [07/09/20 16:23] $ tree -a -d -I .git
.
├── .terraform
│   ├── modules
│   │   └── ccoe
│   │       └── modules
│   │           ├── backup
│   │           │   ├── awsbackup
│   │           │   └── dlm
│   │           ├── cloudwatch
│   │           │   ├── cloudwatch-event
│   │           │   └── cloudwatch-log-group
│   │           ├── compute
│   │           │   ├── ec2-instance
│   │           │   │   └── examples
│   │           │   │       ├── basic
│   │           │   │       └── volume-attachment
│   │           │   ├── lambda
│   │           │   └── lambda-permission
│   │           ├── database
│   │           │   └── dynamodb-table
│   │           │       ├── examples
│   │           │       │   └── basic
│   │           │       ├── pay-per-request
│   │           │       └── provisioned
│   │           ├── global
│   │           │   └── tags
│   │           ├── networking
│   │           ├── s3
│   │           │   ├── s3-bucket
│   │           │   └── s3-bucket-object
│   │           ├── security
│   │           │   ├── iam
│   │           │   │   ├── iam-policy
│   │           │   │   ├── iam-role
│   │           │   │   └── iam-role-policy-attachment
│   │           │   └── keypair
│   │           ├── sns
│   │           │   ├── sns-topic
│   │           │   └── sns-topic-policy
│   │           └── ssm
│   │               ├── maintenance-target
│   │               ├── maintenance-task
│   │               ├── maintenance-window
│   │               ├── patch-baseline
│   │               └── patch-group
│   └── plugins
│       └── darwin_amd64
├── images
└── tfvars

48 directories

I had not installed terraform-ls separately. I only used whatever vscode installed. I am installing it now.

Thanks for sharing the tree - that is useful. A few more questions so I can interpret this in the right context:

  • Are all these nested submodules referenced from the root module (e.g. via module { source = "./module/ccoe/modules/backup/awsbackup")?
  • Alternatively do they reference each other (e.g. compute/lambda references compute/lambda-permission)?
  • Which files trigger the warning - i.e. are you opening *.tf files just within the root folder or inside the submodules too? Does it happen in both cases or either of them?
  • If you’re getting the warning for all files within the hierarchy then I suspect the problem is not related to the hierarchy. In such case I’d recommend collecting some logs as they may shed the light on why the initialized root module isn’t being picked up, which may be for a number of reasons:
    • terraform was not found in $PATH
    • it was impossible to obtain terraform version - which may happen e.g. with tfenv in some cases
    • you have incompatible Terraform version in $PATH (<0.12)
    • it was not possible to obtain the schema, because of missing backend auth, invalid config, or any other reason

I had not installed terraform-ls separately. I only used whatever vscode installed. I am installing it now.

You should not need to install the language server separately in order to modify how it’s being launched, you can just modify the args in the settings, e.g.

    "terraform.languageServer": {
        "args": [
            "serve",
            "-log-file=/tmp/terraform-ls-vscode-{{pid}}.log"
        ],
    },

and you should be prompted to restart VS Code and eventually see the log file in the given path when it launches.

All the nested submodules are referenced from the root module.
The modules to do reference each other.
I am only opening .tf files in the root directory.

10020 [07/10/20 16:10] $ which terraform
/Users/raul/.local/bin/terraform

10022 [07/10/20 16:11] $ terraform --version
Terraform v0.12.18

  • provider.aws v2.68.0

Your version of Terraform is out of date! The latest version
is 0.12.28. You can update by downloading from https://www.terraform.io/downloads.html

I can update my version of terraform once the questions are answered.

Okay, here is the output from the logging…

2020/07/10 16:14:55 root_module_manager.go:138: looking up
/Users/raul/repos/aws/resolver-inbound in module references of /Users/raul/repos/aws/ccoe-unbound
2020/07/10 16:14:55 server.go:342: Posting server notification “window/showMessage” {“type”:2,“message”:“No root module found for main.tf. Functionality may be limited. You may need to run terraform init”}
2020/07/10 16:15:04 server.go:469: Received 1 new requests
2020/07/10 16:15:04 server.go:165: Processing 1 requests
2020/07/10 16:15:04 server.go:223: Checking request for “textDocument/didClose”: {“textDocument”:{“uri”:“file:///Users/raul/repos/aws/resolver-inbound/main.tf”}}
2020/07/10 16:15:04 rpc_logger.go:29: Incoming notification for “textDocument/didClose”: {“textDocument”:{“uri”:“file:///Users/raul/repos/aws/resolver-inbound/main.tf”}}
2020/07/10 16:15:04 server.go:469: Received 1 new requests
2020/07/10 16:15:04 server.go:165: Processing 1 requests
2020/07/10 16:15:04 server.go:223: Checking request for “textDocument/didOpen”: {“textDocument”:{“uri”:“file:///Users/raul/repos/aws/resolver-inbound/outputs.tf”,“languageId”:“terraform”,“version”:1,“text”:“output "account_id" {\n value = data.aws_caller_identity.current.account_id\n}\noutput "caller_arn" {\n value = data.aws_caller_identity.current.arn\n}\noutput "caller_user" {\n value = data.aws_caller_identity.current.user_id\n}\noutput "sg_id" {\n value = aws_security_group.sg.id\n}\noutput "sg_allow_tcp_id" {\n value = aws_security_group_rule.allow_dns_tcp.id\n}\noutput "sg_allow_udp_id" {\n value = aws_security_group_rule.allow_dns_udp.id\n}\n”}}
2020/07/10 16:15:04 rpc_logger.go:29: Incoming notification for “textDocument/didOpen”: {“textDocument”:{“uri”:“file:///Users/raul/repos/aws/resolver-inbound/outputs.tf”,“languageId”:“terraform”,“version”:1,“text”:“output "account_id" {\n value = data.aws_caller_identity.current.account_id\n}\noutput "caller_arn" {\n value = data.aws_caller_identity.current.arn\n}\noutput "caller_user" {\n value = data.aws_caller_identity.current.user_id\n}\noutput "sg_id" {\n value = aws_security_group.sg.id\n}\noutput "sg_allow_tcp_id" {\n value = aws_security_group_rule.allow_dns_tcp.id\n}\noutput "sg_allow_udp_id" {\n value = aws_security_group_rule.allow_dns_udp.id\n}\n”}}
2020/07/10 16:15:04 root_module_manager.go:138: looking up /Users/raul/repos/aws/resolver-inbound in module references of /Users/raul/repos/aws/ccoe-unbound
2020/07/10 16:15:04 server.go:342: Posting server notification “window/showMessage” {“type”:2,“message”:“No root module found for outputs.tf. Functionality may be limited. You may need to run terraform init”}

It looks like the information I did not provide may be part of the problem. I have three git projects in the vs code workspace. It appears that the language server is treating them as if they were one common set of code.

After removing the two other projects from the workspace, then the language server stops complaining. I understand the behavior now.

I don’t know if this should be considered a bug or an enhancement request, but it would be useful if I did not have to have one workspace per project as it makes refactoring trickier.

I see - that sounds like a bug, which I also managed to reproduce - good find! :bug: I will open an issue to track it and discuss how we can fix it there.

Thank you for all your debugging effort.

See https://github.com/hashicorp/vscode-terraform/issues/434

I’m seeing this as well when I open any tf file in a submodule. It’s very annoying, as it gets in the way of my console in vscode. Is there any way I can filter this message or check completely? It would really be nice if it was an optional warning, or could have functionality to click on the warning, and say to ignore forever.

Ensure the installed versions of terraform and vs code is the latest one.
The error should disappear after installing the newest versions and running terraform init command.

1 Like

The point is that when developing a module that will be referenced in other repositories I don’t want to initialise a terraform state for the module itself… and I don’t want to be nagged about it every time I open a file either.