Repository: typst/typst
File: crates/typst-kit/src/package.rs (Rust)

Code before:

        let url = format!(
            "{DEFAULT_REGISTRY}/{DEFAULT_NAMESPACE}/{}-{}.tar.gz",
            spec.name, spec.version
        );

        let data = match self.downloader.download_with_progress(&url, progress) {
            Ok(data) => data,
            Err(ureq::Error::Status(404, _)) => {
                if let Ok(version) = self.determine_latest_version(&spec.versionless()) {
                    return Err(PackageError::VersionNotFound(spec.clone(), version));
                } else {
                    return Err(PackageError::NotFound(spec.clone()));
                }
            }
            Err(err) => {
                return Err(PackageError::NetworkFailed(Some(eco_format!("{err}"))))
            }
        };

        // Temporary place where the package will be extracted before being
        // moved to the target directory.
        //
        // To avoid name clashing we use PRNG to get unique directory name.
        let extracted_package_dir = package_base_dir.join(format!(
            ".cache-{}-{}-{}",
            spec.name,
            spec.version,
            rand::random::<u32>()
        ));

        let create_dir = |dir, dir_name| {
            fs::create_dir_all(dir).map_err(|err| {
                PackageError::Other(Some(eco_format!(
                    "failed to create a {dir_name} directory ({err})"
                )))
            })
        };

        create_dir(extracted_package_dir.as_path(), "temporary package")?;
        create_dir(package_dir, "package")?;

        let removed_download_dir = || {
            fs::remove_dir_all(&extracted_package_dir).map_err(|err| {
                PackageError::Other(Some(eco_format!(
                    "failed to remove a temporary package directory: {err}"
                )))
            })
        };

        let decompressed = flate2::read::GzDecoder::new(data.as_slice());

Review comment (bug):
`.cache` is redundant as we're already in the cache dir (it's also technically wrong since it's not a cache but a temporary folder).

I suggest either `.partial` or `.download`.

```suggestion
            ".partial-{}-{}-{}",
```

Code after:
        let url = format!(
            "{DEFAULT_REGISTRY}/{DEFAULT_NAMESPACE}/{}-{}.tar.gz",
            spec.name, spec.version
        );

        let data = match self.downloader.download_with_progress(&url, progress) {
            Ok(data) => data,
            Err(ureq::Error::Status(404, _)) => {
                if let Ok(version) = self.determine_latest_version(&spec.versionless()) {
                    return Err(PackageError::VersionNotFound(spec.clone(), version));
                } else {
                    return Err(PackageError::NotFound(spec.clone()));
                }
            }
            Err(err) => {
                return Err(PackageError::NetworkFailed(Some(eco_format!("{err}"))))
            }
        };

        // The directory in which the package's version lives.
        let base_dir = cache_dir.join(format!("{}/{}", spec.namespace, spec.name));

        // The place at which the specific package version will live in the end.
        let package_dir = base_dir.join(format!("{}", spec.version));

        // To prevent multiple Typst instances from interferring, we download
        // into a temporary directory first and then move this directory to
        // its final destination.
        //
        // In the `rename` function's documentation it is stated:
        // > This will not work if the new name is on a different mount point.
        //
        // By locating the temporary directory directly next to where the
        // package directory will live, we are (trying our best) making sure
        // that `tempdir` and `package_dir` are on the same mount point.
        let tempdir = Tempdir::create(base_dir.join(format!(
            ".tmp-{}-{}",
            spec.version,
            fastrand::u32(..),
        )))
        .map_err(|err| error("failed to create temporary package directory", err))?;

        // Decompress the archive into the temporary directory.
        let decompressed = flate2::read::GzDecoder::new(data.as_slice());
        tar::Archive::new(decompressed)
            .unpack(&tempdir)
            .map_err(|err| PackageError::MalformedArchive(Some(eco_format!("{err}"))))?;

        // When trying to move (i.e., `rename`) the directory from one place to
        // another and the target/destination directory is empty, then the
        // operation will succeed (if it's atomic, or hardware doesn't fail, or