#1086·opentofu

Don't make redundant copies of the same module package.

Author: joeaawadCreated Jan 10, 2024Updated Sep 29, 2026
Labelsenhancementrfcpending-decision

Summary

Currently, OpenTofu can make thousands of copies of a single module package, which can be very expensive, both in terms of time to make so many copies and in disk usage. This could be refactored to substantially speed up inits for many users.

Problem Statement

Currently, when using remote module sources (for this example I will use GitHub source, but it applies equally to all types of remote module sources), OpenTofu makes separate copies of the entire module package for every single module source on disk. This means that if I have 100 different module calls that specify anywhere between 1 and 100 different module source sub-directories from a single module package, the behavior we will see is that it will download the module package once from the remote source, then make 99 more local copies of that module package.

This behavior can seem relatively minor, but several types of common OpenTofu usage can make this behavior very painful. First, if a company has a single module package (such as a git repo) where they store all of their shared modules in separate subdirectories that numerous internal teams working in numerous separate repos are able to reference via remote module source. This can make the single module package quite large, but avoids having hundreds of separate tiny repos if you have hundreds of shared modules. Second, if you either have a single large root module that references scores or hundreds of remote modules, or numerous root modules that are run in a since automated job that collectively have hundreds or thousands of module invocations. In these cases, profiling the job can identify that just the local copying of a single module package that is being used thousands of times can take upwards of 40-50% of the total execution time and fill up a ton of disk space.

This is also inconsistent with the behavior when using a local path for the module source. When using local path, the init simply provides a pointer to the existing module package on disk, it does not make any copies of the module package. This can result in meaningful differences if someone is using path.module to write files to disk, as noted in related issues.

User-facing description

The user facing impact is:

  1. Reduced execution time when using a single module package multiple times in a given root module
  2. Reduced disk usage because the module package is not copied multiple times

Technical Description

See related issues, but TLDR is changing the behavior of Tofu init so that instead of copying the initially downloaded module package on disk, OpenTofu just creates a pointer to the initial module package download for subsequent usages.

From a technical implementation standpoint, it should be a fairly trivial change since you are just changing the functionality when OpenTofu sees that it is init-ing a single module package for not the first time so that, instead of the existing behavior of copying the full module package, it invokes the existing functionality that it already uses for local module paths to instead point to the existing module package. The current code to copy a module package could probably be removed unless if it is invoked elsewhere.

Rationale and alternatives

This feature would provide a meaningful performance improvement for power users such as my company, since we have a number of very long running jobs that can take up to as much as an hour to plan a bunch of different Terraform root modules. Having that time get reduced by 40-50% would be a huge reason to cut over to OpenTofu.

Alternatives include not using remote module sources and instead using local module sources with a pre-init step to download and populate a cache with the remote module package at a known path that the local paths point to. This is completely viable, but not as nice of a user experience and requires custom tool that all users at the company would have to use.

Downsides

Technically, this could be considered a breaking change (see related issues). However, my argument is that this should NOT be considered a breaking change because there is currently inconsistent between behavior when using a local module source and a remote module source. This inconsistency has never been documented or recommended and per prior documentation, OpenTofu should always behave exactly the same under the hood regardless of where a module is sourced from. As a result, I see this as a bugfix even if it will break a small percentage of users who are, in my opinion, improperly using path.module for write operations. Also, this could be something that is part of a living document of differences between Terraform and OpenTofu as part of a migration guide so that people can make the relatively minor change of using path.root instead of existing usages of path.module for existing use cases when they migrate.

Unresolved Questions

No response

Related Issues

Proof of Concept

No response