Bloxs
Bloxs•7mo ago

Deno LSP doesn't recognize workspaces as imports

For some reason the vscode plugin for deno says this code is invalid even though it runs perfectly fine via the cli. mod.ts:
import { add } from "@mypackage/test";

console.log(add(1, 2)); // 3
import { add } from "@mypackage/test";

console.log(add(1, 2)); // 3
deno.jsonc:
{
"name": "@mypackage/root",
"version": "0.0.1",
"workspaces": [
"./test"
]
}
{
"name": "@mypackage/root",
"version": "0.0.1",
"workspaces": [
"./test"
]
}
./test/mod.ts
export const add = (a: number, b: number) => a + b;
export const add = (a: number, b: number) => a + b;
./test/deno.jsonc
{
"name": "@mypackage/test",
"version": "0.0.1",
"exports": "./mod.ts"
}
{
"name": "@mypackage/test",
"version": "0.0.1",
"exports": "./mod.ts"
}
When I run mod.ts via the cli I get 3 which is expected but the vscode lsp says Relative import path "@mypackage/test" not prefixed with / or ./ or ../
3 Replies
Deno AI Helper
Deno AI Helper•7mo ago
It seems like you're encountering an issue where the Deno Language Server in Visual Studio Code is not recognizing workspace imports. According to the documentation, Deno should automatically detect and apply an import map specified in a deno.json or deno.jsonc configuration file at the root of your workspace. However, from the configuration you've provided, it appears that you're using the workspaces feature, which is different from import maps. The workspaces feature is used to manage multiple Deno projects within a single repository, but it does not automatically create import aliases that can be used in your code. To resolve the issue with the import path not being recognized, you should define an import map that specifies the aliases for your module paths. Here's what you need to do: 1. Create an import map file, for example import_map.json, at the root of your workspace with the following content:
{
"imports": {
"@mypackage/test/": "./test/mod.ts"
}
}
{
"imports": {
"@mypackage/test/": "./test/mod.ts"
}
}
2. Update your deno.jsonc file at the root of your workspace to include the import map:
{
"importMap": "./import_map.json"
}
{
"importMap": "./import_map.json"
}
3. Ensure that the Deno extension for Visual Studio Code is configured to use this import map. You can do this by setting the deno.importMap setting in your .vscode/settings.json file to point to your import map file:
{
"deno.enable": true,
"deno.importMap": "./import_map.json"
}
{
"deno.enable": true,
"deno.importMap": "./import_map.json"
}
Make sure that the path to the import_map.json file is correct and relative to the root of your workspace. After making these changes, Deno should recognize your import map configuration, and the error message about the import path should no longer appear. If you continue to experience issues, you may want to restart Visual Studio Code to
marvinh.
marvinh.•7mo ago
@Bloxs support for workspaces is under active development and not ready for prime time yet. CLI is mostly ready, but as you noticed the lsp powering the vscode extension is not