@@ -47,9 +47,10 @@ class RustPlugin {
47
47
dockerTag : DEFAULT_DOCKER_TAG ,
48
48
dockerImage : DEFAULT_DOCKER_IMAGE ,
49
49
dockerless : false ,
50
+ strictMode : true ,
50
51
} ,
51
52
( this . serverless . service . custom && this . serverless . service . custom . rust ) ||
52
- { }
53
+ { } ,
53
54
) ;
54
55
55
56
// Docker can't access resources outside of the current build directory.
@@ -74,15 +75,13 @@ class RustPlugin {
74
75
""
75
76
) . split ( / \s + / ) ;
76
77
78
+ let target = ( funcArgs || { } ) . target || this . custom . target ;
77
79
78
- let target = ( funcArgs || { } ) . target || this . custom . target
79
-
80
- const targetArgs =
81
- target ?
82
- [ '--target' , target ]
83
- : MUSL_PLATFORMS . includes ( platform )
84
- ? [ "--target" , "x86_64-unknown-linux-musl" ]
85
- : [ ] ;
80
+ const targetArgs = target
81
+ ? [ "--target" , target ]
82
+ : MUSL_PLATFORMS . includes ( platform )
83
+ ? [ "--target" , "x86_64-unknown-linux-musl" ]
84
+ : [ ] ;
86
85
return [
87
86
...defaultArgs ,
88
87
...profileArgs ,
@@ -94,30 +93,29 @@ class RustPlugin {
94
93
localBuildEnv ( funcArgs , env , platform ) {
95
94
const defaultEnv = { ...env } ;
96
95
97
- let target = ( funcArgs || { } ) . target || this . custom . target
98
- let linker = ( funcArgs || { } ) . linker || this . custom . linker
96
+ let target = ( funcArgs || { } ) . target || this . custom . target ;
97
+ let linker = ( funcArgs || { } ) . linker || this . custom . linker ;
99
98
100
- const platformEnv =
101
- linker ?
102
- {
99
+ const platformEnv = linker
100
+ ? {
103
101
RUSTFLAGS : ( env [ "RUSTFLAGS" ] || "" ) + ` -Clinker=${ linker } ` ,
104
102
TARGET_CC : linker ,
105
- [ `CC_${ target || ' x86_64_unknown_linux_musl' } ` ] : linker ,
103
+ [ `CC_${ target || " x86_64_unknown_linux_musl" } ` ] : linker ,
106
104
}
107
- : "win32" === platform
105
+ : "win32" === platform
106
+ ? {
107
+ RUSTFLAGS : ( env [ "RUSTFLAGS" ] || "" ) + " -Clinker=rust-lld" ,
108
+ TARGET_CC : "rust-lld" ,
109
+ CC_x86_64_unknown_linux_musl : "rust-lld" ,
110
+ }
111
+ : "darwin" === platform
108
112
? {
109
- RUSTFLAGS : ( env [ "RUSTFLAGS" ] || "" ) + " -Clinker=rust-lld" ,
110
- TARGET_CC : "rust-lld" ,
111
- CC_x86_64_unknown_linux_musl : "rust-lld" ,
113
+ RUSTFLAGS :
114
+ ( env [ "RUSTFLAGS" ] || "" ) + " -Clinker=x86_64-linux-musl-gcc" ,
115
+ TARGET_CC : "x86_64-linux-musl-gcc" ,
116
+ CC_x86_64_unknown_linux_musl : "x86_64-linux-musl-gcc" ,
112
117
}
113
- : "darwin" === platform
114
- ? {
115
- RUSTFLAGS :
116
- ( env [ "RUSTFLAGS" ] || "" ) + " -Clinker=x86_64-linux-musl-gcc" ,
117
- TARGET_CC : "x86_64-linux-musl-gcc" ,
118
- CC_x86_64_unknown_linux_musl : "x86_64-linux-musl-gcc" ,
119
- }
120
- : { } ;
118
+ : { } ;
121
119
return {
122
120
...defaultEnv ,
123
121
...platformEnv ,
@@ -127,8 +125,11 @@ class RustPlugin {
127
125
localSourceDir ( funcArgs , profile , platform ) {
128
126
let executable = "target" ;
129
127
if ( MUSL_PLATFORMS . includes ( platform ) ) {
130
- let target = ( funcArgs || { } ) . target || this . custom . target
131
- executable = path . join ( executable , target ? target : "x86_64-unknown-linux-musl" ) ;
128
+ let target = ( funcArgs || { } ) . target || this . custom . target ;
129
+ executable = path . join (
130
+ executable ,
131
+ target ? target : "x86_64-unknown-linux-musl" ,
132
+ ) ;
132
133
}
133
134
return path . join ( executable , profile !== "dev" ? "release" : "debug" ) ;
134
135
}
@@ -137,7 +138,7 @@ class RustPlugin {
137
138
return path . join (
138
139
"target" ,
139
140
"lambda" ,
140
- profile !== "dev" ? "release" : "debug"
141
+ profile !== "dev" ? "release" : "debug" ,
141
142
) ;
142
143
}
143
144
@@ -147,7 +148,7 @@ class RustPlugin {
147
148
cargoPackage ,
148
149
binary ,
149
150
profile ,
150
- platform ( )
151
+ platform ( ) ,
151
152
) ;
152
153
153
154
const env = this . localBuildEnv ( funcArgs , process . env , platform ( ) ) ;
@@ -169,7 +170,7 @@ class RustPlugin {
169
170
"bootstrap" ,
170
171
readFileSync ( path . join ( sourceDir , binary ) ) ,
171
172
"" ,
172
- 0o755
173
+ 0o755 ,
173
174
) ;
174
175
const targetDir = this . localArtifactDir ( profile ) ;
175
176
try {
@@ -195,7 +196,7 @@ class RustPlugin {
195
196
srcPath ,
196
197
cargoRegistry ,
197
198
cargoDownloads ,
198
- env
199
+ env ,
199
200
) {
200
201
const defaultArgs = [
201
202
"run" ,
@@ -251,7 +252,7 @@ class RustPlugin {
251
252
this . srcPath ,
252
253
cargoRegistry ,
253
254
cargoDownloads ,
254
- process . env
255
+ process . env ,
255
256
) ;
256
257
257
258
this . serverless . cli . log ( "Running containerized build" ) ;
@@ -281,6 +282,7 @@ class RustPlugin {
281
282
282
283
/** the entry point for building functions */
283
284
build ( ) {
285
+ const strictMode = this . custom . strictMode !== false ;
284
286
const service = this . serverless . service ;
285
287
if ( service . provider . name != "aws" ) {
286
288
return ;
@@ -289,55 +291,65 @@ class RustPlugin {
289
291
this . functions ( ) . forEach ( ( funcName ) => {
290
292
const func = service . getFunction ( funcName ) ;
291
293
const runtime = func . runtime || service . provider . runtime ;
292
- if ( runtime != RUST_RUNTIME ) {
294
+
295
+ func . tags = func . tags || { } ;
296
+ if ( ! ( runtime === RUST_RUNTIME || func . tags . language === "rust" ) ) {
293
297
// skip functions which don't apply to rust
294
298
return ;
295
299
}
296
300
rustFunctionsFound = true ;
297
- const { cargoPackage, binary } = this . cargoBinary ( func ) ;
298
301
299
- this . serverless . cli . log ( `Building Rust ${ func . handler } func...` ) ;
300
- let profile = ( func . rust || { } ) . profile || this . custom . profile ;
301
-
302
- const res = this . buildLocally ( func )
303
- ? this . localBuild ( func . rust , cargoPackage , binary , profile )
304
- : this . dockerBuild ( func . rust , cargoPackage , binary , profile ) ;
305
- if ( res . error || res . status > 0 ) {
302
+ func . package = func . package || { } ;
303
+ if ( func . package . artifact && func . package . artifact !== "" ) {
306
304
this . serverless . cli . log (
307
- `Rust build encountered an error: ${ res . error } ${ res . status } .`
305
+ `Artifact defined for ${ func . handler } , skipping build...` ,
308
306
) ;
309
- throw new Error ( res . error ) ;
310
- }
311
- // If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
312
- //
313
- // The AWS "provided" lambda runtime requires executables to be named
314
- // "bootstrap" -- https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
315
- //
316
- // To avoid artifact naming conflicts when we potentially have more than one function
317
- // we leverage the ability to declare a package artifact directly
318
- // see https://serverless.com/framework/docs/providers/aws/guide/packaging/
319
- // for more information
320
- const artifactPath = path . join (
321
- this . srcPath ,
322
- `target/lambda/${ "dev" === profile ? "debug" : "release" } ` ,
323
- `${ binary } .zip`
324
- ) ;
325
- func . package = func . package || { } ;
326
- func . package . artifact = artifactPath ;
307
+ } else {
308
+ const { cargoPackage, binary } = this . cargoBinary ( func ) ;
309
+
310
+ this . serverless . cli . log ( `Building Rust ${ func . handler } func...` ) ;
311
+ let profile = ( func . rust || { } ) . profile || this . custom . profile ;
312
+
313
+ const res = this . buildLocally ( func )
314
+ ? this . localBuild ( func . rust , cargoPackage , binary , profile )
315
+ : this . dockerBuild ( func . rust , cargoPackage , binary , profile ) ;
316
+ if ( res . error || res . status > 0 ) {
317
+ this . serverless . cli . log (
318
+ `Rust build encountered an error: ${ res . error } ${ res . status } .` ,
319
+ ) ;
320
+ throw new Error ( res . error ) ;
321
+ }
322
+ // If all went well, we should now have find a packaged compiled binary under `target/lambda/release`.
323
+ //
324
+ // The AWS "provided" lambda runtime requires executables to be named
325
+ // "bootstrap" -- https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
326
+ //
327
+ // To avoid artifact naming conflicts when we potentially have more than one function
328
+ // we leverage the ability to declare a package artifact directly
329
+ // see https://serverless.com/framework/docs/providers/aws/guide/packaging/
330
+ // for more information
331
+ const artifactPath = path . join (
332
+ this . srcPath ,
333
+ `target/lambda/${ "dev" === profile ? "debug" : "release" } ` ,
334
+ `${ binary } .zip` ,
335
+ ) ;
336
+ func . package = func . package || { } ;
337
+ func . package . artifact = artifactPath ;
327
338
328
- // Ensure the runtime is set to a sane value for other plugins
329
- if ( func . runtime == RUST_RUNTIME ) {
330
- func . runtime = BASE_RUNTIME ;
339
+ // Ensure the runtime is set to a sane value for other plugins
340
+ if ( func . runtime == RUST_RUNTIME ) {
341
+ func . runtime = BASE_RUNTIME ;
342
+ }
331
343
}
332
344
} ) ;
333
345
if ( service . provider . runtime === RUST_RUNTIME ) {
334
346
service . provider . runtime = BASE_RUNTIME ;
335
347
}
336
- if ( ! rustFunctionsFound ) {
348
+ if ( ! rustFunctionsFound && strictMode ) {
337
349
throw new Error (
338
350
`Error: no Rust functions found. ` +
339
351
`Use 'runtime: ${ RUST_RUNTIME } ' in global or ` +
340
- `function configuration to use this plugin.`
352
+ `function configuration to use this plugin.` ,
341
353
) ;
342
354
}
343
355
}
0 commit comments