denoland / vscode_deno

Visual Studio Code plugin for Deno
https://marketplace.visualstudio.com/items?itemName=denoland.vscode-deno
MIT License
1.5k stars 146 forks source link

Relative import with ~/ triggers deno(import-prefix-missing) #1164

Closed birkskyum closed 2 months ago

birkskyum commented 2 months ago

deno 2.0.0-rc.3

This code executes just fine, but I get this language server squiggly line that I don't know if intentional deno opinion or accidental oversight.

The ~ for relative path, can in some cases be a rather convenient alternative to the ../../../../../ .

Screenshot 2024-09-19 at 14 15 27

Copied text:

Relative import path "~/components/ui/dialog" not prefixed with / or ./ or ../ hint: If you want to use a JSR or npm package, try running deno add jsr:~/components/ui/dialog or deno add npm:~/components/ui/dialogdeno(import-prefix-missing)

nayeemrmn commented 2 months ago

Do you have ~/ in an your deno.json import map? Can you share your config?

birkskyum commented 2 months ago

I don't have a deno.json, just a package.json

nayeemrmn commented 2 months ago

Are you executing it with Deno?

birkskyum commented 2 months ago

Yes, with deno 2 rc.3

nayeemrmn commented 2 months ago

I guess just add a deno.json like:

{
  "imports": {
    "~/": "./"
  }
}

Let me know if it works.

birkskyum commented 2 months ago

It changes the error to a no-local module loading issue. This is just a .tsx file with exports, not some package. The code still executes fine though, with / without the .tsx extension.

Screenshot 2024-09-19 at 18 08 57
nayeemrmn commented 2 months ago

Is that file URL in the error message the correct path? If not, you have to adjust the import map value accordingly, where it says "./".

birkskyum commented 2 months ago

Made a repro in here that gave some pointers to what's going on: https://github.com/birkskyum/squiggly

It's appear to be because the tsconfig.json compilerOptions.path isn't applied properly:

{
  "compilerOptions": {
    "paths": {
      "~/*": [
        "./src/*"
      ]
    }
  }
}
nayeemrmn commented 2 months ago

We don't read tsconfig.json, change the deno.json to:

{
  "imports": {
    "~/": "./src/"
  }
}
birkskyum commented 2 months ago

That works, thanks.

Okay, I didn't realize the tsconfig.json was completely ignored - that seems like an important thing to be aware of when working with deno.

I guess that's also a pointer to how this JSX issue appear - because I have config it in tsconfig that probably will have to be moved to deno.json.

birkskyum commented 2 months ago

@nayeemrmn , it's really not clear anywhere that tsconfig.json isn't read by deno, so if it's the case I think it's important to educate more on. See i.e. this article: https://deno.com/blog/intro-to-tsconfig#from-js-to-ts