-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathrust.rs
More file actions
191 lines (176 loc) · 7.2 KB
/
rust.rs
File metadata and controls
191 lines (176 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::ffi::OsString;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::{fs, io};
use crate::detect::{has_rust_fmt, has_rust_up, has_wasm32_target};
use anyhow::Context;
use cargo_metadata::Message;
use duct::cmd;
use itertools::Itertools;
fn cargo_cmd(subcommand: &str, build_debug: bool, args: &[&str]) -> duct::Expression {
duct::cmd(
"cargo",
[
subcommand,
"--config=net.git-fetch-with-cli=true",
"--target=wasm32-unknown-unknown",
]
.into_iter()
.chain((!build_debug).then_some("--release"))
.chain(args.iter().copied()),
)
}
pub(crate) fn build_rust(
project_path: &Path,
features: Option<&std::ffi::OsString>,
lint_dir: Option<&Path>,
build_debug: bool,
) -> anyhow::Result<PathBuf> {
// Make sure that we have the wasm target installed
if !has_wasm32_target() {
if has_rust_up() {
cmd!("rustup", "target", "add", "wasm32-unknown-unknown")
.run()
.context("Failed to install wasm32-unknown-unknown target with rustup")?;
} else {
anyhow::bail!("wasm32-unknown-unknown target is not installed. Please install it.");
}
}
if let Some(lint_dir) = lint_dir {
let mut err_count: u32 = 0;
let lint_dir = project_path.join(lint_dir);
for file in walkdir::WalkDir::new(lint_dir).into_iter() {
let file = file?;
let printable_path = file.path().to_str().ok_or(anyhow::anyhow!("path not utf-8"))?;
if file.file_type().is_file() && file.path().extension().is_some_and(|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:\n");
}
eprintln!("{printable_path}:{line_number}: {line}");
err_count += 1;
}
}
}
}
}
if err_count > 0 {
eprintln!();
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."
);
}
} else {
println!(
"Warning: Skipping checks for nonfunctional print statements.\n\
If you have used builtin macros for printing, such as println!,\n\
your logs will not show up."
);
}
let mut args = if let Some(features) = features {
vec![format!("--features={}", features.to_string_lossy())]
} else {
vec![]
};
args.push("--message-format=json-render-diagnostics".to_string());
// Convert Vec<String> to Vec<&str>
let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
let reader = cargo_cmd("build", build_debug, &args_str).dir(project_path).reader()?;
let mut artifact = None;
for message in Message::parse_stream(io::BufReader::new(reader)) {
match message {
Ok(Message::CompilerArtifact(art)) => artifact = Some(art),
Err(error) => return Err(anyhow::anyhow!(error)),
_ => {}
}
}
let artifact = artifact.context("no artifact found?")?;
let artifact = artifact.filenames.into_iter().next().context("no wasm?")?;
check_for_issues(artifact.as_ref())?;
Ok(artifact.into())
}
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 {
return Ok(());
};
let Ok(module) = wasmbin::Module::decode_from(&mut io::BufReader::new(file)) else {
return Ok(());
};
if has_wasm_bindgen(&module) {
anyhow::bail!(
"wasm-bindgen detected.\n\
\n\
It seems like either you or a crate in your dependency tree is depending on\n\
wasm-bindgen. wasm-bindgen is only for webassembly modules that target the web\n\
platform, and will not work in the context of SpacetimeDB.\n\
\n\
To find the offending dependency, run `cargo tree -i wasm-bindgen`. Try checking\n\
its cargo features for 'js' or 'web' or 'wasm-bindgen' to see if there's a way\n\
to disable it."
)
}
if has_getrandom(&module) {
anyhow::bail!(
"getrandom usage detected.\n\
\n\
It seems like either you or a crate in your dependency tree is depending on\n\
the `getrandom` crate for random number generation. getrandom is the default\n\
randomness source for the `rand` crate, and is used when you call\n\
`rand::random()` or `rand::thread_rng()`. If this is you, you should instead\n\
use `ctx.rng()` on a `ReducerContext`. If this is a crate in your\n\
tree, you should try to see if the crate provides a way to pass in a custom\n\
`Rng` type, and pass it the rng returned from `ctx.rng()`."
)
}
Ok(())
}
const WBINDGEN_PREFIX: &str = "__wbindgen";
fn has_wasm_bindgen(module: &wasmbin::Module) -> bool {
let check_import = |import: &wasmbin::sections::Import| {
import.path.module.starts_with(WBINDGEN_PREFIX) || import.path.name.starts_with(WBINDGEN_PREFIX)
};
let check_export = |export: &wasmbin::sections::Export| export.name.starts_with(WBINDGEN_PREFIX);
module
.find_std_section::<wasmbin::sections::payload::Import>()
.and_then(|imports| imports.try_contents().ok())
.is_some_and(|imports| imports.iter().any(check_import))
|| module
.find_std_section::<wasmbin::sections::payload::Export>()
.and_then(|exports| exports.try_contents().ok())
.is_some_and(|exports| exports.iter().any(check_export))
}
fn has_getrandom(module: &wasmbin::Module) -> bool {
module
.find_std_section::<wasmbin::sections::payload::Import>()
.and_then(|imports| imports.try_contents().ok())
.is_some_and(|imports| imports.iter().any(|import| import.path.name == "__getrandom_custom"))
}
pub(crate) fn rustfmt(files: impl IntoIterator<Item = PathBuf>) -> anyhow::Result<()> {
if !has_rust_fmt() {
if has_rust_up() {
cmd!("rustup", "component", "add", "rustfmt")
.run()
.context("Failed to install rustfmt with Rustup")?;
} else {
anyhow::bail!("rustfmt is not installed. Please install it.");
}
}
cmd(
"rustfmt",
itertools::chain(
["--edition", "2021"].into_iter().map_into::<OsString>(),
files.into_iter().map_into(),
),
)
.run()?;
Ok(())
}