checks.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  74. /// Check for potential dependency improvements.
  75. ///
  76. /// The main problem people will run into with Solana v2 is that the `solana-program` version
  77. /// specified in users' `Cargo.toml` might be incompatible with `anchor-lang`'s dependency.
  78. /// To fix this and similar problems, users should use the crates exported from `anchor-lang` or
  79. /// `anchor-spl` when possible.
  80. pub fn check_deps(cfg: &WithPath<Config>) -> Result<()> {
  81. // Check `solana-program`
  82. cfg.get_rust_program_list()?
  83. .into_iter()
  84. .map(|path| path.join("Cargo.toml"))
  85. .map(cargo_toml::Manifest::from_path)
  86. .map(|man| man.map_err(|e| anyhow!("Failed to read manifest: {e}")))
  87. .collect::<Result<Vec<_>>>()?
  88. .into_iter()
  89. .filter(|man| man.dependencies.contains_key("solana-program"))
  90. .for_each(|man| {
  91. eprintln!(
  92. "WARNING: Adding `solana-program` as a separate dependency might cause conflicts.\n\
  93. To solve, remove the `solana-program` dependency and use the exported crate from \
  94. `anchor-lang`.\n\
  95. `use solana_program` becomes `use anchor_lang::solana_program`.\n\
  96. Program name: `{}`\n",
  97. man.package().name()
  98. )
  99. });
  100. Ok(())
  101. }
  102. /// Check whether the `idl-build` feature is being used correctly.
  103. ///
  104. /// **Note:** The check expects the current directory to be a program directory.
  105. pub fn check_idl_build_feature() -> Result<()> {
  106. let manifest_path = Path::new("Cargo.toml").canonicalize()?;
  107. let manifest = Manifest::from_path(&manifest_path)?;
  108. // Check whether the manifest has `idl-build` feature
  109. let has_idl_build_feature = manifest
  110. .features
  111. .iter()
  112. .any(|(feature, _)| feature == "idl-build");
  113. if !has_idl_build_feature {
  114. let anchor_spl_idl_build = manifest
  115. .dependencies
  116. .iter()
  117. .any(|dep| dep.0 == "anchor-spl")
  118. .then_some(r#", "anchor-spl/idl-build""#)
  119. .unwrap_or_default();
  120. return Err(anyhow!(
  121. r#"`idl-build` feature is missing. To solve, add
  122. [features]
  123. idl-build = ["anchor-lang/idl-build"{anchor_spl_idl_build}]
  124. in `{manifest_path:?}`."#
  125. ));
  126. }
  127. // Check if `idl-build` is enabled by default
  128. manifest
  129. .dependencies
  130. .iter()
  131. .filter(|(_, dep)| dep.req_features().contains(&"idl-build".into()))
  132. .for_each(|(name, _)| {
  133. eprintln!(
  134. "WARNING: `idl-build` feature of crate `{name}` is enabled by default. \
  135. This is not the intended usage.\n\n\t\
  136. To solve, do not enable the `idl-build` feature and include crates that have \
  137. `idl-build` feature in the `idl-build` feature list:\n\n\t\
  138. [features]\n\t\
  139. idl-build = [\"{name}/idl-build\", ...]\n"
  140. )
  141. });
  142. // Check `anchor-spl`'s `idl-build` feature
  143. manifest
  144. .dependencies
  145. .get("anchor-spl")
  146. .and_then(|_| manifest.features.get("idl-build"))
  147. .map(|feature_list| !feature_list.contains(&"anchor-spl/idl-build".into()))
  148. .unwrap_or_default()
  149. .then(|| {
  150. eprintln!(
  151. "WARNING: `idl-build` feature of `anchor-spl` is not enabled. \
  152. This is likely to result in cryptic compile errors.\n\n\t\
  153. To solve, add `anchor-spl/idl-build` to the `idl-build` feature list:\n\n\t\
  154. [features]\n\t\
  155. idl-build = [\"anchor-spl/idl-build\", ...]\n"
  156. )
  157. });
  158. Ok(())
  159. }