diff --git a/src/deploy/functions/prepare.ts b/src/deploy/functions/prepare.ts index 46ed0c2e096..a9461318156 100644 --- a/src/deploy/functions/prepare.ts +++ b/src/deploy/functions/prepare.ts @@ -237,13 +237,17 @@ export async function prepare( const exportType = backend.someEndpoint(wantBackend, (e) => e.platform === "run") ? "tar.gz" : "zip"; + + const isDart = supported.runtimeIsLanguage(wantBuilds[codebase].runtime!, "dart"); + const executablePaths = isDart ? ["bin/server"] : []; + const packagedSource = await prepareFunctionsUpload( options.config.projectDir, sourceDir, localCfg, [...schPathSet], undefined, - { exportType }, + { exportType, executablePaths }, ); source.functionsSourceV2 = packagedSource?.pathToSource; source.functionsSourceV2Hash = packagedSource?.hash; diff --git a/src/deploy/functions/prepareFunctionsUpload.spec.ts b/src/deploy/functions/prepareFunctionsUpload.spec.ts index 8bb0e596433..4f4757586f1 100644 --- a/src/deploy/functions/prepareFunctionsUpload.spec.ts +++ b/src/deploy/functions/prepareFunctionsUpload.spec.ts @@ -1,4 +1,7 @@ import { expect } from "chai"; +import * as sinon from "sinon"; +import * as hashModule from "./cache/hash"; +import * as archiver from "archiver"; import * as os from "os"; import * as path from "path"; import * as fs from "fs"; @@ -93,4 +96,31 @@ describe("prepareFunctionsUpload", () => { } }); }); + + describe("addFilesToArchive", () => { + it("should set mode to 0o755 for executable paths", async () => { + const archive = { + file: sinon.stub(), + } as unknown as archiver.Archiver; + + const files = [ + { name: path.join("src", "index.js"), mode: 0o644 }, + { name: path.join("src", "bin", "server"), mode: 0o644 }, + ]; + + const getSourceHashStub = sinon.stub(hashModule, "getSourceHash").resolves("hash"); + + await prepareFunctionsUpload.addFilesToArchive(archive, files, "src", ["bin/server"]); + + expect((archive.file as sinon.SinonStub).calledTwice).to.be.true; + + expect((archive.file as sinon.SinonStub).firstCall.args[1].name).to.equal("index.js"); + expect((archive.file as sinon.SinonStub).firstCall.args[1].mode).to.equal(0o644); + + expect((archive.file as sinon.SinonStub).secondCall.args[1].name).to.equal("bin/server"); + expect((archive.file as sinon.SinonStub).secondCall.args[1].mode).to.equal(0o755); + + getSourceHashStub.restore(); + }); + }); }); diff --git a/src/deploy/functions/prepareFunctionsUpload.ts b/src/deploy/functions/prepareFunctionsUpload.ts index 0323093d8cc..7bfc579fbb9 100644 --- a/src/deploy/functions/prepareFunctionsUpload.ts +++ b/src/deploy/functions/prepareFunctionsUpload.ts @@ -56,13 +56,41 @@ async function pipeAsync(from: archiver.Archiver, to: fs.WriteStream) { }); } +/** + * Adds files to the archive, forcing executable permissions for specified paths. + * @internal + */ +export async function addFilesToArchive( + archive: archiver.Archiver, + files: fsAsync.ReaddirRecursiveFile[], + sourceDir: string, + executablePaths?: string[], +): Promise { + const hashes: string[] = []; + for (const file of files) { + const name = path.relative(sourceDir, file.name); + const normalizedName = name.split(path.sep).join("/"); + const fileHash = await getSourceHash(file.name); + hashes.push(fileHash); + let mode = file.mode; + if (executablePaths?.includes(normalizedName)) { + mode = 0o755; + } + archive.file(file.name, { + name: normalizedName, + mode, + }); + } + return hashes; +} + async function packageSource( projectDir: string, sourceDir: string, config: projectConfig.ValidatedSingle, additionalSources: string[], runtimeConfig: any, - options?: { exportType: "zip" | "tar.gz" }, + options?: { exportType: "zip" | "tar.gz"; executablePaths?: string[] }, ): Promise { const exportType = options?.exportType || "zip"; const postfix = `.${exportType}`; @@ -87,15 +115,7 @@ async function packageSource( ); try { const files = await fsAsync.readdirRecursive({ path: sourceDir, ignore: ignore }); - for (const file of files) { - const name = path.relative(sourceDir, file.name); - const fileHash = await getSourceHash(file.name); - hashes.push(fileHash); - archive.file(file.name, { - name, - mode: file.mode, - }); - } + hashes.push(...(await addFilesToArchive(archive, files, sourceDir, options?.executablePaths))); for (const name of additionalSources) { const absPath = utils.resolveWithin(projectDir, name); if (!fs.existsSync(absPath)) { @@ -159,7 +179,7 @@ export async function prepareFunctionsUpload( config: projectConfig.ValidatedSingle, additionalSources: string[], runtimeConfig?: backend.RuntimeConfigValues, - options?: { exportType: "zip" | "tar.gz" }, + options?: { exportType: "zip" | "tar.gz"; executablePaths?: string[] }, ): Promise { return packageSource(projectDir, sourceDir, config, additionalSources, runtimeConfig, options); }