checks.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use std::{fs, path::Path};
  2. use anyhow::{anyhow, Result};
  3. use semver::{Version, VersionReq};
  4. use crate::{
  5. config::{Config, Manifest, WithPath},
  6. VERSION,
  7. };
  8. /// Check whether `overflow-checks` codegen option is enabled.
  9. ///
  10. /// https://doc.rust-lang.org/rustc/codegen-options/index.html#overflow-checks
  11. pub fn check_overflow(cargo_toml_path: impl AsRef<Path>) -> Result<bool> {
  12. Manifest::from_path(cargo_toml_path)?
  13. .profile
  14. .release
  15. .as_ref()
  16. .and_then(|profile| profile.overflow_checks)
  17. .ok_or(anyhow!(
  18. "`overflow-checks` is not enabled. To enable, add:\n\n\
  19. [profile.release]\n\
  20. overflow-checks = true\n\n\
  21. in workspace root Cargo.toml",
  22. ))
  23. }
  24. /// Check whether there is a mismatch between the current CLI version and:
  25. ///
  26. /// - `anchor-lang` crate version
  27. /// - `@coral-xyz/anchor` package version
  28. ///
  29. /// This function logs warnings in the case of a mismatch.
  30. pub fn check_anchor_version(cfg: &WithPath<Config>) -> Result<()> {
  31. let cli_version = Version::parse(VERSION)?;
  32. // Check lang crate
  33. let mismatched_lang_version = cfg
  34. .get_rust_program_list()?
  35. .into_iter()
  36. .map(|path| path.join("Cargo.toml"))
  37. .map(cargo_toml::Manifest::from_path)
  38. .filter_map(|man| man.ok())
  39. .filter_map(|man| man.dependencies.get("anchor-lang").map(|d| d.to_owned()))
  40. .filter_map(|dep| Version::parse(dep.req()).ok())
  41. .find(|ver| ver != &cli_version); // Only log the warning once
  42. if let Some(ver) = mismatched_lang_version {
  43. eprintln!(
  44. "WARNING: `anchor-lang` version({ver}) and the current CLI version({cli_version}) \
  45. don't match.\n\n\t\
  46. This can lead to unwanted behavior. To use the same CLI version, add:\n\n\t\
  47. [toolchain]\n\t\
  48. anchor_version = \"{ver}\"\n\n\t\
  49. to Anchor.toml\n"
  50. );
  51. }
  52. // Check TS package
  53. let package_json = {
  54. let package_json_path = cfg.path().parent().unwrap().join("package.json");
  55. let package_json_content = fs::read_to_string(package_json_path)?;
  56. serde_json::from_str::<serde_json::Value>(&package_json_content)?
  57. };
  58. let mismatched_ts_version = package_json
  59. .get("dependencies")
  60. .and_then(|deps| deps.get("@coral-xyz/anchor"))
  61. .and_then(|ver| ver.as_str())
  62. .and_then(|ver| VersionReq::parse(ver).ok())
  63. .filter(|ver| !ver.matches(&cli_version));
  64. if let Some(ver) = mismatched_ts_version {
  65. eprintln!(
  66. "WARNING: `@coral-xyz/anchor` version({ver}) and the current CLI version\
  67. ({cli_version}) don't match.\n\n\t\
  68. This can lead to unwanted behavior. To fix, upgrade the package by running:\n\n\t\
  69. yarn upgrade @coral-xyz/anchor@{cli_version}\n"
  70. );
  71. }
  72. Ok(())
  73. }