Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/deploy/functions/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
// ===Phase 1. Load codebases from source with optional runtime config.
let runtimeConfig: Record<string, unknown> = { firebase: firebaseConfig };

const targetedCodebaseConfigs = context.config!.filter((cfg) => codebases.includes(cfg.codebase));

Check warning on line 99 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion

Check warning on line 99 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

This assertion is unnecessary since it does not change the type of the expression

// Load runtime config if API is enabled and at least one targeted codebase uses it
if (checkAPIsEnabled[1] && targetedCodebaseConfigs.some(shouldUseRuntimeConfig)) {
Expand Down Expand Up @@ -237,13 +237,17 @@
const exportType = backend.someEndpoint(wantBackend, (e) => e.platform === "run")
? "tar.gz"
: "zip";

const isDart = supported.runtimeIsLanguage(wantBuilds[codebase].runtime!, "dart");

Check warning on line 241 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
const executablePaths = isDart ? ["bin/server"] : [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be something to put in deploy/functions/runtimes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should as the right long term approach. How do you feel about keeping this fix for now and address that in a follow-up?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Create a bug and assign it to ajp to assign out. It'll probably go to brittany


const packagedSource = await prepareFunctionsUpload(
options.config.projectDir,
sourceDir,
localCfg,
[...schPathSet],
undefined,
{ exportType },
{ exportType, executablePaths },
);
source.functionsSourceV2 = packagedSource?.pathToSource;
source.functionsSourceV2Hash = packagedSource?.hash;
Expand Down Expand Up @@ -416,7 +420,7 @@
.filter(
(ep) =>
backend.isBlockingTriggered(ep) &&
AUTH_BLOCKING_EVENTS.includes(ep.blockingTrigger.eventType as any),

Check warning on line 423 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 423 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `"providers/cloud.auth/eventTypes/user.beforeCreate" | "providers/cloud.auth/eventTypes/user.beforeSignIn" | "providers/cloud.auth/eventTypes/user.beforeSendEmail" | "providers/cloud.auth/eventTypes/user.beforeSendSms"`
) as (backend.Endpoint & backend.BlockingTriggered)[];

if (authBlockingEndpoints.length === 0) {
Expand Down Expand Up @@ -540,7 +544,7 @@
// Genkit almost always requires an API key, so warn if the customer is about to deploy
// a function and doesn't have one. To avoid repetitive nagging, only warn on the first
// deploy of the function.
export async function warnIfNewGenkitFunctionIsMissingSecrets(

Check warning on line 547 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment

Check warning on line 547 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
have: backend.Backend,
want: backend.Backend,
options: DeployOptions,
Expand Down Expand Up @@ -574,7 +578,7 @@

// Enable required APIs. This may come implicitly from triggers (e.g. scheduled triggers
// require cloudscheduler and, in v1, require pub/sub), use of features (secrets), or explicit dependencies.
export async function ensureAllRequiredAPIsEnabled(

Check warning on line 581 in src/deploy/functions/prepare.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
projectNumber: string,
wantBackend: backend.Backend,
): Promise<void> {
Expand Down
30 changes: 30 additions & 0 deletions src/deploy/functions/prepareFunctionsUpload.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -93,4 +96,31 @@
}
});
});

describe("addFilesToArchive", () => {
it("should set mode to 0o755 for executable paths", async () => {
const archive = {
file: sinon.stub(),
Comment thread
lahirumaramba marked this conversation as resolved.
Resolved
} as unknown as archiver.Archiver;

const files = [
{ name: path.join("src", "index.js"), mode: 0o644 },
{ name: path.join("src", "bin", "server"), mode: 0o644 },
Comment thread
inlined marked this conversation as resolved.
Resolved
];

const getSourceHashStub = sinon.stub(hashModule, "getSourceHash").resolves("hash");

await prepareFunctionsUpload.addFilesToArchive(archive, files, "src", ["bin/server"]);

Comment thread
lahirumaramba marked this conversation as resolved.
Resolved
expect((archive.file as sinon.SinonStub).calledTwice).to.be.true;

expect((archive.file as sinon.SinonStub).firstCall.args[1].name).to.equal("index.js");

Check warning on line 117 in src/deploy/functions/prepareFunctionsUpload.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .name on an `any` value
expect((archive.file as sinon.SinonStub).firstCall.args[1].mode).to.equal(0o644);

Check warning on line 118 in src/deploy/functions/prepareFunctionsUpload.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .mode on an `any` value

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();
});
});
});
42 changes: 31 additions & 11 deletions src/deploy/functions/prepareFunctionsUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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;
Comment thread
lahirumaramba marked this conversation as resolved.
Resolved
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<PackagedSourceInfo | undefined> {
const exportType = options?.exportType || "zip";
const postfix = `.${exportType}`;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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<PackagedSourceInfo | undefined> {
return packageSource(projectDir, sourceDir, config, additionalSources, runtimeConfig, options);
}
Expand Down
Loading