Skip to content

Commit

Permalink
Add ink! linting MVP stage (#431)
Browse files Browse the repository at this point in the history
* Add ink! linting MVP stage

* CI: cache cargo installations; update versions

* Apply suggestions from code review

Co-authored-by: Sergejs Kostjucenko <85877331+sergejparity@users.noreply.github.com>

* Update changelog

* Check if `dylint` requirements are installed

* Make `clippy` happy

* Make tests happy

* Make `LICENSE` symlink

* Make error handling and panic's consistent

* Improve code structure

* Remove unneeded `--workspace`

* Fix `mapping-two-constructor` test file

* Simplify `Option<Vec<_>>` to `Vec`

* Generalize unsetting of env variables in `invoke_cargo`

* Move special case of `dylint` verbosity to call site

* Keep calm and return an `Err`

* Move checking for `dylint-link` to `build.rs`

* Add `ink_linting/Cargo.lock`

* Move `bool_to_exit_code` inline

* Add test for non-fully-qualified path call of `initialize_contract`

* Fix panic when no manifest path was supplied

* Improve structure of `build.rs`

* Make linting error message non-blaming

* Remove incorrect note on not detecting indirections

* Make `clippy` happy

* Impprove text

* Build `ink_linting` with `--locked`

* Fix path separators for Windows CI

* Revert "Fix path separators for Windows CI"

This reverts commit b599831.

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Revert me: Debug failing Windows CI

* Use `which` for detecting `dylint-link` installation

* Remove `DYLINT_DRIVER_PATH`

* Add `--lib`

* Revert "Remove `DYLINT_DRIVER_PATH`"

This reverts commit d0d209f.

* Use temporary `DYLINT_DRIVER_PATH` path during tests

* Give `template` variable a speaking name

Co-authored-by: CI system <>
Co-authored-by: Sergejs Kostjucenko <85877331+sergejparity@users.noreply.github.com>
  • Loading branch information
cmichi and sergejparity committed Feb 23, 2022
1 parent 30387a5 commit 836acbf
Show file tree
Hide file tree
Showing 35 changed files with 3,137 additions and 118 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
steps:

- name: Checkout sources & submodules
uses: actions/checkout@master
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive
Expand All @@ -36,20 +36,33 @@ jobs:

- name: Install toolchain
id: toolchain
uses: actions-rs/toolchain@master
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
components: rust-src
override: true

- name: Install cargo-dylint
uses: baptiste0928/cargo-install@v1
with:
crate: cargo-dylint
version: 1

- name: Install dylint-link
uses: baptiste0928/cargo-install@v1
with:
crate: dylint-link
version: 1

- name: Rust Cache
uses: Swatinem/rust-cache@v1.2.0
uses: Swatinem/rust-cache@v1.3.0

- name: Build contract template on ${{ matrix.platform }}-${{ matrix.toolchain }}
run: |
wasm-opt --version
cargo -vV
cargo dylint --version
cargo run -- contract --version
cargo run -- contract new foobar
echo "[workspace]" >> foobar/Cargo.toml
Expand Down
19 changes: 16 additions & 3 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,40 @@ jobs:
pathInArchive: "binaryen-/bin/wasm-opt.exe"

- name: Checkout sources & submodules
uses: actions/checkout@master
uses: actions/checkout@v2
with:
fetch-depth: 1
submodules: recursive

- name: Install toolchain
id: toolchain
uses: actions-rs/toolchain@master
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
components: rust-src
override: true

- name: Install cargo-dylint
uses: baptiste0928/cargo-install@v1
with:
crate: cargo-dylint
version: 1

- name: Install dylint-link
uses: baptiste0928/cargo-install@v1
with:
crate: dylint-link
version: 1

- name: Rust Cache
uses: Swatinem/rust-cache@v1.2.0
uses: Swatinem/rust-cache@v1.3.0

- name: Build contract template on ${{ matrix.platform }}-${{ matrix.toolchain }}
run: |
wasm-opt --version
cargo -vV
cargo dylint --version
cargo run -- contract --version
cargo run -- contract new foobar
echo "[workspace]" >> foobar/Cargo.toml
Expand Down
26 changes: 26 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ workflow:
- cargo -vV
- rustc -vV
- rustup show
- cargo dylint --version
- bash --version
- ./scripts/ci/pre_cache.sh
# global RUSTFLAGS overrides the linker args so this way is better to pass the flags
- printf '[build]\nrustflags = ["-C", "link-dead-code"]\n' > ${CARGO_HOME}/config
- sccache -s

# needed until https://github.com/mozilla/sccache/issues/1000 is fixed.
# this is unfortunate, since it disables `sccache` for this entire file.
- unset RUSTC_WRAPPER

- git show
rules:
- if: $CI_PIPELINE_SOURCE == "web"
Expand Down Expand Up @@ -81,6 +87,26 @@ clippy:

#### stage: test (all features)

test-dylint:
stage: test
<<: *docker-env
script:
- cd ink_linting/

# Installing these components here is necessary because
# `ink_linting/` has a fixed `rust-toolchain` file.
# We can't move this line to the Docker container, since
# that would then make it impossible to upgrade the
# `ink_linting/rust-toolchain` file while still having
# this CI job succeed.
- rustup component add rustfmt clippy rust-src

- cargo check --verbose
- cargo fmt --verbose --all -- --check
- cargo clippy --verbose -- -D warnings;

- cargo test --verbose --all-features

test:
stage: test
<<: *docker-env
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Linting rules for smart contracts

We are introducing a linter for ink! smart contracts in this release!
From now on `cargo-contract` checks if the ink! smart contract that is
`build` or `check`-ed follows certain rules.

As a starting point we've only added one linting rule so far; it asserts correct
initialization of the [`ink_storage::Mapping`](https://paritytech.github.io/ink/ink_storage/struct.Mapping.html)
data structure.

In order for the linting to work with your smart contract, the contract has to be
written in at least ink! 3.0.0-rc9. If it's older the linting will just always succeed.

### Added
- Add linting to assert correct initialization of [`ink_storage::Mapping`](https://paritytech.github.io/ink/ink_storage/struct.Mapping.html) - [#431](https://github.com/paritytech/cargo-contract/pull/431)

## [0.17.0] - 2022-01-19

### Changed
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ zip = { version = "0.5.13", default-features = false }
walkdir = "2.3.2"
substrate-build-script-utils = "3.0.0"
platforms = "2.0.0"
which = "4.2.4"

[dev-dependencies]
assert_cmd = "2.0.4"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ More relevant links:
There's only an old version in your distributions package manager? Just use a
[binary release](https://github.com/WebAssembly/binaryen/releases).

* Step 4: `cargo install cargo-dylint dylint-link`.

* Step 3: `cargo install --force cargo-contract`.

You can always update the `cargo-contract` binary to the latest version by running the Step 3.
Expand Down
Loading

0 comments on commit 836acbf

Please sign in to comment.