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

Extension fails format on save even with fresh CRA TS project #155

Closed
jcphua opened this issue Mar 10, 2023 · 2 comments
Closed

Extension fails format on save even with fresh CRA TS project #155

jcphua opened this issue Mar 10, 2023 · 2 comments

Comments

@jcphua
Copy link

jcphua commented Mar 10, 2023

Describe the bug

Basic functionality of formatting on save does not work for TypeScript project. The immediate error seen in the Prettier Eslint output is:

Error: Failed to load plugin 'react' declared in 'CLIOptions': The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''

To Reproduce

  1. As a barebones and easily replicatable starting point, created a new create-react-app project with TypeScript as template (from macos CLI): npx create-react-app cra-typescript --template typescript.
  2. Followed the installation instructions from the vs-code-prettier-eslint extension README:
    a. npm i -D prettier eslint @typescript-eslint/parser
    b. Updated settings.json exactly according to instructions:
{
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
"editor.formatOnPaste": false, // required
"editor.formatOnType": false, // required
"editor.formatOnSave": true, // optional
"editor.formatOnSaveMode": "file", // required to format on save
"files.autoSave": "onFocusChange" // optional but recommended
}
  • c. My indicator for successful operation is just simply for correct indention of TypeScript source code.

    However even attempting a simple save of file src/App.tsx at this point (no change to the default generated boilerplate contents of this file), and also not yet beginning setup of prettier or eslint config files/properties), seemingly has no effect.

    The only indicator of a failure is when viewing VSCode Output pane (with Prettier Eslint output selected), you see the following:

Error: Failed to load plugin 'react' declared in 'CLIOptions': The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received '' 
TypeError: Failed to load plugin 'react' declared in 'CLIOptions': The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''
    at new NodeError (node:internal/errors:371:5)
    at createRequire (node:internal/modules/cjs/loader:1281:13)
    at Object.resolve (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/shared/relative-module-resolver.js:23:16)
    at Object.ModuleResolver.resolve (<folder>/cra-typescript/node_modules/@rushstack/eslint-patch/src/modern-module-resolution.ts:235:36)
    at ConfigArrayFactory._loadPlugin (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:1067:33)
    at ConfigArrayFactory._loadPlugin (<folder>/cra-typescript/node_modules/@rushstack/eslint-patch/src/modern-module-resolution.ts:243:35)
    at <folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:958:33
    at Array.reduce (<anonymous>)
    at ConfigArrayFactory._loadPlugins (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:954:22)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:754:44)
    at _normalizeObjectConfigDataBody.next (<anonymous>)
    at ConfigArrayFactory._normalizeObjectConfigData (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:694:20)
    at _normalizeObjectConfigData.next (<anonymous>)
    at ConfigArrayFactory.create (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/config-array-factory.js:489:16)
    at createCLIConfigArray (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/cascading-config-array-factory.js:168:47)
    at CascadingConfigArrayFactory.clearCache (<folder>/cra-typescript/node_modules/@eslint/eslintrc/lib/cascading-config-array-factory.js:339:32)
    at new ESLint (<folder>/cra-typescript/node_modules/eslint/lib/eslint/eslint.js:449:32)
    at Pge (~/.vscode/extensions/rvest.vs-code-prettier-eslint-5.0.4/dist/extension.js:180:69)
    at ~/.vscode/extensions/rvest.vs-code-prettier-eslint-5.0.4/dist/extension.js:188:700
    at Lge (~/.vscode/extensions/rvest.vs-code-prettier-eslint-5.0.4/dist/extension.js:180:1439)

Have performed the same preliminary steps as per above on a default CRA (non-TypeScript) project (via npx create-react-app cra-javascript) and can see successful formatting on save and no errors on output, so my local environment does not appear to be the issue.

Expected behavior

No errors in output pane, successful indentation/formatting on save of TypeScript file.

Example Project

  • Standard TypeScript CRA project (via npx create-react-app cra-typescript --template typescript)
    package.json
{
  "name": "cra-typescript",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.14",
    "@types/react": "^18.0.28",
    "@types/react-dom": "^18.0.11",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "typescript": "^4.9.5",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@typescript-eslint/parser": "^5.54.1",
    "eslint": "^8.35.0",
    "prettier": "^2.8.4"
  }
}

Versions (please complete the following information):

  • VS Code Prettier ESLint extension 5.0.4
  • Visual Studio Code: 1.76.1
  • Node: 16.17.0
  • Package Manager 8.19.2
  • prettier: 2.8.4
  • eslint: 8.35.0
  • create-react-app: 5.0.1

System Specifications (please complete the following information):

  • OS: macos Ventura 13.2.1
  • Processor: Apple M1
  • RAM Size: 16GB
@idahogurl
Copy link
Owner

idahogurl commented Mar 10, 2023

@jcphua This is an issue with @rushstack/eslint-patch. An issue has been created on their repository page. In the meantime, you can find the fix explained here.

The issue cannot be fixed by the extension since it's using local node_modules so the patch file needs to be created locally. I could perhaps have a command that can run to create the patched file locally. Would a FAQ in discussions for this error and other other common errors help? Or I could update the Troubleshooting area to include the error message and what to do about it.

@jcphua
Copy link
Author

jcphua commented Mar 17, 2023

@idahogurl Thanks for providing the link to the manual/local fix.

For visibility, and others facing the same issue maybe it's a good idea to mention it in FAQ/other documentation, but since you've supplied the details on the issue and fix, I'm happy for this issue to be closed.

Cheers!

@jcphua jcphua closed this as completed Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants