AtomLinter / linter-eslint-node

ESLint plugin for Atom/Pulsar Linter (v8 and above)
https://web.pulsar-edit.dev/packages/linter-eslint-node
MIT License
4 stars 3 forks source link

Question: Error: Cannot find module '/package.json' #13

Closed brettz9 closed 2 years ago

brettz9 commented 2 years ago

Hi,

Very excited to have this tool available. Thanks so much!

Issue Type

Question

Issue Description

I don't know if you'd consider this a bug, but wondering if you might know how to resolve, being more familiar with the Atom environment.

In my linting config (eslint-config-ash-nazg), I dynamically detect the ecmaVersion and env for my .eslintrc.cjs based on the minimum version of the node subfield of package.json engines so as to avoid the need for manually setting them.

With this code, I was using process.cwd() (process.cwd() helps the code work from the command line) to try to find the project root (and then package.json within that), but the base path it returns is /. I then tried the following:

typeof atom === 'undefined'
              ? process.cwd()
              : (atom?.workspace?.getCenter()?.paneContainer?.activePane?.activeItem?.getDirectoryPath() || process.cwd())

But the path is apparently not being found (same result if I remove the optional chaining operators). This code, even with the atom-specific code, appears to still be returning /.

Any idea if it is possible for me to detect (or for you to set any properties I can detect) the root directory of the current file's project?

I am using pnpm, which can theoretically change path discovery, but I wouldn't think that would affect Atom's discovery.

Thanks!

Bug Checklist

Linter Eslint Node: Debug output here

Cannot read config file: /Users/brett/eslint-config-ash-nazg/rc.js Error: Cannot find module '/package.json' Require stack:

/Users/brett/eslint-config-ash-nazg/rc.js
/Users/brett/jsdoccomment/node_modules/.pnpm/@eslint+eslintrc@[1]().2.1/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.[0]()/node_modules/eslint/lib/cli-engine/cli-engine.js
[/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0](mailto:/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0)/node_modules/eslint/lib/eslint/eslint.js
[/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0](mailto:/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0)/node_modules/eslint/lib/eslint/index.js
[/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0](mailto:/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0)/node_modules/eslint/lib/api.js
[/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0](mailto:/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0)/node_modules/eslint/lib/api.js Referenced from: /Users/brett/eslint-config-ash-nazg/sauron-node-overrides.js
linter-eslint-node debug information

Atom version: 1.60.0
linter-eslint-node version: 1.0.3
Worker using Node at path: /Users/brett/.nvm/versions/node/v14.17.1/bin/node
Worker Node version: v14.17.1
Worker PID: 81288
ESLint version: 8.11.0
ESLint location: [/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0](mailto:/Users/brett/jsdoccomment/node_modules/.pnpm/eslint@8.11.0)/node_modules/eslint/
Linting in this project performed by: linter-eslint-node
Hours since last Atom restart: 0.1
Platform: darwin
Current file's scopes: [
  "source.js"
]
linter-eslint-node configuration: {
  "disabling": {
    "disableWhenNoEslintConfig": false,
    "rulesToSilenceWhileTyping": []
  },
  "scopes": [
    "source.js",
    "source.jsx",
    "source.js.jsx",
    "source.flow",
    "source.babel",
    "source.js-semantic",
    "source.ts"
  ],
  "nodeBin": "node",
  "warnAboutOldEslint": true,
  "autofix": {
    "fixOnSave": false,
    "rulesToDisableWhileFixing": [],
    "ignoreFixableRulesWhileTyping": false
  },
  "advanced": {
    "disableEslintIgnore": false,
    "showRuleIdInMessage": true,
    "useCache": true
  }
}
brettz9 commented 2 years ago

For some reason, GitHub isn't letting me fake a fork now to offer a PR, but FWIW, I've resolved the problem thus:

In /lib/worker.js of your project, I added on line 178:

    process.chdir(resolveDir);

I don't know if this could create any side effects for Atom but seems to be working ok.

It's also ok if I set the following on line 334 before the getESLint call:

  let oldCwd = process.cwd();

and then reset it back after the linting calls, e.g., line 398:

  process.chdir(oldCwd);
brettz9 commented 2 years ago

I do get this error one time on start up, but apparently unrelated:

Failed to load plugin 'import' declared in '../.eslintrc': Cannot find module 'eslint-plugin-import' Require stack:

/Users/brett/.atom/packages/linter-eslint-node/placeholder.js
brettz9 commented 2 years ago

Sorry, it appears that only appears with the /lib/worker.js file open. My own regular files work fine though with my process.cwd injection.

UziTech commented 2 years ago

If you want to create a PR with your fix I would be happy to review it 😁👍

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.0.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket: