Repository: denoland/deno
Pull request: fix(upgrade): support RC release with --version flag

This commit fixes scenarios like:
```
$ deno upgrade --version=v1.46.0-rc.0
Downloading https://github.com/denoland/deno/releases/download/v1.46.0-rc.0/deno-aarch64-apple-darwin.zip
Download could not be found, aborting
```

Now:
```
$ target/debug/deno upgrade --version=v1.46.0-rc.0
Downloading https://dl.deno.land/release/v1.46.0-rc.0/deno-aarch64-apple-darwin.zip
Deno is upgrading to version 1.46.0-rc.0

Upgraded successfully to Deno v1.46.0-rc.0

```

Also some unneeded code was removed.

Changed files:
filename: cli/tools/upgrade.rs
patch: @@ -380,13 +380,13 @@ async fn check_for_upgrades_for_lsp_with_provider(
         }
       }
       Ok(Some(LspVersionUpgradeInfo {
-        latest_version: latest_version.display,
+        latest_version: latest_version.version_or_hash,
         is_canary: false,
       }))
     }
 
     ReleaseChannel::Canary => Ok(Some(LspVersionUpgradeInfo {
-      latest_version: latest_version.display,
+      latest_version: latest_version.version_or_hash,
       is_canary: true,
     })),
 
@@ -490,7 +490,7 @@ pub async fn upgrade(
     "{}",
     colors::gray(format!(
       "Deno is upgrading to version {}",
-      &selected_version_to_upgrade.display
+      &selected_version_to_upgrade.version_or_hash
     ))
   );
 
@@ -529,13 +529,17 @@ pub async fn upgrade(
   check_windows_access_denied_error(output_result, output_exe_path)?;
 
   log::info!(
-    "\nUpgraded successfully to Deno {}\n",
-    colors::green(selected_version_to_upgrade.display())
+    "\nUpgraded successfully to Deno {} {}\n",
+    colors::green(selected_version_to_upgrade.display()),
+    colors::gray(&format!(
+      "({})",
+      selected_version_to_upgrade.release_channel.name()
+    ))
   );
   if requested_version.release_channel() == ReleaseChannel::Stable {
     print_release_notes(
       version::DENO_VERSION_INFO.deno,
-      &selected_version_to_upgrade.display,
+      &selected_version_to_upgrade.version_or_hash,
     );
   }
 
@@ -577,10 +581,15 @@ impl RequestedVersion {
       }
       (ReleaseChannel::Canary, passed_version)
     } else {
-      if Version::parse_standard(&passed_version).is_err() {
+      let Ok(semver) = Version::parse_standard(&passed_version) else {
         bail!("Invalid version passed");
       };
-      (ReleaseChannel::Stable, passed_version)
+
+      if semver.pre.contains(&"rc".to_string()) {
+        (ReleaseChannel::Rc, passed_version)
+      } else {
+        (ReleaseChannel::Stable, passed_version)
+      }
     };
 
     Ok(RequestedVersion::SpecificVersion(channel, passed_version))
@@ -619,9 +628,8 @@ fn select_specific_version_for_upgrade(
       }
 
       Ok(Some(AvailableVersion {
-        version_or_hash: version.to_string(),
+        version_or_hash: version,
         release_channel,
-        display: version,
       }))
     }
     ReleaseChannel::Canary => {
@@ -635,13 +643,28 @@ fn select_specific_version_for_upgrade(
       }
 
       Ok(Some(AvailableVersion {
-        version_or_hash: version.to_string(),
+        version_or_hash: version,
+        release_channel,
+      }))
+    }
+    ReleaseChannel::Rc => {
+      let current_is_passed = version::DENO_VERSION_INFO.release_channel
+        == ReleaseChannel::Rc
+        && version::DENO_VERSION_INFO.deno == version;
+
+      if !force && current_is_passed {
+        log::info!(
+          "Version {} is already installed",
+          version::DENO_VERSION_INFO.deno
+        );
+        return Ok(None);
+      }
+
+      Ok(Some(AvailableVersion {
+        version_or_hash: version,
         release_channel,
-        display: version,
       }))
     }
-    // TODO(bartlomieju)
-    ReleaseChannel::Rc => unreachable!(),
     // TODO(bartlomieju)
     ReleaseChannel::Lts => unreachable!(),
   }
@@ -741,17 +764,15 @@ async fn find_latest_version_to_upgrade(
 struct AvailableVersion {
   version_or_hash: String,
   release_channel: ReleaseChannel,
-  // TODO(bartlomieju): remove this one
-  display: String,
 }
 
 impl AvailableVersion {
   /// Format display version, appending `v` before version number
   /// for non-canary releases.
   fn display(&self) -> Cow<str> {
     match self.release_channel {
-      ReleaseChannel::Canary => Cow::Borrowed(&self.display),
-      _ => Cow::Owned(format!("v{}", self.display)),
+      ReleaseChannel::Canary => Cow::Borrowed(&self.version_or_hash),
+      _ => Cow::Owned(format!("v{}", self.version_or_hash)),
     }
   }
 }
@@ -778,13 +799,11 @@ fn normalize_version_from_server(
       Ok(AvailableVersion {
         version_or_hash: v.to_string(),
         release_channel,
-        display: v.to_string(),
       })
     }
     ReleaseChannel::Canary => Ok(AvailableVersion {
       version_or_hash: text.to_string(),
       release_channel,
-      display: text.to_string(),
     }),
   }
 }
@@ -1111,7 +1130,6 @@ mod test {
         latest_version: Rc::new(RefCell::new(Ok(AvailableVersion {
           version_or_hash: "".to_string(),
           release_channel: ReleaseChannel::Stable,
-          display: "".to_string(),
         }))),
         time: Rc::new(RefCell::new(chrono::Utc::now())),
       }
@@ -1140,7 +1158,6 @@ mod test {
       *self.latest_version.borrow_mut() = Ok(AvailableVersion {
         version_or_hash: version.to_string(),
         release_channel,
-        display: version.to_string(),
       });
     }
 
@@ -1437,7 +1454,6 @@ mod test {
       AvailableVersion {
         version_or_hash: "1.0.0".to_string(),
         release_channel: ReleaseChannel::Stable,
-        display: "1.0.0".to_string()
       },
     );
     // should not replace v after start
@@ -1450,7 +1466,6 @@ mod test {
       AvailableVersion {
         version_or_hash: "1.0.0-test-v".to_string(),
         release_channel: ReleaseChannel::Stable,
-        display: "1.0.0-test-v".to_string()
       }
     );
     // should not strip v for canary
@@ -1463,7 +1478,6 @@ mod test {
       AvailableVersion {
         version_or_hash: "v1452345asdf".to_string(),
         release_channel: ReleaseChannel::Canary,
-        display: "v1452345asdf".to_string()
       }
     );
     assert_eq!(
@@ -1472,7 +1486,6 @@ mod test {
       AvailableVersion {
         version_or_hash: "1.46.0-rc.0".to_string(),
         release_channel: ReleaseChannel::Rc,
-        display: "1.46.0-rc.0".to_string(),
       },
     );
   }