pzmosquito / eslint-import-resolver-vite

Vite module resolution plugin for eslint-plugin-import.
MIT License
16 stars 4 forks source link

No errors are thrown if the config can't be required #5

Closed FezVrasta closed 1 year ago

FezVrasta commented 1 year ago

If the configuration has errors inside, the plugin silently fails.

The same is valid if the config is ESM rather than CJS

pzmosquito commented 1 year ago

If the config has error, it should be caught before reaching this plugin. If the alias cannot be resolved, it will throw a path not found error. Can you share an example where the error was not caught by Vite and caused silent failure of this plugin?

FezVrasta commented 1 year ago

I import some file from the config with a relative path, but when the same config is imported by this plugin the import didn't work because the execution root was different. This happened because eslint is ran from the repository root, while vite runs from one of the monorepo package directories.

The other case was my ESM config export default { ... } failing silently because this module expects module.exports = { default: { ... } }

pzmosquito commented 1 year ago

I import some file from the config with a relative path, but when the same config is imported by this plugin the import didn't work because the execution root was different. This happened because eslint is ran from the repository root, while vite runs from one of the monorepo package directories.

This plugin is just a resolver for eslint-plugin-import. It should be the eslint-plugin-import throwing the error, not this plugin.

The other case was my ESM config export default { ... } failing silently because this module expects module.exports = { default: { ... } }

All my projects use Vite config in ESM. In fact, I assume most Vite configs should be written in ESM since Vite's official docs are all using ESM. If yours doesn't work, can you share your config file?

FezVrasta commented 1 year ago

This plugin is just a resolver for eslint-plugin-import. It should be the eslint-plugin-import throwing the error, not this plugin.

For some reason your module is swallowing the errors, if I added a try..catch around the require statement and made it throw the error, everything worked as intended, I got the error exposed.

All my projects use Vite config in ESM. In fact, I assume most Vite configs should be written in ESM since Vite's official docs are all using ESM. If yours doesn't work, can you share your config file?

Unfortunately I can't share it, but the error I got was that export could only be used on ES modules, which is the same error you get if you try to require an ES module in Node without having the package marked as module.

pzmosquito commented 1 year ago

For some reason your module is swallowing the errors, if I added a try..catch around the require statement and made it throw the error, everything worked as intended, I got the error exposed.

This plugin already has try catch on the entire logic (see here). if the error is swallowed, it's eslint-plugin-import (or something else) not spitting it out.

Unfortunately I can't share it, but the error I got was that export could only be used on ES modules, which is the same error you get if you try to require an ES module in Node without having the package marked as module.

I'll see what I can do to reproduce it, but no guarantee since I can't see the actual code that produced the error.

juliovedovatto commented 1 year ago

I faced the same situation when I tried to use this package to handle my aliases.

Basically, the plugin fails due ESLint nature. when importing vite.config.js

> DEBUG="eslint-plugin-import:resolver:vite" npm run lint

 eslint-plugin-import:resolver:vite Path not found: /home/julio-vedovatto/Projects/my-vite-app/vite.config.js:1
(function (exports, require, module, __filename, __dirname) { import { fileURLToPath, URL } from 'node:url';
                                                              ^^^^^^

SyntaxError: Cannot use import statement outside a module
    at new Script (node:vm:100:7)
    at NativeCompileCache._moduleCompile (/home/julio-vedovatto/Projects/my-vite-app/node_modules/v8-compile-cache/v8-compile-cache.js:240:18)
    at Module._compile (/home/julio-vedovatto/Projects/my-vite-app/node_modules/v8-compile-cache/v8-compile-cache.js:184:36)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)
    at Module.load (node:internal/modules/cjs/loader:1043:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1067:19)
    at require (/home/julio-vedovatto/Projects/my-vite-app/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.exports.resolve (/home/julio-vedovatto/Projects/my-vite-app/node_modules/eslint-import-resolver-vite/index.js:39:32)
    at v2 (/home/julio-vedovatto/Projects/my-vite-app/node_modules/eslint-module-utils/resolve.js:116:23) +0ms
  eslint-plugin-import:resolver:vite resolving: ./plugins/auth +4ms
  eslint-plugin-import:resolver:vite in file: /home/julio-vedovatto/Projects/my-vite-app/src/App.vue +0ms
  eslint-plugin-import:resolver:vite Path not found: /home/julio-vedovatto/Projects/my-vite-app/vite.config.js:1
(function (exports, require, module, __filename, __dirname) { import { fileURLToPath, URL } from 'node:url';

My assumption is: vite config is ESM and ESLint is CJS.

@pzmosquito wouldyou mind to share how you were able to use your package w/ vite? I tried to find examples, but no success. I can prepare a reproduction repo case you find more useful.

In the meanwhile, I'll switch back to eslint-import-resolver-alias

pzmosquito commented 1 year ago

@pzmosquito wouldyou mind to share how you were able to use your package w/ vite? I tried to find examples, but no success. I can prepare a reproduction repo case you find more useful.

here's my config:

//
// vite config file
//
import path from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import eslintPlugin from "vite-plugin-eslint";

const mode = process.env.APP_ENV === "dev" ? "development" : "production";

console.log(`Vite is compiling '${process.env.APP_ENV}' env in '${mode}' mode.`);

export const viteConfig = defineConfig({
    mode,
    resolve: {
        alias: {
            _: path.resolve(__dirname, "src"),
        },
    },
    build: {
        sourcemap: true,
    },
    plugins: [
        react(),
    ],
    clearScreen: false,
    server: {
        host: "0.0.0.0",
        port: 3000,
        strictPort: true,
        hmr: {
            clientPort:443,
        },
    },
});

export default ({ command }) => {
    if (command === "serve") {
        viteConfig.plugins.push(eslintPlugin({ cache: false }));
    }
    return viteConfig;
};

//
// eslintrc config
//
module.exports = {
    parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        ecmaFeatures: {
            jsx: true,
        },
    },
    env: {
        browser: true,
        es6: true,
    },
    settings: {
        "import/resolver": {
            vite: {
                namedExport: "viteConfig",
            },
        },
    },
};
pzmosquito commented 1 year ago

close due to inactivity. Please feel free to reopen if needed.