diff --git a/src/runtimes/node/utils/zip.ts b/src/runtimes/node/utils/zip.ts index 55cb9c064..4d1a804b3 100644 --- a/src/runtimes/node/utils/zip.ts +++ b/src/runtimes/node/utils/zip.ts @@ -108,10 +108,6 @@ const createZipArchive = async function ({ const destPath = join(destFolder, `${basename(filename, extension)}.zip`) const { archive, output } = startZip(destPath) - // We don't need an entry file if it would end up with the same path as the - // function's main file. - const needsEntryFile = !isNamedLikeEntryFile(mainFile, { basePath, filename }) - // There is a naming conflict with the entry file if one of the supporting // files (i.e. not the main file) has the path that the entry file needs to // take. @@ -121,6 +117,10 @@ const createZipArchive = async function ({ mainFile, }) + // We don't need an entry file if it would end up with the same path as the + // function's main file. Unless we have a file conflict and need to move everything into a subfolder + const needsEntryFile = hasEntryFileConflict || !isNamedLikeEntryFile(mainFile, { basePath, filename }) + // If there is a naming conflict, we move all user files (everything other // than the entry file) to its own sub-directory. const userNamespace = hasEntryFileConflict ? DEFAULT_USER_SUBDIRECTORY : '' diff --git a/tests/fixtures/naming_conflict/func1/func1.js b/tests/fixtures/naming_conflict/func1/func1.js new file mode 100644 index 000000000..272ae546a --- /dev/null +++ b/tests/fixtures/naming_conflict/func1/func1.js @@ -0,0 +1,3 @@ +exports.handler = function handler (event, context) { + return import('./func1.mjs').then(m => m.handler(event, context)) +} diff --git a/tests/fixtures/naming_conflict/func1/func1.mjs b/tests/fixtures/naming_conflict/func1/func1.mjs new file mode 100644 index 000000000..767df32b9 --- /dev/null +++ b/tests/fixtures/naming_conflict/func1/func1.mjs @@ -0,0 +1 @@ +export const handler = () => true diff --git a/tests/main.test.ts b/tests/main.test.ts index d3564a918..8f86394be 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -1383,6 +1383,28 @@ describe('zip-it-and-ship-it', () => { }, ) + testMany( + 'Generates a entry file if no entry file needed but naming conflict occurs', + ['bundler_default', 'bundler_nft'], + async (options) => { + const fixtureName = 'naming_conflict' + const opts = merge(options, { + basePath: join(FIXTURES_DIR, fixtureName), + }) + const { tmpDir } = await zipNode(fixtureName, { + opts, + length: 1, + }) + + const function2Entry = await importFunctionFile(`${tmpDir}/func1.js`) + + expect(await function2Entry.handler()).toBe(true) + + await expect(`${tmpDir}/src/func1.js`).toPathExist() + await expect(`${tmpDir}/src/func1.mjs`).toPathExist() + }, + ) + testMany( 'Bundles functions from multiple directories when the first argument of `zipFunctions()` is an array', ['bundler_esbuild', 'bundler_default', 'bundler_nft'],