/
rust.go
467 lines (397 loc) · 13.7 KB
/
rust.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package compute
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/Masterminds/semver/v3"
"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/common"
"github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/text"
)
const (
// RustToolchainVersion is the `rustup` toolchain string for the compiler
// that we support
RustToolchainVersion = "1.46.0"
// WasmWasiTarget is the Rust compilation target for Wasi capable Wasm.
WasmWasiTarget = "wasm32-wasi"
)
// CargoPackage models the package confuiguration properties of a Rust Cargo
// package which we are interested in and is embedded within CargoManifest and
// CargoLock.
type CargoPackage struct {
Name string `toml:"name" json:"name"`
Version string `toml:"version" json:"version"`
Dependencies []CargoPackage `toml:"-" json:"dependencies"`
}
// CargoManifest models the package confuiguration properties of a Rust Cargo
// manifest which we are interested in and are read from the Cargo.toml manifest
// file within the $PWD of the package.
type CargoManifest struct {
Package CargoPackage
}
// Read the contents of the Cargo.toml manifest from filename.
func (m *CargoManifest) Read(filename string) error {
_, err := toml.DecodeFile(filename, m)
return err
}
// CargoMetadata models information about the workspace members and resolved
// dependencies of the current package via `cargo metadata` command output.
type CargoMetadata struct {
Package []CargoPackage `json:"packages"`
}
// Read the contents of the Cargo.lock file from filename.
func (m *CargoMetadata) Read() error {
cmd := exec.Command("cargo", "metadata", "--format-version", "1")
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
if err := json.NewDecoder(stdout).Decode(&m); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
// Rust is an implments Toolchain for the Rust lanaguage.
type Rust struct {
client api.HTTPClient
}
// Verify implments the Toolchain interface and verifies whether the Rust
// language toolchain is correctly configured on the host.
func (r Rust) Verify(out io.Writer) error {
// 1) Check `rustup` is on $PATH
//
// Rustup is Rust's toolchain installer and manager, it is needed to assert
// that the correct WASI WASM compiler target is installed correctly. We
// only check whether the binary exists on the users $PATH and error with
// installation help text.
fmt.Fprintf(out, "Checking if rustup is installed...\n")
p, err := exec.LookPath("rustup")
if err != nil {
return errors.RemediationError{
Inner: fmt.Errorf("`rustup` not found in $PATH"),
Remediation: fmt.Sprintf("To fix this error, run the following command:\n\n\t$ %s", text.Bold("curl https://sh.rustup.rs -sSf | sh")),
}
}
fmt.Fprintf(out, "Found rustup at %s\n", p)
// 2) Check that the `1.43.0` toolchain is installed
//
// We use rustup to assert that the toolchain is installed by streaming the output of
// `rustup toolchain list` and looking for a toolchain whose prefix matches our desired
// version.
fmt.Fprintf(out, "Checking if Rust %s is installed...\n", RustToolchainVersion)
cmd := exec.Command("rustup", "toolchain", "list")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error executing rustup: %w", err)
}
scanner := bufio.NewScanner(strings.NewReader(string(stdoutStderr)))
scanner.Split(bufio.ScanLines)
var found bool
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), RustToolchainVersion) {
found = true
break
}
}
if !found {
return errors.RemediationError{
Inner: fmt.Errorf("rust toolchain %s not found", RustToolchainVersion),
Remediation: fmt.Sprintf("To fix this error, run the following command:\n\n\t$ %s\n", text.Bold("rustup toolchain install "+RustToolchainVersion)),
}
}
// 3) Check `wasm32-wasi` target exists
//
// We use rustup to assert that the target is installed for our toolchain by streaming the
// output of `rustup target list` and looking for the the `wasm32-wasi` value. If not found,
// we error with help text suggesting how to install.
fmt.Fprintf(out, "Checking if %s target is installed...\n", WasmWasiTarget)
cmd = exec.Command("rustup", "target", "list", "--installed", "--toolchain", RustToolchainVersion)
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error executing rustup: %w", err)
}
scanner = bufio.NewScanner(strings.NewReader(string(stdoutStderr)))
scanner.Split(bufio.ScanWords)
found = false
for scanner.Scan() {
if scanner.Text() == WasmWasiTarget {
found = true
break
}
}
if !found {
return errors.RemediationError{
Inner: fmt.Errorf("rust target %s not found", WasmWasiTarget),
Remediation: fmt.Sprintf("To fix this error, run the following command:\n\n\t$ %s\n", text.Bold(fmt.Sprintf("rustup target add %s --toolchain %s", WasmWasiTarget, RustToolchainVersion))),
}
}
fmt.Fprintf(out, "Found wasm32-wasi target\n")
// 4) Check Cargo.toml file exists in $PWD
//
// A valid Cargo.toml file is needed for the `cargo build` compilation
// process. Therefore, we assert whether one exists in the current $PWD.
fpath, err := filepath.Abs("Cargo.toml")
if err != nil {
return fmt.Errorf("error getting Cargo.toml path: %w", err)
}
if !common.FileExists(fpath) {
return fmt.Errorf("%s not found", fpath)
}
fmt.Fprintf(out, "Found Cargo.toml at %s\n", fpath)
// 5) Verify `fastly` and `fastly-sys` crate version
//
// A valid and up-to-date version of the fastly-sys crate is required.
if !common.FileExists(fpath) {
return fmt.Errorf("%s not found", fpath)
}
var metadata CargoMetadata
if err := metadata.Read(); err != nil {
return fmt.Errorf("error reading cargo metadata: %w", err)
}
// Fetch the latest crate versions from cargo.io API.
latestFastly, err := getLatestCrateVersion(r.client, "fastly")
if err != nil {
return fmt.Errorf("error fetching latest crate version: %w", err)
}
latestFastlySys, err := getLatestCrateVersion(r.client, "fastly-sys")
if err != nil {
return fmt.Errorf("error fetching latest crate version: %w", err)
}
// Create a semver contraint to be within the latest minor range or above.
// TODO(phamann): Update this to major when fastly-sys hits 1.x.x.
fastlySysConstraint, err := semver.NewConstraint(fmt.Sprintf("~%d.%d.0", latestFastlySys.Major(), latestFastlySys.Minor()))
if err != nil {
return fmt.Errorf("error parsing latest crate version: %w", err)
}
fastlySysVersion, err := getCrateVersionFromMetadata(metadata, "fastly-sys")
// If fastly-sys crate not found, error with dual remediation steps.
if err != nil {
return newCargoUpdateRemediationErr(err, latestFastly.String())
}
// If fastly-sys version doesn't meet our constraint of being within the
// minor range, error with dual remediation steps.
if ok := fastlySysConstraint.Check(fastlySysVersion); !ok {
return newCargoUpdateRemediationErr(fmt.Errorf("fastly crate not up-to-date"), latestFastly.String())
}
fastlyVersion, err := getCrateVersionFromMetadata(metadata, "fastly")
// If fastly crate not found, error with dual remediation steps.
if err != nil {
return newCargoUpdateRemediationErr(err, latestFastly.String())
}
// If fastly crate version is lower than the latest, suggest user should
// update, but don't error.
if fastlyVersion.LessThan(latestFastly) {
text.Break(out)
text.Info(out, fmt.Sprintf(
"an optional upgrade for the fastly crate is available, edit %s with:\n\n\t %s\n\nAnd then run the following command:\n\n\t$ %s\n",
text.Bold("Cargo.toml"),
text.Bold(fmt.Sprintf(`fastly = "^%s"`, latestFastly)),
text.Bold("cargo update -p fastly"),
))
text.Break(out)
}
return nil
}
// Build implments the Toolchain interface and attempts to compile the package
// Rust source to a Wasm binary.
func (r Rust) Build(out io.Writer, verbose bool) error {
// Get binary name from Cargo.toml.
var m CargoManifest
if err := m.Read("Cargo.toml"); err != nil {
return fmt.Errorf("error reading Cargo.toml manifest: %w", err)
}
binName := m.Package.Name
// Specify the toolchain using the `cargo +<version>` syntax.
toolchain := fmt.Sprintf("+%s", RustToolchainVersion)
args := []string{
toolchain,
"build",
"--bin",
binName,
"--release",
"--target",
WasmWasiTarget,
"--color",
"always",
}
if verbose {
args = append(args, "--verbose")
}
// Call cargo build with Wasm Wasi target and release flags.
// gosec flagged this:
// G204 (CWE-78): Subprocess launched with variable
// Disabling as the variables come from trusted sources.
/* #nosec */
cmd := exec.Command("cargo", args...)
// Add debuginfo RUSTFLAGS to command environment to ensure DWARF debug
// infomation (such as, source mappings) are compiled into the binary.
cmd.Env = append(os.Environ(),
`RUSTFLAGS=-C debuginfo=2`,
)
// Pipe the child process stdout and stderr to our own writer.
var stdoutBuf, stderrBuf bytes.Buffer
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := io.MultiWriter(out, &stdoutBuf)
stderr := io.MultiWriter(out, &stderrBuf)
// Start the command.
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start compilation process: %w", err)
}
var errStdout, errStderr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
if errStdout != nil {
return fmt.Errorf("error streaming stdout output from child process: %w", errStdout)
}
if errStderr != nil {
return fmt.Errorf("error streaming stderr output from child process: %w", errStderr)
}
// Wait for the command to exit.
if err := cmd.Wait(); err != nil {
// If we're not in verbose mode return the bufferred stderr output
// from cargo as the error.
if !verbose && stderrBuf.Len() > 0 {
return fmt.Errorf("error during compilation process:\n%s", strings.TrimSpace(stderrBuf.String()))
}
return fmt.Errorf("error during compilation process")
}
// Get working directory.
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current working directory: %w", err)
}
src := filepath.Join(dir, "target", WasmWasiTarget, "release", "deps", fmt.Sprintf("%s.wasm", binName))
dst := filepath.Join(dir, "bin", "main.wasm")
// Check if bin directory exists and create if not.
binDir := filepath.Join(dir, "bin")
fi, err := os.Stat(binDir)
switch {
case err == nil && fi.IsDir():
// no problem
case err == nil && !fi.IsDir():
return fmt.Errorf("error creating bin directory: target already exists as a regular file")
case os.IsNotExist(err):
if err := os.MkdirAll(binDir, 0750); err != nil {
return err
}
case err != nil:
return err
}
err = common.CopyFile(src, dst)
if err != nil {
return fmt.Errorf("error copying wasm binary: %w", err)
}
return nil
}
// CargoCrateVersion models a Cargo crate version returned by the crates.io API.
type CargoCrateVersion struct {
Version string `json:"num"`
}
// CargoCrateVersions models a Cargo crate version returned by the crates.io API.
type CargoCrateVersions struct {
Versions []CargoCrateVersion `json:"versions"`
}
// getLatestCrateVersion fetches all versions of a given Rust crate from the
// crates.io HTTP API and returns the latest valid semver version.
func getLatestCrateVersion(client api.HTTPClient, name string) (*semver.Version, error) {
url := fmt.Sprintf("https://crates.io/api/v1/crates/%s/versions", name)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error fetching latest crate version: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
crate := CargoCrateVersions{}
err = json.Unmarshal(body, &crate)
if err != nil {
return nil, err
}
var versions []*semver.Version
for _, v := range crate.Versions {
if version, err := semver.NewVersion(v.Version); err == nil {
versions = append(versions, version)
}
}
if len(versions) < 1 {
return nil, fmt.Errorf("no valid crate versions found")
}
sort.Sort(semver.Collection(versions))
latest := versions[len(versions)-1]
return latest, nil
}
// getCrateVersionFromLockfile searches for a crate inside a CargoMetadata tree
// and returns the crates version as a semver.Version.
func getCrateVersionFromMetadata(metadata CargoMetadata, crate string) (*semver.Version, error) {
// Search for crate in metadata tree.
var c CargoPackage
for _, p := range metadata.Package {
if p.Name == crate {
c = p
break
}
for _, pp := range p.Dependencies {
if pp.Name == crate {
c = pp
break
}
}
}
if c.Name == "" {
return nil, fmt.Errorf("%s crate not found", crate)
}
// Parse lockfile version to semver.Version.
version, err := semver.NewVersion(c.Version)
if err != nil {
return nil, fmt.Errorf("error parsing cargo metadata: %w", err)
}
return version, nil
}
// newCargoUpdateRemediationErr constructs a new a new RemediationError which
// wraps a cargo error and suggests to update the fastly crate to a specified
// version as its remediation message.
func newCargoUpdateRemediationErr(err error, version string) errors.RemediationError {
return errors.RemediationError{
Inner: err,
Remediation: fmt.Sprintf(
"To fix this error, edit %s with:\n\n\t %s\n\nAnd then run the following command:\n\n\t$ %s\n",
text.Bold("Cargo.toml"),
text.Bold(fmt.Sprintf(`fastly = "^%s"`, version)),
text.Bold("cargo update -p fastly"),
),
}
}