Skip to content

Throw error when specified configFile doesn't exist #5104

@AlexRMU

Description

@AlexRMU

Clear and concise description of the problem

When using @unocss/svelte-scoped (both Vite plugin and preprocessor), users can specify a custom path to the UnoCSS config file using the configFile option. However, if the specified file doesn't exist, the plugin silently falls back to the default configuration without any warning or error.

This silent fallback can lead to confusing situations where users expect their custom configuration to be applied but instead get default behavior, making debugging unnecessarily difficult.

Current Behavior

// vite.config.ts
import UnoCSS from "@unocss/svelte-scoped/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    UnoCSS({
      configFile: "./uno.config.custom.ts", // File doesn't exist
      // Other options...
    }),
    sveltekit(),
  ],
});

Result: Plugin continues to work with default configuration, no error is thrown.

Expected Behavior

When a custom configFile path is explicitly provided but the file doesn't exist, the plugin should throw a clear error message, such as:

Error: UnoCSS config file not found: ./uno.config.custom.ts
Please check the path or remove the configFile option to use the default config location.

Why This Matters

  1. Explicit configuration should be validated - When users explicitly specify a config file path, they have a clear intent. Silent fallback contradicts this intent.

  2. Faster debugging - Clear error messages save developers time by immediately identifying the problem rather than leaving them wondering why their configuration isn't being applied.

  3. Consistent with common tooling patterns - Most build tools (Vite, Webpack, Rollup, etc.) throw errors when explicitly specified config files are not found.

  4. Prevents silent failures in CI/CD - In automated environments, missing config files should fail loudly rather than deploying with unexpected default configuration.

Suggested solution

Add file existence validation when configFile option is provided:

  • If configFile is specified and the file doesn't exist → throw an error
  • If configFile is not specified → continue with current default config resolution behavior

This validation should apply to both:

  • @unocss/svelte-scoped/vite (Vite plugin)
  • @unocss/svelte-scoped/preprocess (Svelte preprocessor)

Comparison with Other UnoCSS Integrations

For consistency, it's worth noting that the main unocss/vite plugin follows the standard pattern of config file resolution but doesn't have this specific issue because it uses UnoCSS's core config loading mechanism differently. However, the principle of failing fast when explicitly provided configs are missing is widely adopted across the ecosystem.

Alternative

No response

Additional context

This issue is specifically about explicit user configuration. The default config file resolution (when configFile is not specified) should continue to work as it does currently - looking for uno.config.{ts,js,mjs} in standard locations and gracefully using defaults if none are found.

Edge Cases to Consider

  1. Typos in path - Most common cause of missing files
  2. Relative vs absolute paths - Error message should show the resolved absolute path
  3. Build vs dev mode - Validation should work consistently in both modes

Minimal Reproduction

# Create a SvelteKit project
npm create svelte@latest my-app
cd my-app

# Install dependencies
npm install @unocss/svelte-scoped

# Configure with non-existent file
# vite.config.ts - specify configFile: './non-existent.ts'

# Run dev server
npm run dev

# Expected: Clear error about missing config file
# Actual: Works with default config, no indication of problem

Environment:

  • Package: @unocss/svelte-scoped
  • Versions: All current versions (tested with latest)
  • Applies to: Both Vite plugin and preprocessor modes
  • Frameworks: SvelteKit, Svelte with Vite, Svelte with svelte-package

Validations

  • Read the Contributing Guidelines.
  • Read the README.md of using the package.
  • Already used the Interactive Docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions