summaryrefslogtreecommitdiffstats
path: root/tools/lint/clippy/__init__.py
blob: b972954ff4b81631d250ab2e1e4400453bccb92d (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import json
import os
import re
import signal

import six
from mozboot.util import get_tools_dir
from mozfile import which
from mozlint import result
from mozlint.pathutils import get_ancestors_by_name
from mozprocess import ProcessHandler
from packaging.version import Version

CLIPPY_WRONG_VERSION = """
Clippy is not installed or an older version was detected. Please make sure
clippy is installed and up to date. The minimum required version is {version}.

To install:

    $ rustup component add clippy

To update:

    $ rustup update

Also ensure 'cargo' is on your $PATH.
""".strip()


CARGO_NOT_FOUND = """
Could not find cargo! Install cargo.

And make sure that it is in the PATH
""".strip()


def parse_issues(log, config, issues, base_path, onlyIn):
    results = []
    if onlyIn:
        onlyIn = os.path.normcase(os.path.normpath(onlyIn))
    for issue in issues:

        try:
            detail = json.loads(six.ensure_text(issue))
            if "message" in detail:
                p = detail["target"]["src_path"]
                detail = detail["message"]
                if "level" in detail:
                    if (
                        detail["level"] == "error" or detail["level"] == "failure-note"
                    ) and not detail["code"]:
                        log.debug(
                            "Error outside of clippy."
                            "This means that the build failed. Therefore, skipping this"
                        )
                        log.debug("File = {} / Detail = {}".format(p, detail))
                        continue
                    # We are in a clippy warning
                    if len(detail["spans"]) == 0:
                        # For some reason, at the end of the summary, we can
                        # get the following line
                        # {'rendered': 'warning: 5 warnings emitted\n\n', 'children':
                        # [], 'code': None, 'level': 'warning', 'message':
                        # '5 warnings emitted', 'spans': []}
                        # if this is the case, skip it
                        log.debug(
                            "Skipping the summary line {} for file {}".format(detail, p)
                        )
                        continue

                    l = detail["spans"][0]
                    p = os.path.join(base_path, l["file_name"])
                    if onlyIn and onlyIn not in os.path.normcase(os.path.normpath(p)):
                        # Case when we have a .rs in the include list in the yaml file
                        log.debug(
                            "{} is not part of the list of files '{}'".format(p, onlyIn)
                        )
                        continue
                    res = {
                        "path": p,
                        "level": detail["level"],
                        "lineno": l["line_start"],
                        "column": l["column_start"],
                        "message": detail["message"],
                        "hint": detail["rendered"],
                        "rule": detail["code"]["code"],
                        "lineoffset": l["line_end"] - l["line_start"],
                    }
                    results.append(result.from_config(config, **res))

        except json.decoder.JSONDecodeError:
            log.debug("Could not parse the output:")
            log.debug("clippy output: {}".format(issue))
            continue

    return results


def get_cargo_binary(log):
    """
    Returns the path of the first rustfmt binary available
    if not found returns None
    """
    cargo_home = os.environ.get("CARGO_HOME")
    if cargo_home:
        log.debug("Found CARGO_HOME in {}".format(cargo_home))
        cargo_bin = os.path.join(cargo_home, "bin", "cargo")
        if os.path.exists(cargo_bin):
            return cargo_bin
        log.debug("Did not find {} in CARGO_HOME".format(cargo_bin))
        return None

    rust_path = os.path.join(get_tools_dir(), "rustc", "bin")
    return which("cargo", path=os.pathsep.join([rust_path, os.environ["PATH"]]))


def get_clippy_version(log, cargo):
    """
    Check if we are running the deprecated clippy
    """
    output = run_cargo_command(
        log, [cargo, "clippy", "--version"], universal_newlines=True
    )

    version = re.findall(r"(\d+-\d+-\d+)", output[0])
    if not version:
        return False
    version = Version(version[0].replace("-", "."))
    log.debug("Found version: {}".format(version))
    return version


class clippyProcess(ProcessHandler):
    def __init__(self, *args, **kwargs):
        kwargs["stream"] = False
        ProcessHandler.__init__(self, *args, **kwargs)

    def run(self, *args, **kwargs):
        orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
        ProcessHandler.run(self, *args, **kwargs)
        signal.signal(signal.SIGINT, orig)


def run_cargo_command(log, cmd, **kwargs):
    log.debug("Command: {}".format(cmd))
    env = os.environ.copy()
    # Cargo doesn't find cargo-clippy on its own if it's not in `$PATH` when
    # `$CARGO_HOME` is not set and cargo is not in `~/.cargo`.
    env["PATH"] = os.pathsep.join([os.path.dirname(cmd[0]), os.environ["PATH"]])
    proc = clippyProcess(cmd, env=env, **kwargs)
    proc.run()
    try:
        proc.wait()
    except KeyboardInterrupt:
        proc.kill()

    return proc.output


def lint(paths, config, fix=None, **lintargs):
    log = lintargs["log"]
    cargo = get_cargo_binary(log)

    if not cargo:
        print(CARGO_NOT_FOUND)
        if "MOZ_AUTOMATION" in os.environ:
            return 1
        return []

    min_version_str = config.get("min_clippy_version")
    min_version = Version(min_version_str)
    actual_version = get_clippy_version(log, cargo)
    log.debug(
        "Found version: {}. Minimum expected version: {}".format(
            actual_version, min_version
        )
    )

    if not actual_version or actual_version < min_version:
        print(CLIPPY_WRONG_VERSION.format(version=min_version_str))
        return 1

    cmd_args_clean = [cargo]
    cmd_args_clean.append("clean")

    cmd_args_common = ["--manifest-path"]
    cmd_args_clippy = [cargo]

    cmd_args_clippy += [
        "clippy",
        "--message-format=json",
    ]

    if fix:
        cmd_args_clippy += ["--fix"]

    lock_files_to_delete = []
    for p in paths:
        lock_file = os.path.join(p, "Cargo.lock")
        if not os.path.exists(lock_file):
            lock_files_to_delete.append(lock_file)

    results = []
    for p in paths:
        # Quick sanity check of the paths
        if p.endswith("Cargo.toml"):
            print("Error: expects a directory or a rs file")
            print("Found {}".format(p))
            return 1

    for p in paths:
        onlyIn = []
        path_conf = p
        log.debug("Path = {}".format(p))
        if os.path.isfile(p):
            # We are dealing with a file. We remove the filename from the path
            # to find the closest Cargo file
            # We also store the name of the file to be able to filter out other
            # files built by the cargo
            p = os.path.dirname(p)
            onlyIn = path_conf

        elif os.path.isdir(p):
            # Sometimes, clippy reports issues from other crates
            # Make sure that we don't display that either
            onlyIn = p

        cargo_files = get_ancestors_by_name("Cargo.toml", p, lintargs["root"])
        p = cargo_files[0]

        log.debug("Path translated to = {}".format(p))
        # Needs clean because of https://github.com/rust-lang/rust-clippy/issues/2604
        clean_command = cmd_args_clean + cmd_args_common + [p]
        run_cargo_command(log, clean_command)

        # Create the actual clippy command
        base_command = cmd_args_clippy + cmd_args_common + [p]
        output = run_cargo_command(log, base_command)

        # Remove build artifacts created by clippy
        run_cargo_command(log, clean_command)

        # Source paths in clippy spans, when they are relative, are relative to the
        # workspace if there is one.
        for cargo_toml in cargo_files:
            with open(cargo_toml) as fh:
                if "[workspace]" in fh.read():
                    p = cargo_toml
                    break
        results += parse_issues(log, config, output, os.path.dirname(p), onlyIn)

    # Remove Cargo.lock files created by clippy
    for lock_file in lock_files_to_delete:
        if os.path.exists(lock_file):
            os.remove(lock_file)

    return sorted(results, key=lambda issue: issue.path)