denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
95.7k stars 5.3k forks source link

Bug: types of transient npm deps not working #26055

Open marvinhagemeister opened 1 week ago

marvinhagemeister commented 1 week ago

Reported on discord: https://discord.com/channels/684898665143206084/1292952503213883534/1292952503213883534

if i'm importing something from npm with an npm: specifier (i.e. npm:vite), if it has type definitions that reference another npm package (i.e. esbuild), is it a deno issue or a package issue if the types aren't resolved by deno (at least, the deno vscode extension)?

tried with both npm:vite@5.4.8 as well as npm:vite@6.0.0-beta.2 and both of them don't load the types of esbuild when importing.

i don't have this issue when importing it from esm.sh, though. and if i manually add esbuild to the imports in my deno.json, it works too. but not if i directly import from npm:vite.

if this is to be expected then that's all good, just was wondering if i should report an issue or anything 🙂

actually, it correctly resolves the type (in the vscode extension?) up until i try to look at the types for vite.

not sure if it's just flaky, but it seems to only work (until i navigate to the types for vite) if i tell deno to generate a lock file and i have run deno clean? i think? e.g.: deno.json:

{
  "lock": true,
  "imports": {
      "vite": "npm:vite@^6.0.0-beta.2",
      "esbuild": "npm:esbuild@^0.24.0",
      "postcss": "npm:postcss@^8.4.47",
      "rollup": "npm:rollup@^4.22.5"
  }
}

file.ts

import { build } from 'vite';

await build({
  // stuff
  esbuild: {
    loader: 'tsx', // error is here, but only after i try to look at the types for vite in vscode.
  },
});
AllySummers commented 1 week ago

Various situations/notes I found. Apologies for the length

no deno.json/package.json - fully works (even after looking at vite types)

mod.ts:

import { build } from 'npm:vite@6.0.0-beta.2';

build({
    esbuild: {
        loader: 'tsx',
    },
});

cached/resolvable module, but an empty deno.json - doesn't resolve type at all

deno.json:

{}

mod.ts:

import { build } from 'npm:vite@6.0.0-beta.2';

build({
    esbuild: {
        loader: 'tsx', // Object literal may only specify known properties, and 'loader' does not exist in type 'ESBuildOptions'.deno-ts(2353)
    },
});

deno.json with vite as a dependency - works until you look at the types for vite

deno.json:

{
    "imports": {
        "vite": "npm:vite@6.0.0-beta.2"
    }
}

mod.ts:

import { build } from 'vite';

build({
    esbuild: {
        // only **after** looking at types for vite do i get this error
        loader: 'tsx', // Object literal may only specify known properties, and 'loader' does not exist in type 'ESBuildOptions'.deno-ts(2353)
    },
});

deno.json with vite as a dependency, but using full specifier - fully works (even after looking at vite types)

deno.json:

{
    "imports": {
        "vite": "npm:vite@6.0.0-beta.2"
    }
}

mod.ts:

import { build } from 'npm:vite@6.0.0-beta.2';

build({
    esbuild: {
        loader: 'tsx',
    },
});

deno.json with vite and esbuild as a dependency - works until you look at the types for vite

deno.json:

{
    "imports": {
        "vite": "npm:vite@6.0.0-beta.2",
        "esbuild": "npm:esbuild@0.24.0"
    }
}

mod.ts:

import { build } from 'vite';

build({
    esbuild: {
        // only **after** looking at types for vite do i get this error
        loader: 'tsx', // Object literal may only specify known properties, and 'loader' does not exist in type 'ESBuildOptions'.deno-ts(2353)
    },
});

package.json with only vite as a dependency, and no deno.json, installed with no flags for deno install - fully works (even after looking at vite types)

deno.json:

`package.json`: ```json { "dependencies": { "vite": "6.0.0-beta.2" } } ``` `mod.ts`: ```ts import { build } from 'vite'; build({ esbuild: { loader: 'tsx', }, }); ``` ### `package.json` with only vite as a dependency, and no `deno.json`, installed with `deno install --no-lock --node-modules-dir=none` - doesn't resolve `esbuild` types at all `deno.json`: `package.json`: ```json { "dependencies": { "vite": "6.0.0-beta.2" } } ``` `mod.ts`: ```ts import { build } from 'vite'; build({ esbuild: { loader: 'tsx', // Object literal may only specify known properties, and 'loader' does not exist in type 'ESBuildOptions'.deno-ts(2353) }, }); ``` ### `package.json` with only vite as a dependency, and `deno.json` with node modules dir disabled - works _until_ you look at `vite` types `deno.json`: ```jsonc { "nodeModulesDir": "none", "lock": false // same results if `true` } ``` `package.json`: ```json { "dependencies": { "vite": "6.0.0-beta.2" } } ``` `mod.ts`: ```ts import { build } from 'vite'; build({ esbuild: { // only **after** looking at types for vite do i get this error loader: 'tsx', // Object literal may only specify known properties, and 'loader' does not exist in type 'ESBuildOptions'.deno-ts(2353) }, }); ``` ### `package.json` with only vite as a dependency, and `deno.json` with node modules dir manual/auto - fully works (even after looking at vite types) `deno.json`: ```jsonc { "nodeModulesDir": "auto", // same for `"manual"` "lock": false // same results if `true` } ``` `package.json`: ```json { "dependencies": { "vite": "6.0.0-beta.2" } } ``` `mod.ts`: ```ts import { build } from 'vite'; build({ esbuild: { loader: 'tsx', }, }); ```
nayeemrmn commented 6 days ago

This has to do with scope attribution of open documents. It's kind of fixed by #24548 (which I'd been struggling to get the test to work for on CI) as long as the types aren't open on startup.