Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tf-gcp wrong path for cloud functions #6230

Merged
merged 4 commits into from
Apr 17, 2024
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
2 changes: 0 additions & 2 deletions libs/wingsdk/src/shared/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { normalPath } from "./misc";
const SDK_PATH = normalPath(resolve(__dirname, "..", ".."));

export interface Bundle {
entrypointPath: string;
directory: string;
hash: string;
outfilePath: string;
Expand Down Expand Up @@ -87,7 +86,6 @@ export function createBundle(
.digest("hex");

return {
entrypointPath: outfile,
directory: outdir,
hash: codeHash,
outfilePath: outfile,
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-sim/function.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class Function implements IFunctionClient, ISimulatorResourceInstance {
throw new Error("Bundle not created");
}

return new Sandbox(this.bundle.entrypointPath, {
return new Sandbox(this.bundle.outfilePath, {
env: {
...this.env,
WING_SIMULATOR_CALLER: this.context.resourceHandle,
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-sim/service.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Service implements IServiceClient, ISimulatorResourceInstance {
return;
}

this.sandbox = new Sandbox(this.bundle.entrypointPath, {
this.sandbox = new Sandbox(this.bundle.outfilePath, {
env: {
...this.environmentVariables,
WING_SIMULATOR_URL: this.context.serverUrl,
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-tf-azure/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class Function extends cloud.Function {
// Move index.js to function name directory. Every Azure function in a function app
// must be in its own folder containing an index.js and function.json files
fs.mkdirSync(`${codeDir}/${this.functionName}`);
fs.renameSync(bundle.entrypointPath, `${outDir}/index.js`);
fs.renameSync(bundle.outfilePath, `${outDir}/index.js`);

// As per documentation "a function must have exactly one trigger" so for now
// by default a function will support http get requests
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/src/target-tf-gcp/function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from "fs";
import { join } from "path";
import { join, basename } from "path";
import { AssetType, Lazy, TerraformAsset } from "cdktf";
import { Construct } from "constructs";
import { App } from "./app";
Expand Down Expand Up @@ -197,7 +197,7 @@ export class Function extends cloud.Function {
packageJson,
JSON.stringify(
{
main: "index.js",
main: basename(bundle.outfilePath),
dependencies: {
"@google-cloud/functions-framework": "^3.0.0",
"@google-cloud/datastore": "8.4.0",
Expand Down
18 changes: 18 additions & 0 deletions libs/wingsdk/test/target-tf-gcp/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readdirSync, readFileSync, existsSync } from "fs";
import { join } from "path";
import * as cdktf from "cdktf";
import { test, expect } from "vitest";
import { Function } from "../../src/cloud";
Expand All @@ -24,6 +26,22 @@ test("basic function", () => {
new Function(app, "Function", inflight);
const output = app.synth();

const functionOutDir = readdirSync(app.workdir, {
recursive: true,
withFileTypes: true,
}).find((d) => d.isDirectory())!;
const packageJson = JSON.parse(
readFileSync(
join(app.workdir, functionOutDir.name, "package.json"),
"utf-8"
)
);
const indexFilename = "index.cjs";
expect(packageJson.main).toBe(indexFilename);
expect(
existsSync(join(app.workdir, functionOutDir.name, indexFilename))
).toBeTruthy();

// THEN
expect(tfResourcesOf(output)).toEqual([
"google_cloudfunctions_function",
Expand Down
Loading