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

feat: set default Rust toolchain #715

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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