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

Commit

Permalink
fix: install Rust build target (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jul 27, 2021
1 parent 96c8335 commit 1ce8157
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/runtimes/rust/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const { runCommand } = require('../../utils/shell')
const { BUILD_TARGET, MANIFEST_NAME } = require('./constants')

const build = async ({ srcDir }) => {
await installBuildTarget()

// We compile the binary to a temporary directory so that we don't pollute
// the user's functions directory.
const { path: targetDirectory } = await tmp.dir()
Expand Down Expand Up @@ -64,4 +66,17 @@ const checkRustToolchain = async () => {
}
}

let buildTargetInstallation

// 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])
}

return buildTargetInstallation
}

module.exports = { build }
26 changes: 18 additions & 8 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1662,12 +1662,16 @@ test.serial(

test.serial('Builds Rust functions from source if the `buildRustSource` feature flag is enabled', async (t) => {
shellUtilsStub.callsFake(async (...args) => {
const directory = join(args[2].env.CARGO_TARGET_DIR, args[1][2], 'release')
const binaryPath = join(directory, 'hello')
const [rootCommand] = args

await makeDir(directory)
if (rootCommand === 'cargo') {
const directory = join(args[2].env.CARGO_TARGET_DIR, args[1][2], 'release')
const binaryPath = join(directory, 'hello')

return pWriteFile(binaryPath, '')
await makeDir(directory)

return pWriteFile(binaryPath, '')
}
})

const fixtureName = 'rust-source'
Expand All @@ -1679,15 +1683,21 @@ test.serial('Builds Rust functions from source if the `buildRustSource` feature
},
})

t.is(shellUtilsStub.callCount, 1)
t.is(shellUtilsStub.callCount, 2)

const { args: call1 } = shellUtilsStub.getCall(0)
const { args: call2 } = shellUtilsStub.getCall(1)

t.is(call1[0], 'cargo')
t.is(call1[1][0], 'build')
t.is(call1[1][1], '--target')
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(call2[0], 'cargo')
t.is(call2[1][0], 'build')
t.is(call2[1][1], '--target')
t.is(call2[1][2], 'x86_64-unknown-linux-musl')

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

1 comment on commit 1ce8157

@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: 12.9s

largeDepsZisi: 1m 2.9s

Please sign in to comment.