[go: nahoru, domu]

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

Spin Build Error: Unknown imported function: 'fermyon:spin/config' 'get-config' #220

Open
MikeTeddyOmondi opened this issue Jan 26, 2024 · 2 comments

Comments

@MikeTeddyOmondi
Copy link
MikeTeddyOmondi commented Jan 26, 2024

I encountered an error while building an application with Spin when trying to use the Config import from the spin SDK (@fermyon/spin-sdk)

Environment

Linux - Ubuntu 22.04
Spin version - 2.1.0 (f33bb63 2023-12-14)

Source Code

I used the spin-http-ts template to generate the project

spin.toml

spin_manifest_version = 2

[application]
authors = ["MikeTeddyOmondi <mike_omondi@outlook.com>"]
description = "API w/ TS & Spin"
name = "spin-ts-api"
version = "0.1.0"

[[trigger.http]]
route = "/..."
component = "spin-ts-api"

[component.spin-ts-api]
source = "target/spin-ts-api.wasm"
exclude_files = ["**/node_modules"]
allowed_outbound_hosts = ["libsql://localhost:8080"]
[component.spin-ts-api.variables]
libsql_url = "libsql://localhost:8080?tls=0"
[component.spin-ts-api.build]
command = "npm run build"

package.json

{
  "name": "spin-ts-api",
  "version": "1.0.0",
  "description": "API w/ TS & Spin",
  "main": "index.js",
  "scripts": {
    "build": "npx webpack --mode=production && mkdir -p target && spin js2wasm -o target/spin-ts-api.wasm dist/spin.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "generate:migrations": "drizzle-kit generate:sqlite",
    "run:migrations": "node ./src/migrate.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/uuid": "^9.0.8",
    "drizzle-kit": "^0.20.13",
    "ts-loader": "^9.4.1",
    "typescript": "^4.8.4",
    "webpack": "^5.74.0",
    "webpack-cli": "^4.10.0"
  },
  "dependencies": {
    "@fermyon/spin-sdk": "0.6.0",
    "@libsql/client": "^0.4.0",
    "drizzle-orm": "^0.29.3",
    "uuid": "^9.0.1"
  }
}

index.ts - pulled from this example

import {
  HandleRequest,
  HttpRequest,
  HttpResponse,
  Router, 
  Config // THIS DOES NOT WORK BUT DOES WHEN COMMENTED OUT 
} from "@fermyon/spin-sdk";
import { withCorrelationId } from "./middlewares";

const encoder = new TextEncoder();
const decoder = new TextDecoder("utf-8");
const router = Router();

// let libsql_url = Config.get("libsql_url"); // THIS IS WHERE I WANTED TO USE THE CONFIG IMPORT

// ...
// rest of the code goes here
// ....

Unexpected Behaviour:

picasso@picasso:~/Desktop/src/projects/wasm/spin/spin-ts-api$ spin build
Building component spin-ts-api with `npm run build`

> spin-ts-api@1.0.0 build
> npx webpack --mode=production && mkdir -p target && spin js2wasm -o target/spin-ts-api.wasm dist/spin.js

asset spin.js 13.9 KiB [compared for emit] (name: main)
orphan modules 6.09 KiB [orphan] 12 modules
runtime modules 937 bytes 4 modules
cacheable modules 10.8 KiB
  ./src/index.ts + 6 modules 10.2 KiB [built] [code generated]
  ./node_modules/typedarray-to-buffer/index.js 646 bytes [built] [code generated]
webpack 5.90.0 compiled successfully in 1791 ms

Starting to build Spin compatible module
Preinitiating using Wizer
Error: the `wizer.initialize` function trapped

Caused by:
    0: error while executing at wasm backtrace:
           0: 0x6c4c0 - <unknown>!spin_sdk::wit::fermyon::spin::config::get_config::h4d5943445acc57cc
           1: 0x2905e - <unknown>!spin_js_engine::spin_get_config::h125755b80cf5df1a
           2: 0x18588 - <unknown>!quickjs_wasm_rs::js_binding::context::build_trampoline::trampoline::hdd2b62f9d273076d
           3: 0x93696 - <unknown>!js_c_function_data_call
           4: 0x94d53 - <unknown>!JS_CallInternal
           5: 0x96b35 - <unknown>!JS_CallInternal
           6: 0x96971 - <unknown>!JS_CallInternal
           7: 0x96971 - <unknown>!JS_CallInternal
           8: 0xc4301 - <unknown>!JS_EvalFunctionInternal
           9: 0xa8f36 - <unknown>!__JS_EvalInternal
          10: 0xc5209 - <unknown>!JS_Eval
          11: 0x78604 - <unknown>!quickjs_wasm_rs::js_binding::context::Context::eval_global::h5e906feb1352150b
          12: 0x364e5 - <unknown>!wizer.initialize
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    1: Error: attempted to call an unknown imported function: 'fermyon:spin/config' 'get-config'
       
       You cannot call arbitrary imported functions during Wizer initialization.
Error: Couldn't create wasm from input
Error: Build command for component spin-ts-api failed with status Exited(1)

The applications builds when the import is removed

picasso@picasso:~/Desktop/src/projects/wasm/spin/spin-ts-api$ spin build
Building component spin-ts-api with `npm run build`

> spin-ts-api@1.0.0 build
> npx webpack --mode=production && mkdir -p target && spin js2wasm -o target/spin-ts-api.wasm dist/spin.js

asset spin.js 13.9 KiB [emitted] (name: main)
orphan modules 6.09 KiB [orphan] 12 modules
runtime modules 937 bytes 4 modules
cacheable modules 10.8 KiB
  ./src/index.ts + 6 modules 10.2 KiB [built] [code generated]
  ./node_modules/typedarray-to-buffer/index.js 646 bytes [built] [code generated]
webpack 5.90.0 compiled successfully in 1753 ms

Starting to build Spin compatible module
Preinitiating using Wizer
Optimizing wasm binary using wasm-opt
Spin compatible module built successfully
Finished building all Spin components

Expected Behaviour

I expected to use the Config import to import secrets defined in the spin.toml file

@karthik2804
Copy link
Collaborator

Hi @MikeTeddyOmondi , The Spin host functions are only available inside the handler function (handleRequest). The error you run into is because we use wizer to pre-initialize the wasm module and that does not use the spin runtime.

You would also want to use configs inside the function because anything outside will essentially be run only once during build time. Let me know if I can help further.

@MikeTeddyOmondi
Copy link
Author

@karthik2804 That's true... I had to move the router and its handlers to the main function for it to compile safely....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants