Skip to content

Commit

Permalink
fix: tf-gcp wrong path for cloud functions (#6230)
Browse files Browse the repository at this point in the history
Fixes #6229

Also removed a duplicate part of the bundling API.

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Apr 17, 2024
1 parent cb17e0f commit 719df34
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
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 @@ -147,7 +147,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

0 comments on commit 719df34

Please sign in to comment.