{"record":{"id":"9d20b8bd624ebf32","repo":"Lightning-AI/pytorch-lightning","slug":"self-class-name-expected-config-path-t","errorCode":null,"errorMessage":"{self.__class__.__name__} expected {config_path} to NOT exist. Aborting to avoid overwriting results of a previous run. You can delete the previous config file, set `LightningCLI(save_config_callback=None)` to disable config saving, or set `LightningCLI(save_config_kwargs={\"overwrite\": True})` to overwrite the config file.","messagePattern":"(.+?) expected (.+?) to NOT exist\\. Aborting to avoid overwriting results of a previous run\\. You can delete the previous config file, set `LightningCLI\\(save_config_callback=None\\)` to disable config saving, or set `LightningCLI\\(save_config_kwargs=(.+?)\\)` to overwrite the config file\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/cli.py","lineNumber":278,"sourceCode":"\n    @override\n    def setup(self, trainer: Trainer, pl_module: LightningModule, stage: str) -> None:\n        if self.already_saved:\n            return\n\n        if self.save_to_log_dir:\n            log_dir = trainer.log_dir  # this broadcasts the directory\n            assert log_dir is not None\n            config_path = os.path.join(log_dir, self.config_filename)\n            fs = get_filesystem(log_dir)\n\n            if not self.overwrite:\n                # check if the file exists on rank 0\n                file_exists = fs.isfile(config_path) if trainer.is_global_zero else False\n                # broadcast whether to fail to all ranks\n                file_exists = trainer.strategy.broadcast(file_exists)\n                if file_exists:\n                    raise RuntimeError(\n                        f\"{self.__class__.__name__} expected {config_path} to NOT exist. Aborting to avoid overwriting\"\n                        \" results of a previous run. You can delete the previous config file,\"\n                        \" set `LightningCLI(save_config_callback=None)` to disable config saving,\"\n                        ' or set `LightningCLI(save_config_kwargs={\"overwrite\": True})` to overwrite the config file.'\n                    )\n\n            if trainer.is_global_zero:\n                # save only on rank zero to avoid race conditions.\n                # the `log_dir` needs to be created as we rely on the logger to do it usually\n                # but it hasn't logged anything at this point\n                fs.makedirs(log_dir, exist_ok=True)\n                self.parser.save(\n                    self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile\n                )\n\n        if trainer.is_global_zero:\n            self.save_config(trainer, pl_module, stage)\n            self.already_saved = True","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/cli.py#L260-L296","documentation":"SaveConfigCallback.setup writes config.yaml into the log dir and, unless overwrite=True, refuses to clobber an existing file: rank 0 checks existence and broadcasts the result so all ranks raise the same RuntimeError. This protects results of a previous run pointed at by the same version dir.","triggerScenarios":"Re-running with Trainer(default_root_dir=..., version=123) or reusing a fixed log_dir (e.g. via resume) where <log_dir>/config.yaml already exists, with LightningCLI defaults (overwrite=False).","commonSituations":"Manual experiment reruns with a hard-coded version number; shell scripts rerun after a crash that already wrote config.yaml; multi-rank jobs where only rank 0's check matters but all ranks must fail in sync.","solutions":["Delete the existing config.yaml (or the whole version dir) before rerunning","Let Lightning auto-version: omit version= so a fresh folder is used each run","Set LightningCLI(save_config_kwargs={'overwrite': True})","Disable saving: LightningCLI(save_config_callback=None)"],"exampleFix":"# before\ncli = LightningCLI(MyModule, save_config_kwargs={})\n# rerun with same log_dir -> RuntimeError\n# after\ncli = LightningCLI(MyModule, save_config_kwargs={\"overwrite\": True})\n# or: rm -rf lightning_logs/version_123 before rerun","handlingStrategy":"validation","validationCode":"from fsspec.implementations.local import LocalFileSystem\nimport os\n\ndef config_exists(log_dir, name='config.yaml') -> bool:\n    return os.path.isfile(os.path.join(log_dir, name))\n\nif rerun and config_exists(log_dir):\n    os.remove(os.path.join(log_dir, 'config.yaml'))  # or pass overwrite=True","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Let Lightning auto-increment version dirs instead of hard-coding versions","Pass save_config_kwargs={'overwrite': True} in scripted reruns","Delete stale version dirs at the start of rerun scripts"],"tags":["lightning-cli","save-config","overwrite","file-exists","idempotent-rerun"],"backgroundTag":"refusing-to-overwrite-existing-file","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}