Skip to content

oxlint,oxfmt: Support vite.config.ts only when its usage is explicitly indicated #20416

@leaysgur

Description

@leaysgur

To support Vite+, we recently added vite.config.ts support in addition to our config files.

However, this change has caused Oxlint and Oxfmt to throw errors under specific conditions, even in projects that aren't using Vite+.

At least, I want to avoid affecting existing Oxapps users.

What's happening

Oxfmt CLI + LSP, and Oxlint LSP try to auto discover root config files.

First, It starts searching from the cwd, then find-up.
The priority order is: the respective config files (such as .oxfmtrc.json) and then vite.config.ts.

And for loading .ts config file, we are using bare dynamic import().

For example, situation like this:

- .oxlintrc.json
- vite.config.ts
- packages/
  - proj-1
    - vite.config.ts

CLI run from root should work, which successfully find our config first from root.

However, if CLI or LSP is used from proj-1 as cwd, it finds vite.config.ts and tries to load it.

This is where the problem lies.

The problem happens when vite.config.ts is written with the assumption that it will be loaded via Vite. Vite bundles configuration files by default, so it may NOT be compatible with native import().

For actual usecases, please see also voidzero-dev/vite-plus#930.

The CLI immediately reports an error and exits, but the LSP falls back to the default and continues running.

Proposal

This is what I've done in #20417.

  • Check env var VITE_PLUS_VERSION exists
  • If true:
    • Only vite.config.ts will be used as a config file
    • Existing config files (.oxlintrc.json, oxlint.config.ts, etc) will NOT be referenced
  • else:
    • Only existing config files (.oxlintrc.json, oxlint.config.ts, etc) will be used
    • The same as it was before implementing vite.config.ts support

Since Vite+ ensures the entry points for both CLI (vp lint|fmt) and LSP (--lsp bin wrapper), this is possible.

PR: voidzero-dev/vite-plus#946

Why environment variables instead of CLI flags:
  • CLI flags are specifically for the CLI
  • For LSP, the relevant are the settings sent per workspace in the initialization request
  • Env vars can be easily read from the JS as well
Why not just append `vite.config.ts`:
  • With Vite+, there is no motivation to combine our config files
  • And also using Oxlint or Oxfmt directly is not encouraged
  • By this flag, we may be able to switch loading function
    • What if import("vite-plus/config").then(({ loadConfig }) => loadConfig(path, "lint")?

We should address import() cannot cover all vite.config.ts issue separately for Vite+ though, but this helps existing Oxapps users.

I think vite-plus side could provide a function for loading the configs.

Workaround

In the meantime, here's what you can do:

  • Rollback to the version before vite.config.ts support was added
    • oxlint@1.52 and oxfmt@0.37
  • or Always use -c, --config to disable auto discovery
    • For LSP, set oxc.configPath and oxc.fmt.configPath
  • or Place Oxapps config next to vite.config.ts
    • and re-export root config
    • Please note that relative path related options should be updated
  • or Update your vite.config.ts to be compatible with --configLoader=native

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Priority

    None yet

    Effort

    None yet

    Start date

    None yet

    Target date

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions