{"id":"c1acbcc9f7450401","repo":"pypa/pip","slug":"which-would-be-overwritten","errorCode":null,"errorMessage":" which would be overwritten","messagePattern":" which would be overwritten","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/distlib/util.py","lineNumber":534,"sourceCode":"        if not os.path.exists(target):\n            return True\n\n        return os.stat(source).st_mtime > os.stat(target).st_mtime\n\n    def copy_file(self, infile, outfile, check=True):\n        \"\"\"Copy a file respecting dry-run and force flags.\n        \"\"\"\n        self.ensure_dir(os.path.dirname(outfile))\n        logger.info('Copying %s to %s', infile, outfile)\n        if not self.dry_run:\n            msg = None\n            if check:\n                if os.path.islink(outfile):\n                    msg = '%s is a symlink' % outfile\n                elif os.path.exists(outfile) and not os.path.isfile(outfile):\n                    msg = '%s is a non-regular file' % outfile\n            if msg:\n                raise ValueError(msg + ' which would be overwritten')\n            shutil.copyfile(infile, outfile)\n        self.record_as_written(outfile)\n\n    def copy_stream(self, instream, outfile, encoding=None):\n        assert not os.path.isdir(outfile)\n        self.ensure_dir(os.path.dirname(outfile))\n        logger.info('Copying stream %s to %s', instream, outfile)\n        if not self.dry_run:\n            if encoding is None:\n                outstream = open(outfile, 'wb')\n            else:\n                outstream = codecs.open(outfile, 'w', encoding=encoding)\n            try:\n                shutil.copyfileobj(instream, outstream)\n            finally:\n                outstream.close()\n        self.record_as_written(outfile)\n","sourceCodeStart":516,"sourceCodeEnd":552,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/distlib/util.py#L516-L552","documentation":"Raised by FileOperator.copy_file() (when check=True, the default) if the destination outfile is a symlink or a non-regular file (e.g. a directory, device, FIFO). Overwriting such a file could follow an unexpected link or clobber a special file, so a ValueError is raised combining the reason ('X is a symlink' / 'X is a non-regular file') with the suffix ' which would be overwritten'.","triggerScenarios":"operator.copy_file(src, '/path/to/symlink') where the destination is a symlink; copy_file into a path that is currently a directory or FIFO; an existing install left a symlink that a later copy tries to overwrite.","commonSituations":"Re-installing a package whose console_scripts are symlinks, virtualenv layouts using symlinks for site-packages, or stale special files left in an install prefix.","solutions":["Remove or replace the offending destination before copying: if it is a symlink, os.unlink() it first.","Pass check=False to copy_file only if you have explicitly verified the destination is safe to overwrite.","Audit the install destination for symlinks/special files prior to the copy operation."],"exampleFix":"// before\noperator.copy_file(src, dest)  # dest is a symlink\n// after\nimport os\nif os.path.islink(dest):\n    os.unlink(dest)\noperator.copy_file(src, dest)","handlingStrategy":"validation","validationCode":"import os\ndef safe_copy(operator, src, dst, check=True):\n    if check and (os.path.islink(dst) or (os.path.exists(dst) and not os.path.isfile(dst))):\n        os.unlink(dst)  # or raise, per policy\n    return operator.copy_file(src, dst, check=check)","typeGuard":null,"tryCatchPattern":"try:\n    operator.copy_file(src, dst)\nexcept ValueError as e:\n    if 'would be overwritten' in str(e):\n        os.unlink(dst)\n        operator.copy_file(src, dst)\n    else:\n        raise","preventionTips":["Pre-clean install destinations of symlinks/special files before copying.","Keep check=True (default) to catch dangerous overwrites.","Avoid pointing copy_file at directories or device nodes."],"tags":["filesystem","symlink","copy","packaging","safety"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}