Skip to content

Relative CSS asset import does not work with root config set #18117

@Demivan

Description

@Demivan

Describe the bug

#17856 broke relative CSS asset imports from node_modules when root config is set.

const replaceUrl = await urlReplacer(dep.url, dep.loc.filePath)

dep.loc.filePath is used as importer, but that path is relative (../../node_modules/.pnpm/@fontsource) and causes the asset path to be resolved to a path outside the project.

Reproduction

https://github.com/Demivan/vite-css-asset-resolution-bug

Steps to reproduce

pnpm i
pnpm build

System Info

System:
    OS: Linux 6.10 NixOS 24.11 (Vicuna) 24.11 (Vicuna)
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 9.38 GB / 46.96 GB
    Container: Yes
    Shell: 3.7.1 - /run/current-system/sw/bin/fish
  Binaries:
    Node: 22.8.0 - ~/.nix-profile/bin/node
    npm: 10.8.2 - ~/.nix-profile/bin/npm
    pnpm: 9.10.0 - ~/.nix-profile/bin/pnpm
    bun: 1.1.20 - ~/.nix-profile/bin/bun
  npmPackages:
    vite: 5.4.1 => 5.4.1

Used Package Manager

pnpm

Logs

Click to expand!
> vite build "--debug"

  vite:config bundled config file loaded in 18.62ms +0ms
  vite:config using resolved config: {
  vite:config   root: '/home/demivan/projects/vite-bug/packages/app',
  vite:config   css: { transformer: 'lightningcss', lightningcss: { targets: [Object] } },
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     ssrEmitAssets: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     modulePreload: { polyfill: true },
  vite:config     cssMinify: true
  vite:config   },
  vite:config   configFile: '/home/demivan/projects/vite-bug/vite.config.ts',
  vite:config   configFileDependencies: [ '/home/demivan/projects/vite-bug/vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   base: '/',
  vite:config   decodedBase: '/',
  vite:config   rawBase: '/',
  vite:config   resolve: {
  vite:config     mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ],
  vite:config     conditions: [],
  vite:config     extensions: [
  vite:config       '.mjs',  '.js',
  vite:config       '.mts',  '.ts',
  vite:config       '.jsx',  '.tsx',
  vite:config       '.json'
  vite:config     ],
  vite:config     dedupe: [],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [ [Object], [Object] ]
  vite:config   },
  vite:config   publicDir: '/home/demivan/projects/vite-bug/packages/app/public',
  vite:config   cacheDir: '/home/demivan/projects/vite-bug/packages/app/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   ssr: {
  vite:config     target: 'node',
  vite:config     optimizeDeps: { noDiscovery: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   bundleChain: [],
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:force-systemjs-wrap-complete',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   esbuild: { jsxDev: false },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules$1],
  vite:config     middlewareMode: false,
  vite:config     fs: {
  vite:config       strict: true,
  vite:config       allow: [Array],
  vite:config       deny: [Array],
  vite:config       cachedChecks: undefined
  vite:config     }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   envDir: '/home/demivan/projects/vite-bug/packages/app',
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(1) {
  vite:config     'fnpd_/home/demivan/projects/vite-bug/packages/app' => {
  vite:config       dir: '/home/demivan/projects/vite-bug/packages/app',
  vite:config       data: [Object],
  vite:config       hasSideEffects: [Function: hasSideEffects],
  vite:config       webResolvedImports: {},
  vite:config       nodeResolvedImports: {},
  vite:config       setResolvedCache: [Function: setResolvedCache],
  vite:config       getResolvedCache: [Function: getResolvedCache]
  vite:config     },
  vite:config     set: [Function (anonymous)]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     holdUntilCrawlEnd: true,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +19ms
vite v5.4.1 building for production...
transforming (1) index.html  vite:resolve 0.39ms ./files/roboto-flex-cyrillic-ext-400-normal.woff2 -> null +0ms

./files/roboto-flex-cyrillic-ext-400-normal.woff2 referenced in ./files/roboto-flex-cyrillic-ext-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.12ms ./files/roboto-flex-cyrillic-ext-400-normal.woff -> null +1ms

./files/roboto-flex-cyrillic-ext-400-normal.woff referenced in ./files/roboto-flex-cyrillic-ext-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.11ms ./files/roboto-flex-cyrillic-400-normal.woff2 -> null +0ms

./files/roboto-flex-cyrillic-400-normal.woff2 referenced in ./files/roboto-flex-cyrillic-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.11ms ./files/roboto-flex-cyrillic-400-normal.woff -> null +0ms

./files/roboto-flex-cyrillic-400-normal.woff referenced in ./files/roboto-flex-cyrillic-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.17ms ./files/roboto-flex-greek-400-normal.woff2 -> null +1ms

./files/roboto-flex-greek-400-normal.woff2 referenced in ./files/roboto-flex-greek-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.12ms ./files/roboto-flex-greek-400-normal.woff -> null +0ms

./files/roboto-flex-greek-400-normal.woff referenced in ./files/roboto-flex-greek-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.26ms ./files/roboto-flex-vietnamese-400-normal.woff2 -> null +0ms

./files/roboto-flex-vietnamese-400-normal.woff2 referenced in ./files/roboto-flex-vietnamese-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.16ms ./files/roboto-flex-vietnamese-400-normal.woff -> null +1ms

./files/roboto-flex-vietnamese-400-normal.woff referenced in ./files/roboto-flex-vietnamese-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.13ms ./files/roboto-flex-latin-ext-400-normal.woff2 -> null +0ms

./files/roboto-flex-latin-ext-400-normal.woff2 referenced in ./files/roboto-flex-latin-ext-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.15ms ./files/roboto-flex-latin-ext-400-normal.woff -> null +0ms

./files/roboto-flex-latin-ext-400-normal.woff referenced in ./files/roboto-flex-latin-ext-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.10ms ./files/roboto-flex-latin-400-normal.woff2 -> null +0ms

./files/roboto-flex-latin-400-normal.woff2 referenced in ./files/roboto-flex-latin-400-normal.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
  vite:resolve 0.09ms ./files/roboto-flex-latin-400-normal.woff -> null +1ms

./files/roboto-flex-latin-400-normal.woff referenced in ./files/roboto-flex-latin-400-normal.woff didn't resolve at build time, it will remain unchanged to be resolved at runtime
✓ 4 modules transformed.
dist/index.html                 0.26 kB │ gzip: 0.20 kB
dist/assets/index-Didi1poi.css  1.95 kB │ gzip: 0.50 kB
dist/assets/index-9Z44_DjJ.js   0.71 kB │ gzip: 0.40 kB
✓ built in 82ms

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: cssp3-minor-bugAn edge case that only affects very specific usage (priority)regressionThe issue only appears after a new release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions