{"record":{"id":"b3a5115d018bc129","repo":"hashicorp/terraform","slug":"errinitcopynotempty","errorCode":"errInitCopyNotEmpty","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.LogConfigurationCopyingStart(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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/init_run.go#L56-L92","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Remove -from-module if the configuration is already in the working directory.","Run init -from-module in a clean/empty directory instead.","Delete or move existing files from the target directory before using -from-module.","Use a fresh temporary directory for module scaffolding."],"exampleFix":"// before: directory already has main.tf\nterraform init -from-module=git::https://github.com/org/module\n// error: The working directory already contains files.\n\n// after (option 1: init without -from-module)\nterraform init\n// after (option 2: use empty dir)\nmkdir scaffold && cd scaffold\nterraform init -from-module=git::https://github.com/org/module","handlingStrategy":"validation","validationCode":"// Validate the directory is empty before using -from-module:\n// entries, err := os.ReadDir(workdir)\n// if err != nil || len(entries) > 0 {\n//     return fmt.Errorf(\"directory must be empty for -from-module\")\n// }\n// exec.Command(\"terraform\", \"init\", \"-from-module=\"+src)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only use -from-module with a clean, empty working directory.","In CI, create a fresh directory for -from-module scaffolding.","Omit -from-module when the configuration already exists in the directory."],"tags":["terraform","cli","init","from-module","argument-validation"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}