{"record":{"id":"0d903d827725b3f5","repo":"charmbracelet/crush","slug":"failed-to-create-file-w","errorCode":null,"errorMessage":"failed to create file: %w","messagePattern":"failed to create file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lsp/util/edit.go","lineNumber":188,"sourceCode":"// applyDocumentChange applies a DocumentChange (create/rename/delete operations)\nfunc applyDocumentChange(change protocol.DocumentChange, encoding powernap.OffsetEncoding) error {\n\tif change.CreateFile != nil {\n\t\tpath, err := change.CreateFile.URI.Path()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"invalid URI: %w\", err)\n\t\t}\n\n\t\tif change.CreateFile.Options != nil {\n\t\t\tif change.CreateFile.Options.Overwrite {\n\t\t\t\t// Proceed with overwrite\n\t\t\t} else if change.CreateFile.Options.IgnoreIfExists {\n\t\t\t\tif _, err := os.Stat(path); err == nil {\n\t\t\t\t\treturn nil // File exists and we're ignoring it\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif err := os.WriteFile(path, []byte(\"\"), 0o644); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create file: %w\", err)\n\t\t}\n\t}\n\n\tif change.DeleteFile != nil {\n\t\tpath, err := change.DeleteFile.URI.Path()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"invalid URI: %w\", err)\n\t\t}\n\n\t\tif change.DeleteFile.Options != nil && change.DeleteFile.Options.Recursive {\n\t\t\tif err := os.RemoveAll(path); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to delete directory recursively: %w\", err)\n\t\t\t}\n\t\t} else {\n\t\t\tif err := os.Remove(path); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to delete file: %w\", err)\n\t\t\t}\n\t\t}","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/lsp/util/edit.go#L170-L206","documentation":"When applying a CreateFile document change, applyDocumentChange creates an empty file with os.WriteFile. This error wraps any failure (permission denied, parent directory missing, filesystem read-only). Options like Overwrite/IgnoreIfExists are handled before this call, so this is a hard create failure.","triggerScenarios":"CreateFile change where the target directory does not exist, the process lacks write permission, or the filesystem is read-only and Overwrite/IgnoreIfExists do not bypass the write.","commonSituations":"Creating a file in a directory that was deleted concurrently; new-file refactors targeting a package folder not yet created; sandboxed environments without write access.","solutions":["Create the parent directory (os.MkdirAll) before applying the change.","Check write permission on the target directory.","Ensure the filesystem/mount is writable.","If the file already exists and should not be replaced, set IgnoreIfExists in CreateFileOptions."],"exampleFix":"// before\n// parent dir missing\nchange.CreateFile = &protocol.CreateFile{URI: newURI}\n// after\nos.MkdirAll(filepath.Dir(path), 0o755)\nchange.CreateFile = &protocol.CreateFile{URI: newURI}","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(path)\nif info, err := os.Stat(dir); err != nil || !info.IsDir() {\n    os.MkdirAll(dir, 0o755)\n}","typeGuard":null,"tryCatchPattern":"var mkErr *fs.PathError\nif errors.As(err, &mkErr) && errors.Is(mkErr.Err, fs.ErrNotExist) {\n    os.MkdirAll(filepath.Dir(mkErr.Path), 0o755) // then retry create\n}","preventionTips":["Create parent directories before CreateFile changes.","Verify directory write permissions.","Use IgnoreIfExists when the file may already exist."],"tags":["lsp","file-io","create-file"],"backgroundTag":"file-create-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}