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

UTF-8 with BOM is not parsed correctly in imported package.json #18911

Closed
7 tasks done
bisubus opened this issue Dec 7, 2024 · 0 comments · Fixed by #19000
Closed
7 tasks done

UTF-8 with BOM is not parsed correctly in imported package.json #18911

bisubus opened this issue Dec 7, 2024 · 0 comments · Fixed by #19000
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@bisubus
Copy link

bisubus commented Dec 7, 2024

Describe the bug

package.json with UTF-8 with BOM encoding is not parsed correctly in imported modules, both in dev and build modes. This is likely to happen when a package doesn't originate from npm registry.

This results in an obscure error when a package that contains such package.json fails to be imported:

error during build:
[vite]: Rollup failed to resolve import "bom" from ".../src/import-packages.mjs".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at viteWarn (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50728:17)
    at onRollupWarning (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50760:5)
    at onwarn (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50426:7)
    at file:///.../node_modules/rollup/dist/es/shared/node-entry.js:19475:13
    at Object.logger [as onLog] (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:21201:9)
    at ModuleLoader.handleInvalidResolvedId (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20090:26)
    at file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20048:26
    at async Promise.all (index 1)

And more specific error when package directory is directly imported:

error during build:
[commonjs--resolver] Unexpected token '', "{
  "exp"... is not valid JSON
    at JSON.parse (<anonymous>)
    at loadPackageData (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:9420:21)
    at tryCleanFsResolve (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:16089:23)
    at tryFsResolve (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:16037:15)
    at Object.resolveId (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:15875:19)
    at Object.handler (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50806:15)
    at file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20812:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20712:28)
    at async resolveId (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:19316:26)
    at async ModuleLoader.resolveId (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:19745:15)

Imported package.json without BOM is correctly processed by Vite under the same conditions.

package.json with BOM are correctly imported natively in Node, besides the inconsistencies between Node versions be caused by importing directories with ESM (ERR_UNSUPPORTED_DIR_IMPORT):

node src/import-packages.mjs

Reproduction

https://github.com/bisubus/vite-package-json-utf8-demo

Steps to reproduce

npm i
npx vite --config vite.config-packages.js build
npx vite --config vite.config-dirs.js build
npx vite --config vite.config-packages.js
npx vite --config vite.config-dirs.js

System Info

vite: 6.0.3

OS: Windows 10 10.0.19044
Node: 20.12.2 - C:\Program Files\nodejs\node.EXE
npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD

Used Package Manager

npm

Logs

  vite:config bundled config file loaded in 16.87ms +0ms
  vite:config using resolved config: {
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     polyfillModulePreload: true,
  vite:config     modulePreload: { polyfill: true },
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     sourcemap: false,
  vite:config     terserOptions: {},
  vite:config     rollupOptions: { input: './index-dirs.html' },
  vite:config     commonjsOptions: { include: [ /node_modules/ ], extensions: [ '.js', '.cjs' ] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [ /node_modules/ ] },
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssrManifest: false,
  vite:config     ssrEmitAssets: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     cssCodeSplit: true,
  vite:config     minify: 'esbuild',
  vite:config     ssr: false,
  vite:config     emitAssets: true,
  vite:config     createEnvironment: [Function: createEnvironment],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssMinify: true
  vite:config   },
  vite:config   configFile: '.../vite.config-dirs.js',
  vite:config   environments: {
  vite:config     client: {
  vite:config       define: undefined,
  vite:config       resolve: {
  vite:config         externalConditions: [ 'node' ],
  vite:config         extensions: [ '.mjs', '.js', '.ts', '.jsx', '.tsx', '.json' ],
  vite:config         dedupe: [],
  vite:config         noExternal: [],
  vite:config         external: [],
  vite:config         preserveSymlinks: false,
  vite:config         alias: [
  vite:config           {
  vite:config             find: /^\/?@vite\/env/,
  vite:config             replacement: '/@fs/.../node_modules/vite/dist/client/env.mjs'
  vite:config           },
  vite:config           {
  vite:config             find: /^\/?@vite\/client/,
  vite:config             replacement: '/@fs/.../node_modules/vite/dist/client/client.mjs'
  vite:config           }
  vite:config         ],
  vite:config         mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ],
  vite:config         conditions: [ 'module', 'browser', 'development|production' ],
  vite:config         enableBuiltinNoExternalCheck: false
  vite:config       },
  vite:config       keepProcessEnv: false,
  vite:config       consumer: 'client',
  vite:config       optimizeDeps: {
  vite:config         include: [],
  vite:config         exclude: [],
  vite:config         needsInterop: [],
  vite:config         extensions: [],
  vite:config         disabled: undefined,
  vite:config         holdUntilCrawlEnd: true,
  vite:config         force: false,
  vite:config         noDiscovery: false,
  vite:config         esbuildOptions: { preserveSymlinks: false }
  vite:config       },
  vite:config       dev: {
  vite:config         warmup: [],
  vite:config         sourcemap: { js: true },
  vite:config         sourcemapIgnoreList: [Function: isInNodeModules$1],
  vite:config         preTransformRequests: true,
  vite:config         createEnvironment: [Function: defaultCreateClientDevEnvironment],
  vite:config         recoverable: true,
  vite:config         moduleRunnerTransform: false
  vite:config       },
  vite:config       build: {
  vite:config         target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config         polyfillModulePreload: true,
  vite:config         modulePreload: { polyfill: true },
  vite:config         outDir: 'dist',
  vite:config         assetsDir: 'assets',
  vite:config         assetsInlineLimit: 4096,
  vite:config         sourcemap: false,
  vite:config         terserOptions: {},
  vite:config         rollupOptions: { input: './index-dirs.html' },
  vite:config         commonjsOptions: { include: [ /node_modules/ ], extensions: [ '.js', '.cjs' ] },
  vite:config         dynamicImportVarsOptions: { warnOnError: true, exclude: [ /node_modules/ ] },
  vite:config         write: true,
  vite:config         emptyOutDir: null,
  vite:config         copyPublicDir: true,
  vite:config         manifest: false,
  vite:config         lib: false,
  vite:config         ssrManifest: false,
  vite:config         ssrEmitAssets: false,
  vite:config         reportCompressedSize: true,
  vite:config         chunkSizeWarningLimit: 500,
  vite:config         watch: null,
  vite:config         cssCodeSplit: true,
  vite:config         minify: 'esbuild',
  vite:config         ssr: false,
  vite:config         emitAssets: true,
  vite:config         createEnvironment: [Function: createEnvironment],
  vite:config         cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config         cssMinify: true
  vite:config       }
  vite:config     }
  vite:config   },
  vite:config   resolve: {
  vite:config     externalConditions: [ 'node' ],
  vite:config     extensions: [ '.mjs', '.js', '.ts', '.jsx', '.tsx', '.json' ],
  vite:config     dedupe: [],
  vite:config     noExternal: [],
  vite:config     external: [],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [
  vite:config       {
  vite:config         find: /^\/?@vite\/env/,
  vite:config         replacement: '/@fs/.../node_modules/vite/dist/client/env.mjs'
  vite:config       },
  vite:config       {
  vite:config         find: /^\/?@vite\/client/,
  vite:config         replacement: '/@fs/.../node_modules/vite/dist/client/client.mjs'
  vite:config       }
  vite:config     ],
  vite:config     mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
  vite:config     conditions: [ 'module', 'node', 'development|production' ],
  vite:config     enableBuiltinNoExternalCheck: false
  vite:config   },
  vite:config   configFileDependencies: [ '.../vite.config-dirs.js' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: 'vite.config-dirs.js',
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '...',
  vite:config   base: '/',
  vite:config   decodedBase: '/',
  vite:config   rawBase: '/',
  vite:config   publicDir: '.../public',
  vite:config   cacheDir: '.../node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  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:rollup-options-plugins',
  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:manifest',
  vite:config     'vite:ssr-manifest',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   css: {
  vite:config     transformer: 'postcss',
  vite:config     preprocessorMaxWorkers: 0,
  vite:config     devSourcemap: false,
  vite:config     lightningcss: undefined
  vite:config   },
  vite:config   json: { namedExports: true, stringify: 'auto' },
  vite:config   esbuild: { jsxDev: false },
  vite:config   server: {
  vite:config     port: 5173,
  vite:config     strictPort: false,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: false,
  vite:config     proxy: undefined,
  vite:config     cors: true,
  vite:config     headers: {},
  vite:config     warmup: { clientFiles: [], ssrFiles: [] },
  vite:config     middlewareMode: false,
  vite:config     fs: {
  vite:config       strict: true,
  vite:config       deny: [ '.env', '.env.*', '*.{crt,pem}', '**/.git/**' ],
  vite:config       allow: [ '...' ]
  vite:config     },
  vite:config     preTransformRequests: true,
  vite:config     perEnvironmentStartEndDuringDev: false,
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules$1]
  vite:config   },
  vite:config   builder: undefined,
  vite:config   preview: {
  vite:config     port: 4173,
  vite:config     strictPort: false,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: false,
  vite:config     proxy: undefined,
  vite:config     cors: true,
  vite:config     headers: {}
  vite:config   },
  vite:config   envDir: '...',
  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_...' => {
  vite:config       dir: '...',
  vite:config       data: {
  vite:config         name: 'vite-package-json-utf8-demo',
  vite:config         private: true,
  vite:config         version: '0.0.0',
  vite:config         type: 'module',
  vite:config         scripts: {},
  vite:config         dependencies: { bom: 'file:./src/bom', nobom: 'file:./src/nobom' },
  vite:config         devDependencies: { vite: '^6.0.1' }
  vite:config       },
  vite:config       hasSideEffects: [Function: hasSideEffects],
  vite:config       setResolvedCache: [Function: setResolvedCache],
  vite:config       getResolvedCache: [Function: getResolvedCache]
  vite:config     },
  vite:config     set: [Function (anonymous)]
  vite:config   },
  vite:config   worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   future: undefined,
  vite:config   ssr: {
  vite:config     target: 'node',
  vite:config     optimizeDeps: { esbuildOptions: { preserveSymlinks: false } },
  vite:config     resolve: { conditions: undefined, externalConditions: undefined }
  vite:config   },
  vite:config   optimizeDeps: {
  vite:config     include: [],
  vite:config     exclude: [],
  vite:config     needsInterop: [],
  vite:config     extensions: [],
  vite:config     disabled: undefined,
  vite:config     holdUntilCrawlEnd: true,
  vite:config     force: false,
  vite:config     noDiscovery: false,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   dev: {
  vite:config     warmup: [],
  vite:config     sourcemap: { js: true },
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules$1],
  vite:config     preTransformRequests: false,
  vite:config     createEnvironment: [Function: defaultCreateDevEnvironment],
  vite:config     recoverable: false,
  vite:config     moduleRunnerTransform: false
  vite:config   },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks],
  vite:config   createResolver: [Function: createResolver],
  vite:config   fsDenyGlob: [Function: arrayMatcher],
  vite:config   safeModulePaths: Set(0) {}
  vite:config } +22ms
vite v6.0.3 building for production...
✓ 2 modules transformed.
x Build failed in 38ms
error during build:
[commonjs--resolver] Unexpected token '', "{
  "exp"... is not valid JSON
    at JSON.parse (<anonymous>)
    at loadPackageData (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:9420:21)
    at tryCleanFsResolve (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:16089:23)
    at tryFsResolve (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:16037:15)
    at Object.resolveId (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:15875:19)
    at Object.handler (file:///.../node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50806:15)
    at file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20812:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:20712:28)
    at async resolveId (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:19316:26)
    at async ModuleLoader.resolveId (file:///.../node_modules/rollup/dist/es/shared/node-entry.js:19745:15)

Validations

@sapphi-red sapphi-red added the p2-edge-case Bug, but has workaround or limited in scope (priority) label Dec 9, 2024
sapenlei added a commit to sapenlei/vite that referenced this issue Dec 18, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants