question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot locate module locally

See original GitHub issue

Prerequisites

Installation details

  • IDE version (Help->About->Copy to Clipboard)
    IntelliJ IDEA 2021.2 (Ultimate Edition)
    Build #IU-212.4746.92, built on July 27, 2021
    Runtime version: 11.0.11+9-b1504.13 x86_64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
    macOS 10.15.7
    GC: ParNew, ConcurrentMarkSweep
    Memory: 4029M
    Cores: 8
    Registry: scala.erase.compiler.process.jdk.once=false
    Non-Bundled Plugins: org.intellij.plugins.hcl (0.7.14), mobi.hsz.idea.gitignore (4.2.0), name.kropp.intellij.makefile (212.4746.52), org.toml.lang (0.2.152.4026-212), org.jetbrains.plugins.hocon (2021.1.0), intellij.prettierJS (212.4746.57), Pythonid (212.4746.96), org.jetbrains.plugins.go (212.4746.92), org.intellij.scala (2021.2.15), pro.bashsupport (2.0.4.212)
    Kotlin: 212-1.5.10-release-IJ4746.92
    
  • intellij-hcl plugin version (Settings->Plugins) - 0.7.14
  • Terraform version (terraform -v) - v1.0.10 (darwin_amd64)

Terraform Configuration Files

module "iam" {
  source = "s3::https://s3.amazonaws.com/path/to/module.zip"
}

And here is the project folder (including .terraform cache):

.
├── .terraform
│   ├── modules
│   │   ├── iam
│   │   │   ├── Makefile
│   │   │   ├── main.tf
│   │   │   ├── modules
│   │   │   │   ├── iam
│   │   │   │   │   ├── .terraform.lock.hcl
│   │   │   │   │   ├── main.tf
│   │   │   │   │   ├── outputs.tf
│   │   │   │   │   └── variables.tf
│   │   │   │   ├── local-data
│   │   │   │   │   └── outputs.tf
│   │   │   │   └── path
│   │   │   │       ├── outputs.tf
│   │   │   │       └── variables.tf
│   │   │   ├── output.tf
│   │   │   ├── policies
│   │   │   │   └── assume-role
│   │   │   │       └── codebuild.json
│   │   │   ├── variables.tf
│   │   │   └── versions.tf
│   │   ├── iam.path.path
│   │   │   ├── go.mod
│   │   │   ├── go.sum
│   │   │   ├── infra
│   │   │   ├── output.tf
│   │   │   ├── readme.md
│   │   │   └── variables.tf
│   │   ├── iam.session
│   │   │   ├── Makefile
│   │   │   ├── main.tf
│   │   │   ├── modules
│   │   │   │   └── local-data
│   │   │   │       └── outputs.tf
│   │   │   ├── outputs.tf
│   │   │   └── versions.tf
│   │   └── modules.json
│   └── providers
│       └── registry.terraform.io
│           └── hashicorp
│               └── aws
│                   └── 3.58.0
│                       └── darwin_amd64
│                           └── terraform-provider-aws_v3.58.0_x5
├── .terraform.lock.hcl
└── main.tf

Exception

N/A

Expected Behavior

The HCL plugin should be able to reference any module cached in the ./.terraform in the current directory. We are using Terraform inside of a monorepo, so we have several directories scattered around with Terraform configuration. I have noticed in some project folders we do not have this issue and in others we do. It seems to happen most often within sub-modules (e.g. ./modules/bar, relative to the project root), or when referencing sub-folders of a module, for example:

module "bar" {
  source = "s3::https://s3.amazonaws.com/path/to/module.zip//path/to/sub-module"
}

Actual Behavior

 Cannot locate module locally: Terraform Module 'iam' with source 's3::https://s3.amazonaws.com/path/to/module.zip' directory not found locally, use `terraform get` to fetch modules.

The module exists, of course, and is downloaded and cached in the .terraform folder. Performing Terraform CLI commands in the directory will succeed. It’s just a problem with the plugin cache.

. <-- in this folder the module was resolved, and it also resolved it in the sub-folder
├── Makefile
├── main.tf
├── modules
│   ├── iam <-- the folder where the module cannot be resolved
│   │   └── main.tf
│   └── meta
│       └── outputs.tf
├── output.tf
├── readme.md
├── variables.tf
└── versions.tf

In the example we are downloading the module from S3, but it shouldn’t matter what the source is. Notably, though, I haven’t seen this happen with a local module reference (e.g. source = "../../path/to/module").

Steps to Reproduce

See the comment below https://github.com/VladRassokhin/intellij-hcl/issues/365#issuecomment-981972598

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:33
  • Comments:18

github_iconTop GitHub Comments

73reactions
mohamed-haidara-ckocommented, Dec 30, 2021

Not sure if it’s the same cause but had the same issue with Terraform 1.1.0. They changed how they store the Source in .terraform/modules/modules.json causing my issue. See https://github.com/hashicorp/terraform/pull/28854. Looks like the plugin reads that file.

Fixed it by making sure the Source field in .terraform/modules/modules.json matches exactly the source in the TF file. If doesn’t match, modify it for all the modules in that file and restart the IDE (or close and open the file).

Example for an official module: change registry.terraform.io/terraform-aws-modules/vpc/aws to terraform-aws-modules/vpc/aws

I’m using PyCharm 2021.3 and version 0.7.14 of the plugin. I have also a monorepo with multiple Terraform projects!

57reactions
danpolandcommented, Dec 20, 2021

Alternativlity, you can prepend the source URI in your main.tf with registry.terraform.io/. Not including it is just a short hand way of indicating that the module is hosted on the Terraform registry. By prepending the registry in your source reference you don’t have to touch the files in the cache.

module "vpc" {
  source  = "registry.terraform.io/terraform-aws-modules/vpc/aws"
  version = "3.11.0"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Terraform: cannot locate module locally: unknown reason
Fixed it by making sure the Source field in .terraform/modules/modules.json matches exactly the source in the TF file. If doesn't match, modify it...
Read more >
Can't locate module locally #204 - GitHub
Does it work after invoking terraform get in console or from quick fix? Please run terraform get and include content of .terraform/modules/ ...
Read more >
Terraform Cannot locate module locally: Unknown reason
i am encountering with the issue which is terraform cant not locate the module locally. I really appreciate your advise. Thanks in advance....
Read more >
Anyone else use IntelliJ and lost ability to drill into modules?
Also modules which have sources outside of the current workspace will appear as "Cannot locate module locally, unknown reason".
Read more >
Module Sources | Terraform - HashiCorp Developer
Local paths are special in that they are not "installed" in the same sense that other sources are: the files are already present...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found