vscode_deno icon indicating copy to clipboard operation
vscode_deno copied to clipboard

Alias & relative imports with importmap: requires prefix

Open Pictor13 opened this issue 2 years ago • 1 comments

I'm getting crazy with import_map.json, bare-specifiers and aliases.
I manage to make the import-map work correctly only for remote URLs, but not for aliases in order to manage relative paths.

The purpose is to define prefixes/aliases so that I do not have to track changes to relative import paths, if I move files around directories.

Hence my map is:

{
    "imports": {
      "/": "./",                  // all absolute imports relative to import-map
      "./": "./",                 // all relative imports relative to import-map
      "domain": "./_domain/",
      "app/": "./_app/",
      "infra/": "./_app/infrastructure/",
      "db/": "infra/adapter/database/"
    }
  }

I notice several problems here:

  • if I do an import from "/some/path/file.ts", Deno insist reading the / as Operative-System root (on Ubuntu); even though I aliased / with ./ (import-map folder)
  • if I do an import from "app/some/dependency.ts", Deno tells me that

    Relative import path "app/some/dependency.ts" not prefixed with / or ./ or ../ (resolve error)

I'm in a loop, cause I want to define aliases to not specify full paths:

  • but Deno wants me to prefix them with /
  • but at the same time it ignores the / remapping nad insists reading them from the system root folder.
  • the Deno Runtime and VSCode Deno extension seem to have some differences in how they read import statements.
    My VSCode was showing no problems anymore in the whole codebase; but when running deno run on the code I'm getting a lot of broken imports (with the problems above) and all these inconsistencies in interpreting the import-map.

How to fix my import setup ❓

The documentation online is so limited on the topic. The examples shown in most of the Q&A on stack-overflow or other resources are always a bout very basic use cases, they mostly are copy-pastes from the official docue, and they do not go deep into explaining how the import-maps work.

Also I think it is pretty unclear and undocumented whether aliases can be reused inside other aliases/specifiers.
For example I'm not sure if the last specifier "db/": "infra/adapter/database/" is legit or if it impossible to do so. ❓ Also, probably this would have consequences, when remapping /, given that the slash is used in all the import-map paths as a separator. ❓

I'm obviously misunderstanding something; please help me to figure out what. Otherwise I suspect there is a bug; but I guess there would already be lot of issues open if that would be the case (I searched other issues, and couldn't find a solution).


OS: Ubuntu 20.04
Deno:

$ deno --version
deno 1.34.1 (release, x86_64-unknown-linux-gnu)
v8 11.5.150.2
typescript 5.0.4

VSCode: 1.79.2
VSCode extension: latest version

Pictor13 avatar Jun 26 '23 21:06 Pictor13

I also ran into this, it's quite an irritating bug. Using Deno vscode in a monorepo is painful/impossible but unfortunately it looks like issues for the common repo configurations haven't seen any movement in months, they got shifted from q3 plan to q4 plan.

evelant avatar Nov 07 '23 17:11 evelant

Seems like the import_map.json wasn't included in configuration. Rename it to deno.json, it should be detected now even if not in the root directory.

nayeemrmn avatar Jun 19 '24 17:06 nayeemrmn

Pictor13 wrote:

I manage to make the import-map work correctly only for remote URLs, but not for aliases in order to manage relative paths.

As written, the import-map is detected; it's the content that isn't interpreted as expected.

[..] import_map.json [..] Rename it to deno.json [..]

But... they are different files, aren't they? One is Deno config and the other is the Import config.
Also deno.json isn't a mandatory file (at least, it wasn't in June'23; I didn't touch Deno since about 6 months)

Pictor13 avatar Jun 21 '24 16:06 Pictor13