remarkjs / remark

markdown processor powered by plugins part of the @unifiedjs collective
https://remark.js.org
MIT License
7.66k stars 358 forks source link

remark-parse/lib/index.d.ts imports a module that cannot be referenced #1039

Closed yamachu closed 2 years ago

yamachu commented 2 years ago

Initial checklist

Affected packages and versions

remark-parse 10.0.1

Link to runnable example

No response

Steps to reproduce

https://github.com/yamachu/remark-types-broken-repro

  1. Create a typescript project with strict type checking ( like this https://github.com/yamachu/remark-types-broken-repro/blob/699e5a1ef6963c6f0c6d06d503b962543ffb8945/tsconfig.json )
  2. add dependencies unified 10.1.2 and remark-parse 10.0.1
  3. write code as below (code: REPRO)
  4. execute npx tsc --noEmit for running type check

REPRO

import { unified } from "unified";
import markdown from "remark-parse";

const processor = unified().use(markdown);

const _ = processor.parse("**This is Markdown**");

Expected behavior

Pass type check without error

Actual behavior

Fail type check with error

Runtime

Node v16

Package manager

yarn 1

OS

macOS

Build and bundle tools

Other (please specify in steps to reproduce)

JounQin commented 2 years ago

Can you paste some error logs?

yamachu commented 2 years ago

thanks @JounQin

here is error log

# ~/Projects/github.com/yamachu/remark-types-broken-repro $ [master]
 yarn run typecheck
yarn run v1.22.19
$ tsc --noEmit
node_modules/remark-parse/lib/index.d.ts:3:26 - error TS2307: Cannot find module 'mdast-util-from-markdown/lib' or its corresponding type declarations.

3   options: void | import('mdast-util-from-markdown/lib').Options | undefined
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Found 1 error in node_modules/remark-parse/lib/index.d.ts:3

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
JounQin commented 2 years ago

There is indeed a lib/index.d.ts for mdast-util-from-markdown, maybe you have different and incompatible mdast-util-from-markdown versions installed.

https://unpkg.com/browse/mdast-util-from-markdown@1.2.0/lib/index.d.ts

I'll check your reproduction soon.

JounQin commented 2 years ago

OK, it should be related to exports field from mdast-util-from-markdown.

yamachu commented 2 years ago

yes, it's related to exports.

https://github.com/syntax-tree/mdast-util-from-markdown/issues/29 This Issue seems reluctant to add to export.

yamachu commented 2 years ago

https://github.com/yamachu/remark/tree/fix-lib-types

With these changes, it worked in my environment. I have searched a bit for a compile option to see if it is possible to make the import path less deep, but have not found it yet...

remcohaszing commented 2 years ago

You are using the TypeScript compiler options "module": "node16" and "moduleResolution": "node16" (the latter is redundant if the former is specified). Those are the correct TypeScript options to use since TypeScript 4.7, but not all packages are using / supporting this yet, including remark apparently.

remark-parse is doing this correctly:

https://github.com/remarkjs/remark/blob/99179c6745a2173d07d5c3b46bad7df767e1989d/packages/remark-parse/lib/index.js#L3

However, TypeScript generates:

/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */
export default function remarkParse(options: void | import("mdast-util-from-markdown/lib").Options | undefined): void;
export type Root = import('mdast').Root;
export type Options = import('mdast-util-from-markdown').Options;

If we use "module": "node16", it’s even worse.

/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */
export default function remarkParse(options: void | import("../../../node_modules/mdast-util-from-markdown/lib/index.js").Options | undefined): void;
export type Root = import('mdast').Root;
export type Options = import('mdast-util-from-markdown').Options;

This appears to be a TypeScript bug (causing bugs in our output. This is definitely a bug in remark’s published packages).

wooorm commented 2 years ago

@remcohaszing I remember there was a similar issue somewhere, and that it was also reported (by you?) over at TypeScript?

remcohaszing commented 2 years ago

Nope, doesn't ring a bell.

wooorm commented 2 years ago

OK well I am not sure what to search for but I am pretty sure you commented on exactly this issue in one of the TypeScript repos and linked it

remcohaszing commented 2 years ago

@yamachu unfortunately the only workaround / solution I see so far is to use "skipLibCheck": true.

yamachu commented 2 years ago

related? https://github.com/microsoft/TypeScript/issues/38111

remcohaszing commented 2 years ago

I think that’s the issue causing this indeed. Hard to believe such a critical issue has been open for over 2 years :confused:

wooorm commented 2 years ago

Closing, see https://github.com/remarkjs/remark/pull/1040#issuecomment-1239597156

yamachu commented 2 years ago

I then solved this problem with the following settings.

tsconfig.json

    "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
    "paths": {
      "mdast-util-from-markdown/lib": [
        "node_modules/mdast-util-from-markdown/lib/index.d.ts"
      ]
    },