{"record":{"id":"84ba72139ad85818","repo":"grafana/k6","slug":"s-already-exists-use-the-force-flag-to-overw","errorCode":null,"errorMessage":"%s already exists. Use the `--force` flag to overwrite it","messagePattern":"(.+?) already exists\\. Use the `--force` flag to overwrite it","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/cmd/new.go","lineNumber":44,"sourceCode":"\tflags.SortFlags = false\n\tflags.BoolVarP(&c.overwriteFiles, \"force\", \"f\", false, \"overwrite existing files\")\n\tflags.StringVar(&c.templateType, \"template\", \"minimal\", \"template type (choices: minimal, protocol, browser) or relative/absolute path to a custom template file\") //nolint:lll\n\tflags.StringVar(&c.projectID, \"project-id\", \"\", \"specify the Grafana Cloud project ID for the test\")\n\treturn flags\n}\n\nfunc (c *newScriptCmd) run(_ *cobra.Command, args []string) (err error) {\n\ttarget := defaultNewScriptName\n\tif len(args) > 0 {\n\t\ttarget = args[0]\n\t}\n\n\tfileExists, err := fsext.Exists(c.gs.FS, target)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif fileExists && !c.overwriteFiles {\n\t\treturn fmt.Errorf(\"%s already exists. Use the `--force` flag to overwrite it\", target)\n\t}\n\n\t// Initialize template manager and validate template before creating any files\n\ttm, err := templates.NewTemplateManager(c.gs.FS)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error initializing template manager: %w\", err)\n\t}\n\n\ttmpl, err := tm.GetTemplate(c.templateType)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error retrieving template: %w\", err)\n\t}\n\n\t// Prepare template arguments\n\targsStruct := templates.TemplateArgs{\n\t\tScriptName: target,\n\t\tProjectID:  c.projectID,\n\t}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/new.go#L26-L62","documentation":"Returned by `k6 new` when the target script file (the positional arg, default 'script.js') already exists on the filesystem and the --force/-f flag was not provided. This is a deliberate guard against clobbering an existing script; the error names the target and tells you the exact escape hatch.","triggerScenarios":"Running `k6 new script.js` twice in a row; running `k6 new` with no arg in a directory that already has script.js; generating into a path that a previous tool created.","commonSituations":"Re-running scaffolding after a failed first attempt; CI/bootstrap steps that assume a clean workspace; teammates scaffolding into an initialized repo.","solutions":["Pass --force to overwrite: `k6 new script.js --force`","Or choose a fresh target name: `k6 new load-test.js`","Or move/back up the existing file first if you need its contents"],"exampleFix":"# before\n$ k6 new script.js\n# error: script.js already exists. Use the `--force` flag to overwrite it\n\n# after\n$ k6 new script.js --force","handlingStrategy":"validation","validationCode":"# Idempotent scaffolding: decide overwrite policy up front\ntarget=\"script.js\"\nif [ -e \"$target\" ] && [ \"$K6_NEW_FORCE\" != \"1\" ]; then\n  echo \"$target exists — skipping k6 new (set K6_NEW_FORCE=1 to overwrite)\"\nelse\n  k6 new \"$target\" $([ \"$K6_NEW_FORCE\" = \"1\" ] && echo --force)\nfi","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check file existence in bootstrap scripts before calling k6 new","Use --force only in throwaway/workspaces where overwrite is intended","Keep generated scripts in git so accidental overwrites are recoverable"],"tags":["cli","filesystem","scaffolding"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}