Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shim): avoid panic if user has malformed lockfile #8461

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions crates/turborepo-lib/src/shim/local_turbo_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use camino::Utf8PathBuf;
use dunce::canonicalize as fs_canonicalize;
use semver::Version;
use semver::{Version, VersionReq};

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Build Turborepo (ubuntu, self-hosted, linux, x64, metal)

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Build Turborepo (macos, macos-12)

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Build Turborepo (windows, windows-latest)

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust clippy

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on ubuntu

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo Rust testing on macos

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

unused import: `VersionReq`

Check warning on line 8 in crates/turborepo-lib/src/shim/local_turbo_state.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

unused import: `VersionReq`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unused import warning

use serde::Deserialize;
use tracing::debug;
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
Expand Down Expand Up @@ -185,37 +185,39 @@
}

pub fn turbo_version_has_shim(version: &str) -> bool {
let version = Version::parse(version).unwrap();
// only need to check major and minor (this will include canaries)
if version.major == 1 {
return version.minor >= 7;
if let Ok(version) = Version::parse(version) {
// only need to check major and minor (this will include canaries)
if version.major == 1 {
return version.minor >= 7;
}
version.major > 1
} else {
// In the case that we don't get passed a valid semver we should avoid a panic.
// We shouldn't hit this we introduce back infering package version from schema
// or package.json.
true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically too loose, but the correct answer for the past 21 months of turbo releases.

}

version.major > 1
}

#[cfg(test)]
mod test {
use test_case::test_case;

use super::*;

#[test]
fn test_skip_infer_version_constraint() {
let canary = "1.7.0-canary.0";
let newer_canary = "1.7.0-canary.1";
let newer_minor_canary = "1.7.1-canary.6";
let release = "1.7.0";
let old = "1.6.3";
let old_canary = "1.6.2-canary.1";
let new = "1.8.0";
let new_major = "2.1.0";

assert!(turbo_version_has_shim(release));
assert!(turbo_version_has_shim(canary));
assert!(turbo_version_has_shim(newer_canary));
assert!(turbo_version_has_shim(newer_minor_canary));
assert!(turbo_version_has_shim(new));
assert!(turbo_version_has_shim(new_major));
assert!(!turbo_version_has_shim(old));
assert!(!turbo_version_has_shim(old_canary));
#[test_case("1.7.0-canary.0", true; "canary")]
#[test_case("1.7.0-canary.1", true; "newer_canary")]
#[test_case("1.7.1-canary.6", true; "newer_minor_canary")]
#[test_case("1.7.0", true; "release")]
#[test_case("1.6.3", false; "old")]
#[test_case("1.6.2-canary.1", false; "old_canary")]
#[test_case("1.8.0", true; "new")]
#[test_case("2.1.0", true; "new major")]
#[test_case("*", true; "star")]
#[test_case("2.0", true; "version 2 0")]
#[test_case("latest", true; "latest")]
#[test_case("canary", true; "canary tag")]
fn test_skip_infer_version_constraint(version: &str, expected: bool) {
assert_eq!(turbo_version_has_shim(version), expected);
}
}
Loading