Skip to content

Commit

Permalink
fix: Fix broken symlinks not being removed. (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 18, 2023
1 parent e2c82e7 commit 78bb8bb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

## Unreleased

#### 🐞 Fixes

- Fixed an issue where broken symlinks would fail to be removed. This would result in subsequent "File exists (os error 17)" errors.

#### ⚙️ Internal

- Updated Rust to v1.74.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ starbase_archive = { version = "0.2.4", features = [
starbase_events = "0.2.2"
starbase_sandbox = { version = "0.1.12" }
starbase_styles = "0.1.16"
starbase_utils = { version = "0.3.8", default-features = false, features = [
starbase_utils = { version = "0.3.11", default-features = false, features = [
"json",
"toml",
] }
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub async fn purge_tool(id: &Id, yes: bool) -> SystemResult {

// Delete binaries
for bin in tool.get_bin_locations()? {
fs::remove_file(bin.path)?;
fs::remove_link(bin.path)?;
}

// Delete shims
Expand Down
20 changes: 13 additions & 7 deletions crates/cli/tests/clean_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ mod clean {
#[test]
fn purges_tool_bin() {
let sandbox = create_empty_sandbox();
sandbox.create_file("bin/node", "");
sandbox.create_file("bin/node.exe", "");
sandbox.create_file("tools/node/fake/file", "");
sandbox.create_file("bin/other", "");

let bin = sandbox.path().join(if cfg!(windows) {
"bin/node.exe"
} else {
"bin/node"
});

#[allow(deprecated)]
std::fs::soft_link(sandbox.path().join("tools/node/fake/file"), &bin).unwrap();

let mut cmd = create_proto_command(sandbox.path());
cmd.arg("clean")
Expand All @@ -45,11 +54,8 @@ mod clean {
.assert()
.success();

if cfg!(windows) {
assert!(!sandbox.path().join("bin/node.exe").exists());
} else {
assert!(!sandbox.path().join("bin/node").exists());
}
assert!(!bin.exists());
assert!(bin.symlink_metadata().is_err());
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ impl Tool {
"Creating binary symlink"
);

fs::remove_file(&output_path)?;
fs::remove_link(&output_path)?;

#[cfg(windows)]
{
Expand Down Expand Up @@ -1556,9 +1556,9 @@ impl Tool {

// If no more default version, delete the symlink,
// otherwise the OS will throw errors for missing sources
if self.manifest.default_version.is_none() {
if self.manifest.default_version.is_none() || self.manifest.installed_versions.is_empty() {
for bin in self.get_bin_locations()? {
fs::remove_file(bin.path)?;
fs::remove_link(bin.path)?;
}
}

Expand All @@ -1579,7 +1579,7 @@ impl Tool {
"Cleaning up temporary files and downloads"
);

fs::remove(self.get_temp_dir())?;
fs::remove_dir_all(self.get_temp_dir())?;

Ok(())
}
Expand Down

0 comments on commit 78bb8bb

Please sign in to comment.