hashicorp/terraform · error

errInitCopyNotEmpty

errInitCopyNotEmpty

Error message

The working directory already contains files. The -from-module option requires
an empty directory into which a copy of the referenced module will be placed.

To initialize the configuration already in this working directory, omit the
-from-module option.

What it means

Returned at init_run.go:74 when 'terraform init -from-module=<source>' is used but the destination working directory is not empty. The -from-module option copies a module into the working directory, which requires an empty target. configs.IsEmptyDir() returns false, triggering the error. The message advises omitting -from-module if the configuration is already present.

Source

Thrown at internal/command/init_run.go:74

		view.Diagnostics(diags)
		return 1
	}

	// Initialization can be aborted by interruption signals
	ctx, done := c.InterruptibleContext(c.CommandContext())
	defer done()

	if initArgs.FromModule != "" {
		src := initArgs.FromModule

		empty, err := configs.IsEmptyDir(path, initArgs.TestsDirectory)
		if err != nil {
			diags = diags.Append(fmt.Errorf("Error validating destination directory: %s", err))
			view.Diagnostics(diags)
			return 1
		}
		if !empty {
			diags = diags.Append(errors.New(strings.TrimSpace(errInitCopyNotEmpty)))
			view.Diagnostics(diags)
			return 1
		}

		view.LogConfigurationCopyingStart(src)

		hooks := uiModuleInstallHooks{
			Ui:             c.Ui,
			ShowLocalPaths: false, // since they are in a weird location for init
			View:           view,
		}

		ctx, span := tracer.Start(ctx, "-from-module=...", trace.WithAttributes(
			attribute.String("module_source", src),
		))

		initDirFromModuleAbort, initDirFromModuleDiags := c.initDirFromModule(ctx, path, src, hooks)
		diags = diags.Append(initDirFromModuleDiags)

View on GitHub (pinned to d32a084675)

Solutions

  1. Remove -from-module if the configuration is already in the working directory.
  2. Run init -from-module in a clean/empty directory instead.
  3. Delete or move existing files from the target directory before using -from-module.
  4. Use a fresh temporary directory for module scaffolding.

Example fix

// before: directory already has main.tf
terraform init -from-module=git::https://github.com/org/module
// error: The working directory already contains files.

// after (option 1: init without -from-module)
terraform init
// after (option 2: use empty dir)
mkdir scaffold && cd scaffold
terraform init -from-module=git::https://github.com/org/module
Defensive patterns

Strategy: validation

Validate before calling

// Validate the directory is empty before using -from-module:
// entries, err := os.ReadDir(workdir)
// if err != nil || len(entries) > 0 {
//     return fmt.Errorf("directory must be empty for -from-module")
// }
// exec.Command("terraform", "init", "-from-module="+src)

Prevention

When it happens

Trigger: Running 'terraform init -from-module=git::https://...' (or any module source) in a directory that already contains .tf files or other content. init_run.go:69-70 calls configs.IsEmptyDir() and init_run.go:71-72 checks '!empty'.

Common situations: Running init with -from-module in a project directory that already has configuration files; CI pipelines that clone a repo and then try to init from a module; scaffolding tools that pre-populate a directory.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/b3a5115d018bc129. Report an issue: GitHub.