Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading custom formatter via file (not package) #941

Open
takenspc opened this issue Nov 18, 2022 · 1 comment
Open

Loading custom formatter via file (not package) #941

takenspc opened this issue Nov 18, 2022 · 1 comment
Labels
Status: Need More Info Lacks enough info to make progress

Comments

@takenspc
Copy link
Contributor

What version of textlint are you using?

12.2.3

What file type (Markdown, plain text, etc.) are you using?

Markdown

What did you do? Please include the actual source code causing the issue.

It is convenient that textlint can load custom formatter via file.

textlint -f custom-formatter.js **/*.md

Source Code: https://github.com/takenspc/junit-reports/tree/textlint

What did you expect to happen?

texlint loads the formatter.

What actually happened? Please include the actual, raw output from textlint.

textlint failed to load the formatter.

Log of GitHub Actions: https://github.com/takenspc/junit-reports/actions/runs/3493635216/jobs/5848666329

Error
Could not find formatter custom-formatter.js
Error: Cannot find module 'custom-formatter.js'
Require stack:
- /home/runner/work/junit-reports/junit-reports/node_modules/@textlint/linter-formatter/lib/src/index.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/options.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/cli.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/bin/textlint.js

Stack trace
Error: Could not find formatter custom-formatter.js
Error: Cannot find module 'custom-formatter.js'
Require stack:
- /home/runner/work/junit-reports/junit-reports/node_modules/@textlint/linter-formatter/lib/src/index.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/options.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/cli.js
- /home/runner/work/junit-reports/junit-reports/node_modules/textlint/bin/textlint.js
    at createFormatter (/home/runner/work/junit-reports/junit-reports/node_modules/@textlint/linter-formatter/lib/src/index.js:40:15)
    at TextLintEngine.onFormat (/home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/textlint-engine.js:48:59)
    at TextLintEngine.formatResults (/home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/engine/textlint-engine-core.js:239:32)
    at /home/runner/work/junit-reports/junit-reports/node_modules/textlint/lib/src/cli.js:221:39

Related codes

if (fs.existsSync(formatterName)) {

if (fs.existsSync(formatterName)) {
  formatterPath = formatterName;
}
// snip
try {
  formatter = moduleInterop(require(formatterPath));
} catch (ex) {
  throw new Error(`Could not find formatter ${formatterName}
${ex}`);
}

In most cases, fs.existsSync resolves relative paths using process.cwd() while require() resolves relative paths using __dirname.

That is, existence of a relative path resolved using process.cwd() does not guarantee the relative path can be required.

One of possible solutions is changing

if (fs.existsSync(formatterName)) {
  formatterPath = formatterName;

to

if (fs.existsSync(path.resolve(__dirname, formatterName))) {
  formatterPath = path.resolve(__dirname, formatterName);
@azu
Copy link
Member

azu commented Nov 18, 2022

Can you try next?

textlint -f ./custom-formatter.js **/*.md

📝 custom-formatter.js is valid npm module name.

@azu azu added the Status: Need More Info Lacks enough info to make progress label Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Need More Info Lacks enough info to make progress
Projects
None yet
Development

No branches or pull requests

2 participants