@@ -6,6 +6,7 @@ import { checkSumOfFiles } from './checksumOfFiles.js'
6
6
import { commonParent } from './commonParent.js'
7
7
import { findDependencies } from './findDependencies.js'
8
8
import { fileURLToPath } from 'node:url'
9
+ import path from 'node:path'
9
10
10
11
export type PackedLambda = {
11
12
id : string
@@ -43,12 +44,21 @@ export const packLambda = async ({
43
44
debug ?: ( label : string , info : string ) => void
44
45
progress ?: ( label : string , info : string ) => void
45
46
} ) : Promise < { handler : string ; hash : string } > => {
46
- const lambdaFiles = [ sourceFile , ...findDependencies ( sourceFile ) ]
47
+ const deps = findDependencies ( sourceFile )
48
+ const lambdaFiles = [ sourceFile , ...deps ]
47
49
48
50
const zipfile = new yazl . ZipFile ( )
49
51
50
52
const stripCommon = removeCommonAncestor ( commonParent ( lambdaFiles ) )
51
53
54
+ const handler = stripCommon ( sourceFile )
55
+
56
+ const folderNames = new Set ( deps . map ( stripCommon ) . map ( ( s ) => s . split ( '/' ) [ 0 ] ) )
57
+ const handlerName = path . parse ( handler ) . name
58
+ if ( folderNames . has ( handlerName ) ) {
59
+ throw new ImportFromFolderNameError ( handlerName )
60
+ }
61
+
52
62
for ( const file of lambdaFiles ) {
53
63
const compiled = (
54
64
await swc . transformFile ( file , {
@@ -91,3 +101,17 @@ export const packLambda = async ({
91
101
92
102
return { handler : stripCommon ( sourceFile ) , hash }
93
103
}
104
+
105
+ /**
106
+ * @see https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/93#issuecomment-2042201321
107
+ */
108
+ export class ImportFromFolderNameError extends Error {
109
+ public readonly folderName : string
110
+ constructor ( folderName : string ) {
111
+ super (
112
+ `Import from folder with same name as handler ("${ folderName } ") not allowed!` ,
113
+ )
114
+ this . name = 'ImportFromFolderNameError'
115
+ this . folderName = folderName
116
+ }
117
+ }
0 commit comments