DavidAnson / vscode-markdownlint

Markdown linting and style checking for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint
MIT License
925 stars 168 forks source link

Cannot use ESM configuration file #349

Open northword opened 1 month ago

northword commented 1 month ago

Thanks for your great work! I encountered a problem in use:

I have a .markdownlint-cli2.mjs in the project root directory, but when I open the md file, the plugin reports an error:

[17:00:00] ERROR: Exception while linting with markdownlint-cli2:
Error: Unable to use configuration file 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'; Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
stack:
 at w (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9155)
 at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:13211
 at async Promise.all (index 0)
 at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
 at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
cause:
 AggregateError: Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
 stack:
  at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9718)
 errors:
  Error: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
  Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
  stack:
   Error [ERR_REQUIRE_ESM]: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
   Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
   at c._load (node:electron/js2c/node_init:2:13801)
   at E._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:177:6051)
   at i._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:174:31787)
   at o._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:139:34334)
   at g (d:\Program Files\Microsoft VS Code\resources\app\out\bootstrap-fork.js:2:647)
   at e.exports (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:20509)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9542)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
  TypeError: A dynamic import callback was not specified.
  stack:
   at importModuleDynamicallyCallback (node:internal/modules/esm/utils:228:9)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9661)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
[17:00:23] ERROR: Exception while linting with markdownlint-cli2:
Error: Unable to use configuration file 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'; Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
stack:
 at w (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9155)
 at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:13211
 at async Promise.all (index 0)
 at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
 at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
cause:
 AggregateError: Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
 stack:
  at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9718)
 errors:
  Error: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
  Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
  stack:
   Error [ERR_REQUIRE_ESM]: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
   Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
   at c._load (node:electron/js2c/node_init:2:13801)
   at E._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:177:6051)
   at i._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:174:31787)
   at o._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:139:34334)
   at g (d:\Program Files\Microsoft VS Code\resources\app\out\bootstrap-fork.js:2:647)
   at e.exports (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:20509)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9542)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
  TypeError: A dynamic import callback was not specified.
  stack:
   at importModuleDynamicallyCallback (node:internal/modules/esm/utils:228:9)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9661)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
[17:03:40] ERROR: Exception while linting with markdownlint-cli2:
Error: Unable to use configuration file 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'; Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
stack:
 at w (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9155)
 at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:13211
 at async Promise.all (index 0)
 at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
 at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
cause:
 AggregateError: Unable to require or import module 'd:/Code/zotero-chinese/wiki/.markdownlint-cli2.mjs'.
 stack:
  at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9718)
 errors:
  Error: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
  Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
  stack:
   Error [ERR_REQUIRE_ESM]: require() of ES Module d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs not supported.
   Instead change the require of d:\Code\zotero-chinese\wiki\.markdownlint-cli2.mjs to a dynamic import() which is available in all CommonJS modules.
   at c._load (node:electron/js2c/node_init:2:13801)
   at E._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:177:6051)
   at i._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:174:31787)
   at o._load (d:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\api\node\extensionHostProcess.js:139:34334)
   at g (d:\Program Files\Microsoft VS Code\resources\app\out\bootstrap-fork.js:2:647)
   at e.exports (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:20509)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9542)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)
  TypeError: A dynamic import callback was not specified.
  stack:
   at importModuleDynamicallyCallback (node:internal/modules/esm/utils:228:9)
   at L (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:9661)
   at c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:12977
   at async Promise.all (index 0)
   at async c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17067
   at async $ (c:\Users\northword\.vscode\extensions\davidanson.vscode-markdownlint-0.55.0\bundle.js:37:17002)

I'm sure my config file is correct because when I change the suffix to cjs and export default to module.exports, it works fine.

It also works fine when running markdownlint-cli2 directly.

It seems this is caused by the plugin being cjs and not being able to load esm's? (I'm not sure)

Below are my config files and package.json:

// @ts-check

