Skip to content

Commit 934bcb2

Browse files
committed
Add support for 'required-features'.
Discover 'required-features' in Cargo manifest and initialize value of 'rust-cargo-features'.
1 parent 1ffbbfe commit 934bcb2

File tree

5 files changed

+99
-25
lines changed

5 files changed

+99
-25
lines changed

flycheck-rust.el

+14-8
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ FILE-NAME is the path of the file that is matched against the
119119
`src_path' value in the list of `targets' returned by `cargo
120120
read-manifest'.
121121
122-
Return a cons cell (KIND . NAME) where KIND is the target
123-
kind (lib, bin, test, example or bench), and NAME the target
124-
name (usually, the crate name). If FILE-NAME exactly matches a
125-
target `src-path', this target is returned. Otherwise, return
122+
Return an alist ((KIND . k) (NAME . n) (REQUIRED-FEATURES . rf))
123+
where KIND is the target kind (lib, bin, test, example or bench),
124+
NAME the target name (usually, the crate name), and REQUIRED-FEATURES is the
125+
optional list of features required to build the selected target. If FILE-NAME
126+
exactly matches a target `src-path', this target is returned. Otherwise, return
126127
the closest matching target, or nil if no targets could be found.
127128
128129
See http://doc.crates.io/manifest.html#the-project-layout for a
@@ -160,7 +161,10 @@ description of the conventional Cargo project layout."
160161
(--find (not (equal target it))))))
161162
(when target
162163
(let-alist target
163-
(cons (flycheck-rust-normalize-target-kind .kind) .name))))))
164+
(seq-filter (lambda (kv) (cdr kv))
165+
(list (cons 'kind (flycheck-rust-normalize-target-kind .kind))
166+
(cons 'name .name)
167+
(cons 'required-features .required-features))))))))
164168

165169

166170
(defun flycheck-rust-normalize-target-kind (kinds)
@@ -194,9 +198,11 @@ Flycheck according to the Cargo project layout."
194198
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
195199
(with-demoted-errors "Error in flycheck-rust-setup: %S"
196200
(-when-let* ((file-name (buffer-file-name))
197-
((kind . name) (flycheck-rust-find-cargo-target file-name)))
198-
(setq-local flycheck-rust-crate-type kind)
199-
(setq-local flycheck-rust-binary-name name))))
201+
(target (flycheck-rust-find-cargo-target file-name)))
202+
(let-alist target
203+
(setq-local flycheck-rust-features .required-features)
204+
(setq-local flycheck-rust-crate-type .kind)
205+
(setq-local flycheck-rust-binary-name .name)))))
200206

201207
(provide 'flycheck-rust)
202208

tests/crate-with-features/Cargo.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "test-crate-with-features"
3+
version = "0.1.0"
4+
5+
[features]
6+
fea1 = []
7+
fea2 = []
8+
9+
[[bin]]
10+
name = "main"
11+
path = "src/main.rs"
12+
required-features = ["fea1"]
13+
14+
[[example]]
15+
name = "example"
16+
path = "example/example.rs"
17+
required-features = ["fea1", "fea2"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/crate-with-features/src/main.rs

Whitespace-only changes.

tests/test-rust-setup.el

+67-17
Original file line numberDiff line numberDiff line change
@@ -38,97 +38,147 @@
3838
(defun crate-file (file-name)
3939
(expand-file-name file-name "tests/test-crate"))
4040

41+
(defun crate-with-features-file (file-name)
42+
(expand-file-name file-name "tests/crate-with-features"))
43+
4144
(defun lib-crate-file (file-name)
4245
(expand-file-name file-name "tests/custom-lib-target"))
4346

4447
(defun build-script-crate-file (file-name)
4548
(expand-file-name file-name "tests/build-script-test"))
4649

50+
(defun cdrassoc (sym alist) (cdr (assoc sym alist)))
51+
52+
(defun get-cargo-version ()
53+
(let ((cargo (funcall flycheck-executable-find "cargo")))
54+
(with-temp-buffer
55+
(call-process cargo nil '(t nil) nil "--version")
56+
(buffer-string)
57+
)))
58+
59+
(defun cargo-version ()
60+
(let* ((version-string-parts (split-string (get-cargo-version)))
61+
(version (cadr version-string-parts))
62+
(version-parts (split-string version "-")))
63+
version-parts))
64+
65+
(defun cargo-older-than-1.29-nightly ()
66+
(let* ((version-parts (cargo-version))
67+
(version (car version-parts))
68+
(channel (cadr version-parts)))
69+
(if (version< version "1.29")
70+
t
71+
(and (version= version "1.29") (string= channel "beta")))))
72+
4773
(describe
4874
"`flycheck-rust-find-cargo-target' associates"
4975

5076
(it "'src/lib.rs' to the library target"
5177
(expect
52-
(car (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
78+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/lib.rs")))
5379
:to-equal "lib"))
5480

5581
(it "'src/a.rs' to the library target"
5682
(expect
57-
(car (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
83+
(cdrassoc 'kind (flycheck-rust-find-cargo-target (crate-file "src/a.rs")))
5884
:to-equal "lib"))
5985

6086
(it "'src/main.rs' to the main binary target"
6187
(expect
6288
(flycheck-rust-find-cargo-target (crate-file "src/main.rs"))
63-
:to-equal '("bin" . "test-crate")))
89+
:to-equal '((kind . "bin") (name . "test-crate"))))
6490

6591
(it "'src/bin/a.rs' to the 'a' binary target"
6692
(expect
6793
(flycheck-rust-find-cargo-target (crate-file "src/bin/a.rs"))
68-
:to-equal '("bin" . "a")))
94+
:to-equal '((kind . "bin") (name . "a"))))
6995

7096
(it "'src/bin/b.rs' to the 'b' binary target"
7197
(expect
7298
(flycheck-rust-find-cargo-target (crate-file "src/bin/b.rs"))
73-
:to-equal '("bin" . "b")))
99+
:to-equal '((kind . "bin") (name . "b"))))
74100

75101
(it "'src/bin/support/mod.rs' to any binary target"
76102
(expect
77103
(flycheck-rust-find-cargo-target (crate-file "src/bin/support/mod.rs"))
78-
:to-equal-one-of '("bin". "a") '("bin". "b")))
104+
:to-equal-one-of
105+
'((kind . "bin") (name . "a"))
106+
'((kind . "bin") (name . "b"))))
79107

80108
(it "'tests/a.rs' to the 'a' test target"
81109
(expect
82110
(flycheck-rust-find-cargo-target (crate-file "tests/a.rs"))
83-
:to-equal '("test" . "a")))
111+
:to-equal '((kind . "test") (name . "a"))))
84112

85113
(it "'tests/support/mod.rs' to any test target"
86114
(expect
87115
(flycheck-rust-find-cargo-target (crate-file "tests/support/mod.rs"))
88-
:to-equal-one-of '("test". "a") '("test". "b")))
116+
:to-equal-one-of
117+
'((kind . "test") (name . "a"))
118+
'((kind . "test") (name . "b"))))
89119

90120
(it "'examples/a.rs' to the 'a' example target"
91121
(expect
92122
(flycheck-rust-find-cargo-target (crate-file "examples/a.rs"))
93-
:to-equal '("example" . "a")))
123+
:to-equal '((kind . "example") (name . "a"))))
94124

95125
(it "'examples/b.rs' to the 'b' example target"
96126
(expect
97127
(flycheck-rust-find-cargo-target (crate-file "examples/b.rs"))
98-
:to-equal '("example" . "b")))
128+
:to-equal '((kind . "example") (name . "b"))))
99129

100130
(it "'examples/support/mod.rs' to any example target"
101131
(expect
102132
(flycheck-rust-find-cargo-target (crate-file "examples/support/mod.rs"))
103-
:to-equal-one-of '("example" . "a") '("example" . "b")))
133+
:to-equal-one-of
134+
'((kind . "example") (name . "a"))
135+
'((kind . "example") (name . "b"))))
104136

105137
(it "'benches/a.rs' to the 'a' bench target"
106138
(expect
107139
(flycheck-rust-find-cargo-target (crate-file "benches/a.rs"))
108-
:to-equal '("bench" . "a")))
140+
:to-equal '((kind . "bench") (name . "a"))))
109141

110142
(it "'benches/b.rs' to the 'b' bench target"
111143
(expect
112144
(flycheck-rust-find-cargo-target (crate-file "benches/b.rs"))
113-
:to-equal '("bench" . "b")))
145+
:to-equal '((kind . "bench") (name . "b"))))
114146

115147
(it "'benches/support/mod.rs' to any bench target"
116148
(expect
117149
(flycheck-rust-find-cargo-target (crate-file "benches/support/mod.rs"))
118-
:to-equal-one-of '("bench" . "a") '("bench" . "b")))
150+
:to-equal-one-of
151+
'((kind . "bench") (name . "a"))
152+
'((kind . "bench") (name . "b"))))
119153

120154
(it "'src/lib.rs' to the library target (custom-lib-target)"
121155
(expect
122156
(car (flycheck-rust-find-cargo-target (lib-crate-file "src/lib.rs")))
123-
:to-equal "lib"))
157+
:to-equal '(kind . "lib")))
124158

125159
(it "'build.rs' to any target in the same workspace member (parent)"
126160
(expect
127161
(flycheck-rust-find-cargo-target (build-script-crate-file "build.rs"))
128-
:to-equal (cons "bin" "build-script-test")))
162+
:to-equal '((kind . "bin") (name . "build-script-test"))))
129163

130164
(it "'build.rs' to any target in the same workspace member (child)"
131165
(expect
132166
(flycheck-rust-find-cargo-target (build-script-crate-file "lib-test/build.rs"))
133-
:to-equal (cons "lib" "lib-test")))
167+
:to-equal '((kind . "lib") (name . "lib-test"))))
168+
169+
(it "'src/main.rs' to the bin target with required-features (fea1)"
170+
(if (cargo-older-than-1.29-nightly)
171+
(signal 'buttercup-pending "requires cargo 1.29"))
172+
(expect
173+
(flycheck-rust-find-cargo-target (crate-with-features-file "src/main.rs"))
174+
:to-equal '((kind . "bin") (name . "main") (required-features . ("fea1")))))
175+
176+
(it "'example/example.rs' to the example target with required-features (fea1 fea2)"
177+
(if (cargo-older-than-1.29-nightly)
178+
(signal 'buttercup-pending "requires cargo 1.29"))
179+
(expect
180+
(flycheck-rust-find-cargo-target (crate-with-features-file "example/example.rs"))
181+
:to-equal '((kind . "example")
182+
(name . "example")
183+
(required-features . ("fea1" "fea2")))))
134184
)

0 commit comments

Comments
 (0)