Skip to content
Prev Previous commit
Next Next commit
feat(lint): Run lint only for onchain compilation targets
Otherwise contracts could be built for e2e tests with the `std` feature.
  • Loading branch information
jubnzv committed Nov 17, 2023
commit aebee32d9055886fd5a64c1c6ab11f387c4dff0a
13 changes: 13 additions & 0 deletions linting/src/no_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,21 @@ declare_lint! {

declare_lint_pass!(NoMain => [NO_MAIN]);

/// Returns true if the target architecture is suitable to be executed on-chain
fn is_contract_build(cx: &EarlyContext<'_>) -> bool {
matches!(
cx.sess().target.llvm_target.to_string().as_str(),
"wasm32-unknown-unknown" | "riscv32i-unknown-none-elf"
)
}

impl EarlyLintPass for NoMain {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
// Disable when building for e2e tests
if !is_contract_build(cx) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we execute this lint on all the builds, even for e2e tests, making no_main mandatory for every ink! contract?

The point is that cargo-contract always runs linter for the host architecture. It is possible to hack it a bit, but should we? Why should we skip this lint for tests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E2E tests will still run the linter because they compile the contract code blob.

Maybe what we really mean/want is that when building the contract "natively" i.e. not no_std, which happens for unit tests and metadata generation. But those are usually executed on a different path so maybe we don't need to make the distinction here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I see. Let's try to introduce this lint for all contracts. I think it won't make any problems.

return
}

// `no_main` is an `Inner` attribute of `#![cfg_attr(...)]`
if krate.attrs.iter().all(|attr| {
if_chain! {
Expand Down