const options = {
  // Disable some built-in rules
  config: {
    // 定义文档中涉及的专有名词
    MD044: {
      code_blocks: false,
      html_elements: false,
      names: [
        "Zotero",
        "JavaScript",
        "GitHub",
        "WebDAV",
        "Android",
        "iOS",
        "macOS",
        "WPS",
        "Word",
        "ZotFile",
        "Zotero Chinese",
      ],
    },

    "search-replace": {
      rules: [
        {
          name: "使用直角引号",
          message: "请使用直角引号而不是弯引号",
          searchPattern: "/“([^“”]+?)”/g",
          replace: "「$1」",
          searchScope: "text",
        },
      ],
    },

    // 兼容 Prettier
    // @see https://github.com/DavidAnson/markdownlint/blob/main/doc/Prettier.md
    "blanks-around-fences": false,
    "blanks-around-headings": false,
    "blanks-around-lists": false,
    "code-fence-style": false,
    "emphasis-style": false,
    "heading-start-left": false,
    "hr-style": false,
    "line-length": false,
    "list-indent": false,
    "list-marker-space": false,
    "no-blanks-blockquote": false,
    "no-hard-tabs": false,
    "no-missing-space-atx": false,
    "no-missing-space-closed-atx": false,
    "no-multiple-blanks": false,
    "no-multiple-space-atx": false,
    "no-multiple-space-blockquote": false,
    "no-multiple-space-closed-atx": false,
    "no-trailing-spaces": false,
    "ol-prefix": false,
    "strong-style": false,
    "ul-indent": false,

    // 其他规则配置
    // @see https://github.com/vuepress-theme-hope/vuepress-theme-hope/blob/main/.markdownlint.json
    default: true,
    MD003: {
      style: "atx",
    },
    MD004: {
      style: "dash",
    },
    MD013: false,
    MD024: {
      siblings_only: true,
    },
    MD025: {
      front_matter_title: "",
    },
    MD033: {
      allowed_elements: [
        "br",
        "template",
        "script",
        "style",
        "ArtPlayer",
        "AudioPlayer",
        "AutoCatalog",
        "Badge",
        "BiliBili",
        "Catalog",
        "CodePen",
        "DemoProject",
        "FontIcon",
        "ProjectLink",
        "PDF",
        "Replit",
        "Share",
        "SiteInfo",
        "StackBlitz",
        "XiGua",
        "VideoPlayer",
        "YouTube",
        "AppearanceSwitch",
        "HopeIcon",
        "FlowChartPlayground",
        "IconDisplay",
        "KatexPlayground",
        "NetlifyBadge",
        "PrintButton",
        "ThemeColorPicker",
        "ToggleFullScreenButton",
        "ToggleRTLButton",
      ],
    },
    // 兼容 Prettier 禁用
    // "MD035": {
    //   "style": "---"
    // },
    MD036: false,
    MD040: false,
    MD046: false,
    MD049: false,
  },
  customRules: ["markdownlint-rule-search-replace"],

  // Ignore files referenced by .gitignore (only valid at root)
  gitignore: true,
};

export default options;
{
  "name": "zotero-guide-zh",
  "version": "0.0.0",
  "description": "Zotero 中文手册",
  "type": "module",
  "scripts": {
    "docs:dev": "vitepress dev --open",
    "docs:build": "vitepress build",
    "docs:preview": "vitepress preview",
    "lint:fix": "markdownlint-cli2 --fix **/*.md & prettier --write .",
    "lint:check": "markdownlint-cli2 **/*.md & prettier --check .",
    "git:sync-force": "bash scripts/sync-force.sh",
    "prepare": "husky"
  },
  "devDependencies": {
    "@mdit/plugin-footnote": "0.12.0",
    "@mdit/plugin-mark": "^0.12.0",
    "@types/node": "20.14.13",
    "husky": "9.1.4",
    "lint-staged": "15.2.7",
    "markdownlint-cli2": "0.13.0",
    "markdownlint-rule-search-replace": "^1.2.0",
    "prettier": "2.8.8",
    "vitepress": "1.3.1"
  },
  "prettier": {
    "tabWidth": 2,
    "printWidth": 80,
    "proseWrap": "preserve",
    "endOfLine": "lf"
  },
  "lint-staged": {
    "*.md": "prettier --write"
  }
}

I would appreciate it if you could help me see what the problem is!

Thank you very much again!

DavidAnson commented 1 month ago

Sorry, this is a VS Code limitation. I'd removed module support from the documentation long ago but added it back a release or two ago because it seemed to work after they made a change in this area. However, this has always worked at development time and I think I only checked that. This remains broken at run time and I have already made the commit to remove it from the documentation (again) for the next release: https://github.com/DavidAnson/vscode-markdownlint/commit/e96150c5274f17e9ad61af5e5a5ca80306d67e80

The original issue is #248 and includes a link to the relevant VS Code issue.