lint-cargo.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. """Check our set of Cargo.toml files for issues"""
  10. import sys
  11. from pprint import pprint
  12. from materialize import MZ_ROOT
  13. from materialize.cargo import Workspace
  14. def check_rust_versions(workspace: Workspace) -> bool:
  15. """Checks that every crate has a minimum specified rust version, and furthermore,
  16. that they are all the same."""
  17. rust_version_to_crate_name: dict[str | None, list[str]] = {}
  18. for name, crate in workspace.crates.items():
  19. rust_version_to_crate_name.setdefault(crate.rust_version, []).append(name)
  20. success = (
  21. len(rust_version_to_crate_name) == 1 and None not in rust_version_to_crate_name
  22. )
  23. if not success:
  24. print(
  25. "Not all crates have the same rust-version value. Rust versions found:",
  26. file=sys.stderr,
  27. )
  28. pprint(rust_version_to_crate_name, stream=sys.stderr)
  29. return success
  30. def check_default_members(workspace: Workspace) -> bool:
  31. """Checks that the default members for the workspace includes all crates
  32. except those that are intentionally excluded."""
  33. EXCLUDED = {
  34. # This crate force enables jemalloc on Linux. We want it to be excluded
  35. # by default, and only pulled in by feature flags for other crates. This
  36. # ensures that `--no-default-features` properly disables jemalloc.
  37. "src/alloc-default",
  38. # This crate depends on the `fivetran-sdk` submodule. We don't want
  39. # users building the main binaries to need to set up this submodule.
  40. "src/fivetran-destination",
  41. }
  42. crates = set(str(c.path.relative_to(c.root)) for c in workspace.crates.values())
  43. default_members = set(workspace.default_members)
  44. missing = crates - EXCLUDED - default_members
  45. success = len(missing) == 0
  46. if not success:
  47. print(
  48. "Crates missing from workspace.default-members in root Cargo.toml:",
  49. file=sys.stderr,
  50. )
  51. for crate in sorted(missing):
  52. print(f' "{crate}",', file=sys.stderr)
  53. return success
  54. def main() -> None:
  55. workspace = Workspace(MZ_ROOT)
  56. lints = [check_rust_versions, check_default_members]
  57. success = True
  58. for lint in lints:
  59. success = success and lint(workspace)
  60. if not success:
  61. sys.exit(1)
  62. if __name__ == "__main__":
  63. main()