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

CLI - Replace clippy with a manual check #1928

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ thiserror.workspace = true
tokio.workspace = true
tokio-tungstenite.workspace = true
toml.workspace = true
walkdir.workspace = true
wasmbin.workspace = true
wasmtime.workspace = true
webbrowser.workspace = true
Expand Down
54 changes: 29 additions & 25 deletions crates/cli/src/tasks/rust.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::{fs, io};

Expand Down Expand Up @@ -28,22 +29,35 @@ pub(crate) fn build_rust(project_path: &Path, skip_clippy: bool, build_debug: bo
);
}

// Note: Clippy has to run first so that it can build & cache deps for actual build while checking in parallel.
if !skip_clippy {
let clippy_conf_dir = tempfile::tempdir()?;
fs::write(clippy_conf_dir.path().join("clippy.toml"), CLIPPY_TOML)?;
eprintln!("checking crate with spacetimedb's clippy configuration");
let out = cargo_cmd(
"clippy",
build_debug,
&["--", "--no-deps", "-Aclippy::all", "-Dclippy::disallowed-macros"],
)
.dir(project_path)
.env("CLIPPY_DISABLE_DOCS_LINKS", "1")
.env("CLIPPY_CONF_DIR", clippy_conf_dir.path())
.unchecked()
.run()?;
anyhow::ensure!(out.status.success(), "clippy found a lint error");
let mut err_count: u32 = 0;
for file in walkdir::WalkDir::new(project_path).into_iter().filter_map(Result::ok) {
bfops marked this conversation as resolved.
Show resolved Hide resolved
let printable_path = file.path().to_str().ok_or(anyhow::anyhow!("path not utf-8"))?;
if file.file_type().is_file() && file.path().extension().map_or(false, |ext| ext == "rs") {
let file = fs::File::open(&file.path())?;
for (idx, line) in io::BufReader::new(file).lines().enumerate() {
let line = line?;
let line_number = idx + 1;
for disallowed in &["println!", "print!", "eprintln!", "eprint!", "dbg!"] {
if line.contains(disallowed) {
if err_count == 0 {
eprintln!("\nDetected nonfunctional print statements:");
}
eprintln!("\n{printable_path}:{line_number}: {line}\n");
err_count += 1;
}
}
}
}
}
if err_count > 0 {
anyhow::bail!(
"Found {err_count} disallowed print statement(s).\n\
These will not be printed from SpacetimeDB modules.\n\
If you need to print something, use the `log` crate\n\
and the `log::info!` macro instead."
);
}
}

let reader = cargo_cmd("build", build_debug, &["--message-format=json-render-diagnostics"])
Expand All @@ -66,16 +80,6 @@ pub(crate) fn build_rust(project_path: &Path, skip_clippy: bool, build_debug: bo
Ok(artifact.into())
}

const CLIPPY_TOML: &str = r#"
disallowed-macros = [
{ path = "std::print", reason = "print!() has no effect inside a spacetimedb module; use log::info!() instead" },
{ path = "std::println", reason = "println!() has no effect inside a spacetimedb module; use log::info!() instead" },
{ path = "std::eprint", reason = "eprint!() has no effect inside a spacetimedb module; use log::warn!() instead" },
{ path = "std::eprintln", reason = "eprintln!() has no effect inside a spacetimedb module; use log::warn!() instead" },
{ path = "std::dbg", reason = "std::dbg!() has no effect inside a spacetimedb module; import spacetime's dbg!() macro instead" },
]
"#;

fn check_for_issues(artifact: &Path) -> anyhow::Result<()> {
// if this fails for some reason, just let it fail elsewhere
let Ok(file) = fs::File::open(artifact) else {
Expand Down
Loading