deno: Compile error when code contains export declaration-like string

  • v0.20.0
  • Parser doesn’t find declarations correctly?

[EDITED]

Previous example has unrelated mistake but I don’ know which code can reproduce error I met.

I met this error when I ran -> https://raw.githubusercontent.com/keroxp/dink/v0.5.1/main.ts That can be compiled with deno@v0.19.0. But got error below with v0.20.0:

error: Uncaught ImportPrefixMissing: relative import path "${resp.url}" not prefixed with / or ./ or ../
► $deno$/dispatch_json.ts:40:11
    at DenoError ($deno$/errors.ts:20:5)
    at unwrapResponse ($deno$/dispatch_json.ts:40:11)
    at sendAsync ($deno$/dispatch_json.ts:85:10)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (23 by maintainers)

Most upvoted comments

Hmm, there doesn’t seem to be a problem? ${deno} in a template literal gets substituted with the value assigned to variable deno, which you haven’t declared.

Yeah, it is a TypeScript issue with preProcessFile. Funnily, it was already open and tagged as a bug, but the reverse. It seems string literals with a variable interpolation in them cause the parser to flip modes, not detecting import/export statements outside of string literals, and detecting them within string literals. I can’t see any easy way to fix it right now. The workaround might be the best thing until the bug is fixed.

Refs: microsoft/TypeScript#30878

Interesting bug @keroxp!

I looks like output from ts.preProcessFile() in cli/js/compiler.ts return this template string as an actual import.

// https://raw.githubusercontent.com/keroxp/dink/v0.5.1/main.ts
...
   let link = `export * from "${resp.url}";\n`;
    if (hasDefaultExport) {
      link += `import {default as dew} from "${resp.url}";\n`;
      link += `export default dew;\n`;
    }
    await Deno.mkdir(modDir, true);
...
$ deno_dev https://raw.githubusercontent.com/keroxp/dink/v0.5.1/main.ts
Compile file:///Users/biwanczuk/dev/dink/main.ts
preprocessed file imports [ 
{ fileName: "./vendor/https/deno.land/std/fs/path.ts", pos: 23, end: 62 }, 
{ fileName: "./vendor/https/deno.land/std/fs/mod.ts", pos: 86, end: 124 }, 
{ fileName: "./vendor/https/deno.land/std/flags/mod.ts", pos: 151, end: 192 }, 
{ fileName: "./vendor/https/deno.land/std/fmt/colors.ts", pos: 229, end: 271 }, 
{ fileName: "${resp.url}", pos: 3914, end: 3925 }, 
{ fileName: "${resp.url}", pos: 4005, end: 4016 } 
]
error: Uncaught ImportPrefixMissing: relative import path "${resp.url}" not prefixed with / or ./ or ../
► $deno$/dispatch_json.ts:40:11
    at DenoError ($deno$/errors.ts:20:5)
    at unwrapResponse ($deno$/dispatch_json.ts:40:11)
    at sendAsync ($deno$/dispatch_json.ts:85:10)

Calling @kitsonk for help

EDIT: IMHO this is a TypeScript bug - it shouldn’t treat imports inside template string as an actual import in the file.

@keroxp a quick workaround for now would be to replace template strings and just add strings old-school (str1 + str2).

@keroxp I can take a look later, can you provide repro commands?

EDIT: Nvm, running deno https://raw.githubusercontent.com/keroxp/dink/v0.5.1/main.ts gives this error. I’ll investigate it 😃