fix(install): clean up the temp dir when a package-manager install fails#1949
Open
shulaoda wants to merge 1 commit into
Open
Conversation
✅ Deploy Preview for viteplus-preview canceled.
|
fengmk2
reviewed
Jun 26, 2026
| let platform_tgz_url = get_npm_package_tgz_url(platform_package_name, version); | ||
| let target_dir_tmp = tempfile::tempdir_in(parent_dir)?.path().to_path_buf(); | ||
| // Keep the TempDir guard alive so a failure path cleans up the temp dir. | ||
| let tmp_dir = tempfile::tempdir_in(parent_dir)?; |
Member
There was a problem hiding this comment.
Are there any clippy rules that can be configured to ensure that subsequent code doesn't repeat the same mistakes?
fengmk2
approved these changes
Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Both
download_package_manager(pnpm/npm/yarn) anddownload_bun_package_managercreated their temporary install directory like this:tempfile::tempdir_inreturns aTempDirguard whoseDropdeletes the directory. Here the guard is an unnamed temporary that drops at the end of thelet, so the directory is deleted immediately and only thePathBufsurvives.It still worked on the happy path because
download_and_extract_tgz_with_hashrecreates the directory (create_dir_all) before writing. But on any failure after that — a download/extract error, or thepackage→ bin rename — the function returns early via?, leaving the recreated temp dir behind. With the guard already gone, nothing cleans it up, so a.tmpXXXXdirectory leaks into$VP_HOME/package_manager/<name>/. Every failed install (flaky network, corrupt download, …) leaks one, and they accumulate forever.Fix
Bind the
TempDirto a namedtmp_dirthat lives for the whole function:Now any early return drops the guard and removes the temp dir. On success the dir is renamed into its final location first, so the end-of-scope drop hits a
NotFoundon the original path, whichtempfileignores — success is unaffected. Applied to both sites.Test
test_download_package_manager_cleans_temp_dir_on_failuredrivesdownload_package_manager(Pnpm, "10.0.0", None)against a mock registry serving a non-gzip tarball (the download succeeds, extraction fails), then asserts no leftover directory remains underpackage_manager/pnpm/. It fails on the old code (a leaked.tmp*dir) and passes on the fix. The fullpackage_managermodule stays green (91 passed).Reproduce
To watch the bug itself: temporarily revert the two sites to
let target_dir_tmp = tempfile::tempdir_in(parent_dir)?.path().to_path_buf();and rerun — the test fails with a leaked.tmpXXXXdirectory underpackage_manager/pnpm/.🤖 Generated with Claude Code