denoland / deno

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

triple slash references don't work in js files sometimes #22320

Open phaux opened 8 months ago

phaux commented 8 months ago

Looks like triple slash references to remote modules don't work when there's at least 2 of them in a project:

// @ts-check
/// <reference types="npm:@types/greasemonkey@4.0.7" />
/// <reference types="npm:exifreader@4.20.0" />

VSCode says:

File 'internal:///missing_dependency.d.ts' not found. deno-ts(6053)

deno check crashes:

error: Uncaught Error: Could not find constraint '@types/greasemonkey@4.0.7' in the list of packages.
    at ext:deno_tsc/99_main_compiler.js:654:32
    at Array.map (<anonymous>)
    at Object.resolveTypeReferenceDirectives (ext:deno_tsc/99_main_compiler.js:643:33)
    at actualResolveTypeReferenceDirectiveNamesWorker (ext:deno_tsc/00_typescript.js:119495:154)
    at resolveTypeReferenceDirectiveNamesWorker (ext:deno_tsc/00_typescript.js:119871:22)
    at resolveTypeReferenceDirectiveNamesReusingOldState (ext:deno_tsc/00_typescript.js:120033:16)
    at processTypeReferenceDirectives (ext:deno_tsc/00_typescript.js:121349:158)
    at findSourceFileWorker (ext:deno_tsc/00_typescript.js:121245:11)
    at findSourceFile (ext:deno_tsc/00_typescript.js:121115:22)
    at ext:deno_tsc/00_typescript.js:121064:24

Importing from esm.sh also doesn't work:

// @ts-check
/// <reference types="https://esm.sh/@types/greasemonkey@4.0.7" />
/// <reference types="https://esm.sh/preact@10.19.3" />

GM.xmlHttpRequest({ url: "/", method: "GET" });
globalThis.preact.h("div", {});

Now the last one only gets types and the rest are ignored:

Check file:///home/lis/Dropbox/userscripts/test.js
error: TS2304 [ERROR]: Cannot find name 'GM'.
GM.xmlHttpRequest({ url: "/", method: "GET" });
~~
    at file:///home/lis/Dropbox/userscripts/test.js:5:1

This only happens in js files. ts works fine.

Version: Deno 1.40.3

dsherret commented 4 months ago

Do you have a lockfile? I think it's corrupt. Maybe try deleting it.

Sorry, the error message is not very good here (I ran into it while debugging some stuff just now and searched the issue tracker)

phaux commented 4 months ago

It happens regardless if there's a lock file.

I found a workaround:

if (false) {
  import("npm:@types/greasemonkey@4.0.7");
  import("npm:exifreader@4.20.0");
}
lucacasonato commented 3 months ago

You can not have more than one triple slash reference comment in a JS file. Only one is supported, and it's types will be used as the types of the JS file.

lucacasonato commented 3 months ago

Ref https://github.com/denoland/deno-docs/pull/502

phaux commented 3 months ago

That's not just a docs issue. The error message could be better and it shouldn't crash Deno.