{"record":{"id":"9928d001cdf74bfa","repo":"hashicorp/terraform","slug":"the-working-directory-already-contains-files-the","errorCode":null,"errorMessage":"The working directory already contains files. The -from-module option requires\nan empty directory into which a copy of the referenced module will be placed.\n\nTo initialize the configuration already in this working directory, omit the\n-from-module option.","messagePattern":"The working directory already contains files\\. The -from-module option requires\nan empty directory into which a copy of the referenced module will be placed\\.\n\nTo initialize the configuration already in this working directory, omit the\n-from-module option\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/init_run.go","lineNumber":74,"sourceCode":"\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\n\t// Initialization can be aborted by interruption signals\n\tctx, done := c.InterruptibleContext(c.CommandContext())\n\tdefer done()\n\n\tif initArgs.FromModule != \"\" {\n\t\tsrc := initArgs.FromModule\n\n\t\tempty, err := configs.IsEmptyDir(path, initArgs.TestsDirectory)\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error validating destination directory: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tif !empty {\n\t\t\tdiags = diags.Append(errors.New(strings.TrimSpace(errInitCopyNotEmpty)))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tview.Output(views.CopyingConfigurationMessage, src)\n\n\t\thooks := uiModuleInstallHooks{\n\t\t\tUi:             c.Ui,\n\t\t\tShowLocalPaths: false, // since they are in a weird location for init\n\t\t\tView:           view,\n\t\t}\n\n\t\tctx, span := tracer.Start(ctx, \"-from-module=...\", trace.WithAttributes(\n\t\t\tattribute.String(\"module_source\", src),\n\t\t))\n\n\t\tinitDirFromModuleAbort, initDirFromModuleDiags := c.initDirFromModule(ctx, path, src, hooks)\n\t\tdiags = diags.Append(initDirFromModuleDiags)","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/init_run.go#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Drop -from-module to initialize the existing configuration: 'terraform init'.","If you really want the module copy, clear the working directory first (remove existing .tf files / .terraform).","Use a fresh empty directory for -from-module: 'mkdir new && cd new && terraform init -from-module=SOURCE'."],"exampleFix":"# before\n$ terraform init -from-module=git::https://example.com/mod  # dir has .tf files\n# after (init existing config)\n$ terraform init","handlingStrategy":"validation","validationCode":"// Only pass -from-module when the target directory is empty.\nfunc shouldUseFromModule(dir, fromModule string) (string, error) {\n    if fromModule == \"\" { return \"\", nil }\n    entries, err := os.ReadDir(dir)\n    if err != nil { return \"\", err }\n    if len(entries) > 0 {\n        return \"\", fmt.Errorf(\"directory %s not empty; drop -from-module to init existing config\", dir)\n    }\n    return fromModule, nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use -from-module only with a fresh empty directory.","Drop -from-module when the directory already holds configuration.","Clean the working dir in CI before re-running init with -from-module."],"tags":["init","from-module","empty-directory","validation"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}