-
Notifications
You must be signed in to change notification settings - Fork 209
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
supabase functions serve: Import maps fail to resolve inside of 'functions' directory #1338
Comments
Hi! Currently I abandoned using import maps (aka path aliases) within However, since using relative paths is evil, I'm also looking for a solution for this issue to be able to use backend-only aliases for functions. I suppose it could be resolved by keeping the same folders structure as in the project folder, I mean avoid moving |
In my case this issue occurs only on Windows, on MacOS is working fine |
@idudinov Hi, could you provide an example of how you structured the file? I tried what you said but still getting errors. |
@Matx00 no problem, here's my setup. // supabase/functions/import_map.json
{
"imports": {
// here listed all packages that are used in "common"
"yup": "https://esm.sh/[email protected]",
// the "common" folder is in the root dir
"common/": "../../src/common/"
}
} Now I can reference my models from import { UserProfile } from 'common/types/models/index.ts'; However I still use relative paths for my shared code within import '../_shared/utils/bigint.ts'; Hopefully this helps, but don't hesitate to ask any questions if any. |
@laktek based on the comments here and additional research in my own project, I think the scope of this issue can be defined using the following reproduction steps. Reproduction Steps
export const foo = 'bar'
"shared/": "./_shared/"
import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { foo } from 'shared/foo.ts';
console.log("Hello from Functions!")
serve(async (req) => {
console.log(foo);
const { name } = await req.json()
const data = {
message: `Hello ${name}!`,
}
return new Response(
JSON.stringify(data),
{ headers: { "Content-Type": "application/json" } },
)
})
Expected ResultThe module should be resolving to WorkaroundMove Other NotesThe fix might be to update the bindImportMap function to detect imports inside of the |
@sweatybridge have you had a chance to look at this? It seems to boil down to the cli mounting the import map in the container at |
I was struggling with trying to get import_map.json to work, only to discover that since v1.30, Deno wants you to put import aliases in the imports object of deno.json.
See https://docs.deno.com/runtime/manual/getting_started/configuration_file#imports-and-scopes Update: This makes my IDE happy but I hit a runtime error - "Relative import path "..." not prefixed with / or ./ or ../"... Update 2: The error only occurs for executable code, it works fine for importing models |
I finally managed to look into this more. I had to use absolute path when mounting import map inside the container. Feel free to verify this fix with |
The issue persists in beta (v1.167.4). I found that the {
"imports": {
"chai/": "npm:/[email protected]/",
"drizzle-orm": "npm:[email protected]",
"drizzle-orm/": "npm:/[email protected]/",
"postgres": "npm:[email protected]",
"date-fns": "npm:[email protected]",
"date-fns-tz": "npm:[email protected]",
"langchain": "npm:[email protected]",
"supabase-js": "npm:@supabase/[email protected]",
"delay": "https://deno.land/x/[email protected]/mod.ts",
"std/": "https://deno.land/[email protected]/",
"oak/": "https://deno.land/x/[email protected]/",
"validasaur/": "https://deno.land/x/[email protected]/",
"@utils/": "./functions/_utils/",
"@repositories/": "./functions/_repositories/",
"@shared/": "./functions/_shared/",
"@tests/": "./functions/tests/"
}
} Here is the command I use as a workaround the issue: npx supabase functions serve --import-map ./supabase/import_map.json --no-verify-jwt |
Confirmed that this is also broken in the release v1.167.4. @sweatybridge Could you please re-open this issue? |
Observations using Supabase v1.168.8
|
Sorry I just double checked everything and confirmed it is a bug with importing directory paths. It will be fixed in the stable release tomorrow. The expected behaviour is that relative paths in If your import map is located at {
"imports": {
"supabase-types": "./_shared/types.ts"
},
} Otherwise, if it is located at {
"imports": {
"supabase-types": "./functions/_shared/types.ts"
},
} |
I'm noting here that the Supabase functions appear to search for "import_map.json" within the "functions" directory. The newly recommended method of defining import maps in the "deno.json" file does not seem to be working. |
Issue
It seems like import maps are failing in multiple ways:
supabase
folder, it seems like a private module is created? But this causes theDuplicate mount point
issue--import-maps
flag vs using the defaultsNote: I haven't had a chance to test any of this with deploy, but I assume these issues will carry over in one way or another...
Reproduction
(this was previously posted to #1093, but decided it deserves a new, open issue)
This is failing in my setup:
Project Structure
supabase/functions/chat/index.ts
In "supabase_edge_runtime" Docker container
home/deno
fallback_import_map.json
The deno LSP (vscode) is happy with this configuration, but when I try to invoke the
functions/chat
function, I get this error:It seems like the
fallback_import_map.json
that gets setup in the "supabase_edge_runtime" container is referencing the wrong path:"core/": "./_core/"
should be"core/": "./functions/_core/"
Also, when I try to run it with the
--import-map
flag, I get theDuplicate mount point
error as above:Update
If I update my import map to:
Then it works (as suspected), but this breaks the imports in my local deno LSP... Also, it goes against what was said in #1093:
So... I don't really understand what's going on.
My workaround was to move my import map OUTSIDE of
functions
(./supabase/import-map.json
), and now vscode and supabase serve are at least consistent. However, when I do this, it doesn't even attempt to generate the module forcommon
anymore.The text was updated successfully, but these errors were encountered: