{"record":{"id":"c1acbcc9f7450401","repo":"pypa/pip","slug":"which-would-be-overwritten","errorCode":null,"errorMessage":" which would be overwritten","messagePattern":" which would be overwritten","errorType":"exception","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/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/distlib/util.py#L516-L552","documentation":"Raised as ValueError by FileOperator.copy_file when check=True and the destination outfile is a symlink or a non-regular existing file. The library refuses to overwrite such targets to avoid clobbering symlinks or special files during installation. The message is the tail fragment ' which would be overwritten' appended to a descriptive prefix built earlier (e.g. '%s is a symlink').","triggerScenarios":"Calling FileOperator().copy_file(src, '/path/that/is/symlink', check=True); installing into a venv whose scripts are symlinks; re-running an install where a previous symlinked file now would be replaced by a regular copy.","commonSituations":"Re-installing into environments that use symlinks (PEP 405 venvs, shared-script layouts); CI caching that leaves symlinks; --target installs over a dir containing symlinks.","solutions":["Pass check=False only if you are certain overwriting the symlink/special file is safe.","Remove or unlink the existing outfile (os.unlink) before copying.","Resolve the symlink target first and copy there, or redirect the install to a clean directory."],"exampleFix":"# before\nfileop.copy_file(src, dst, check=True)\n# after\nif os.path.islink(dst) or (os.path.exists(dst) and not os.path.isfile(dst)):\n    os.unlink(dst)\nfileop.copy_file(src, dst, check=True)","handlingStrategy":"validation","validationCode":"def safe_copy(fileop, src, dst, check=True):\n    if os.path.islink(dst) or (os.path.exists(dst) and not os.path.isfile(dst)):\n        os.unlink(dst)\n    return fileop.copy_file(src, dst, check=check)","typeGuard":"def is_regular_or_missing(path: str) -> bool:\n    return not os.path.exists(path) or (os.path.isfile(path) and not os.path.islink(path))","tryCatchPattern":"try:\n    fileop.copy_file(src, dst, check=True)\nexcept ValueError as e:\n    if 'would be overwritten' in str(e):\n        os.unlink(dst)\n        fileop.copy_file(src, dst, check=False)","preventionTips":["Pre-check destination for symlinks/special files.","Install into a clean directory to avoid symlink conflicts."],"tags":["filesystem","distlib","fileoperator","installation","symlink"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}