sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.09k stars 126 forks source link

App is not linked on Cloudflare Workers #438

Open Ernxst opened 1 month ago

Ernxst commented 1 month ago

Resource.App does not currently work on Cloudflare Workers. Using 0.0.364 with the following worker script and config:

// functions/worker.ts
import { Resource } from "sst";

export default {
  async fetch(request, env) {
    return Response.json({ stage: Resource.App.stage })
  }
} satisfies ExportedHandler
// sst.config.ts
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
    app(input) {
        return {
            name: "ion",
            removal: input.stage === "production" ? "retain" : "remove",
            home: "cloudflare",
            version: "0.0.364"
        };
    },
    async run() {
        const worker = new sst.cloudflare.Worker(
            "MyFunction",
            {
                handler: "./functions/worker.ts",
                url: true,
            }
        );

        return {
            url: worker.url,
        }
    },
});

Invoking the worker throws a Cloudflare error. Examining the log stream in the Cloudflare dashboard shows the following exception:

  "exceptions": [
    {
      "stack": "    at Object.get (worker.mjs:57:11)\n    at fetch (worker.mjs:64:44)\n    at Object.fetch (worker.mjs:41:14)",
      "name": "Error",
      "message": "\"App\" is not linked",
      "timestamp": 1715985053747
    }
  ],

Upon further investigation, going to the worker in the Cloudflare dashboard then to the Variables tab shows:

image

Then, a quick look in sdk/js/src/resource.ts#L27 shows it populates the key from the Cloudflare env as-is without stripping the SST_RESOURCE_ prefix like it does when reading from process.env on line 16.