hashicorp/terraform · error

The working directory already contains files. The -from-modu

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

errInitCopyNotEmpty (init_run.go:74) is returned when 'terraform init -from-module=SOURCE' is run in a working directory that already contains files. -from-module copies the referenced module into the working dir, which requires an empty destination; init refuses to overwrite existing files. The error message directs the user to omit -from-module to initialize the existing config instead.

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.Output(views.CopyingConfigurationMessage, 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 c9def3e214)

Solutions

  1. Drop -from-module to initialize the existing configuration: 'terraform init'.
  2. If you really want the module copy, clear the working directory first (remove existing .tf files / .terraform).
  3. Use a fresh empty directory for -from-module: 'mkdir new && cd new && terraform init -from-module=SOURCE'.

Example fix

# before
$ terraform init -from-module=git::https://example.com/mod  # dir has .tf files
# after (init existing config)
$ terraform init
Defensive patterns

Strategy: validation

Validate before calling

// Only pass -from-module when the target directory is empty.
func shouldUseFromModule(dir, fromModule string) (string, error) {
    if fromModule == "" { return "", nil }
    entries, err := os.ReadDir(dir)
    if err != nil { return "", err }
    if len(entries) > 0 {
        return "", fmt.Errorf("directory %s not empty; drop -from-module to init existing config", dir)
    }
    return fromModule, nil
}

Prevention

When it happens

Trigger: Line 64-77: initArgs.FromModule != "" triggers configs.IsEmptyDir(path, testsDir); if not empty, errors.New(strings.TrimSpace(errInitCopyNotEmpty)) is appended and run returns 1 before any copy happens.

Common situations: Running 'terraform init -from-module=...' inside a directory that already has .tf files or a .terraform/ dir; a CI step that re-runs init with -from-module against a populated checkout; stale files from a previous init.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/9928d001cdf74bfa. Report an issue: GitHub.