Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: set default Rust toolchain (#715)
Browse files Browse the repository at this point in the history
* feat: use RUSTUP_TOOLCHAIN env var

* refactor: set default toolchain

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] committed Oct 11, 2021
1 parent 31aa7d8 commit 65c32c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
23 changes: 14 additions & 9 deletions src/runtimes/rust/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const build = async ({ config, name, srcDir }) => {
const functionName = basename(srcDir)

try {
await installBuildTarget()
await installToolchainOnce()
} catch (error) {
error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: RUNTIME_RUST } }

Expand Down Expand Up @@ -98,17 +98,22 @@ const getTargetDirectory = async ({ config, name }) => {
return path
}

let buildTargetInstallation
let toolchainInstallation

// Installs the build target defined in `BUILD_TARGET`. The Promise is saved to
// `buildTargetInstallation` so that we run the command just once for multiple
// Rust functions.
const installBuildTarget = () => {
if (buildTargetInstallation === undefined) {
buildTargetInstallation = runCommand('rustup', ['target', 'add', BUILD_TARGET])
// Sets the default toolchain and installs the build target defined in
// `BUILD_TARGET`. The Promise is saved to `toolchainInstallation`, so
// that we run the command just once for multiple Rust functions.
const installToolchain = async () => {
await runCommand('rustup', ['default', 'stable'])
await runCommand('rustup', ['target', 'add', BUILD_TARGET])
}

const installToolchainOnce = () => {
if (toolchainInstallation === undefined) {
toolchainInstallation = installToolchain()
}

return buildTargetInstallation
return toolchainInstallation
}

module.exports = { build }
30 changes: 18 additions & 12 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1985,26 +1985,32 @@ test.serial('Builds Rust functions from source if the `buildRustSource` feature
})

t.is(files.length, 2)
t.is(shellUtilsStub.callCount, 3)
// eslint-disable-next-line no-magic-numbers
t.is(shellUtilsStub.callCount, 4)

const { args: call1 } = shellUtilsStub.getCall(0)
const { args: call2 } = shellUtilsStub.getCall(1)
const { args: call3 } = shellUtilsStub.getCall(1)
const { args: call3 } = shellUtilsStub.getCall(2)
const { args: call4 } = shellUtilsStub.getCall(3)

t.is(call1[0], 'rustup')
t.is(call1[1][0], 'target')
t.is(call1[1][1], 'add')
t.is(call1[1][2], 'x86_64-unknown-linux-musl')
t.is(call1[1][0], 'default')
t.is(call1[1][1], 'stable')

t.is(call2[0], 'cargo')
t.is(call2[1][0], 'build')
t.is(call2[1][1], '--target')
t.is(call2[0], 'rustup')
t.is(call2[1][0], 'target')
t.is(call2[1][1], 'add')
t.is(call2[1][2], 'x86_64-unknown-linux-musl')

t.is(call2[0], call3[0])
t.is(call2[1][0], call3[1][0])
t.is(call2[1][1], call3[1][1])
t.is(call2[1][2], call3[1][2])
t.is(call3[0], 'cargo')
t.is(call3[1][0], 'build')
t.is(call3[1][1], '--target')
t.is(call3[1][2], 'x86_64-unknown-linux-musl')

t.is(call4[0], call3[0])
t.is(call4[1][0], call3[1][0])
t.is(call4[1][1], call3[1][1])
t.is(call4[1][2], call3[1][2])

t.is(files[0].mainFile, join(FIXTURES_DIR, fixtureName, 'rust-func-1', 'src', 'main.rs'))
t.is(files[0].name, 'rust-func-1')
Expand Down

1 comment on commit 65c32c2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

largeDepsEsbuild: 10.8s

largeDepsZisi: 1m 0.5s

Please sign in to comment.