vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.17k stars 6.04k forks source link

Inconsistent Builds in Module Sets #10506

Closed kudla closed 1 month ago

kudla commented 1 year ago

Describe the bug

On a same immutable application codebase and environment prerequisites Vite:build produces bundles including different sets of dependent modules. As a consequence chunk content and hashes can mutate from build to build.

Reproduction

https://github.com/kudla/vite-bug-reproduction

Steps to reproduce

npm i
while true; do npm run build  |  grep 'modules transformed' ; done

System Info

System:
    OS: macOS 12.4
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 182.59 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
  Browsers:
    Chrome: 106.0.5249.119
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-react: ^2.0.1 => 2.0.1
    vite: ^3.1.8 => 3.1.8

Used Package Manager

npm

Logs

Stats grep example

Click to expand! ```shell ✓ 1426 modules transformed. ✓ 1429 modules transformed. ✓ 1430 modules transformed. ✓ 1428 modules transformed. ```

Clear logs

Click to expand! ```shell vite v3.1.8 building for production... ✓ 1425 modules transformed. dist/index.html 0.77 KiB dist/assets/index.f0e9d730.js 1.40 KiB / gzip: 0.72 KiB dist/assets/vendor.f2801889.js 33.65 KiB / gzip: 10.86 KiB dist/assets/react.7dcc98bc.js 8.17 KiB / gzip: 3.40 KiB dist/assets/utils.5559554b.js 95.03 KiB / gzip: 34.55 KiB dist/assets/main.6d36f42c.js 124.78 KiB / gzip: 46.36 KiB dist/assets/mui.e8d37d78.js 192.64 KiB / gzip: 64.77 KiB 02:57:12  home  vite-bug  ⬡ v16.15.1   master ✘ ✭  28s  $ npm run build > my-app@0.0.0 build > tsc && vite build `test -n "$VITE_BASE_PATH" && echo "--base=$VITE_BASE_PATH" || true` vite v3.1.8 building for production... ✓ 1430 modules transformed. dist/index.html 0.77 KiB dist/assets/index.41455140.js 1.40 KiB / gzip: 0.71 KiB dist/assets/vendor.f2801889.js 33.65 KiB / gzip: 10.86 KiB dist/assets/react.7dcc98bc.js 8.17 KiB / gzip: 3.40 KiB dist/assets/utils.5559554b.js 95.03 KiB / gzip: 34.55 KiB dist/assets/main.56be54e9.js 124.50 KiB / gzip: 46.27 KiB dist/assets/mui.e8d37d78.js 192.64 KiB / gzip: 64.77 KiB 02:59:09  home  vite-bug  ⬡ v16.15.1   master ✘ ✭  27s  $ npm run build > my-app@0.0.0 build > tsc && vite build `test -n "$VITE_BASE_PATH" && echo "--base=$VITE_BASE_PATH" || true` vite v3.1.8 building for production... ✓ 1426 modules transformed. dist/index.html 0.77 KiB dist/assets/vendor.f2801889.js 33.65 KiB / gzip: 10.86 KiB dist/assets/react.7dcc98bc.js 8.17 KiB / gzip: 3.40 KiB dist/assets/index.3ada4ac9.js 1.40 KiB / gzip: 0.71 KiB dist/assets/utils.5559554b.js 95.03 KiB / gzip: 34.55 KiB dist/assets/main.85318e29.js 124.71 KiB / gzip: 46.33 KiB dist/assets/mui.e8d37d78.js 192.64 KiB / gzip: 64.77 KiB ```

Huck Debug Session

Over here I've added a line kind of

console.log(id)

And based on this I've got some stats on included modules within several different build sessions. Numbers in filenames stand for resulting modules amount for aa session

Raw Logs

build-test-1424.log build-test-1429.log build-test-1430.log

Preaggregated Comparable Module Sets

modules-1430.log modules-1429.log modules-1424.log

And the Largest Module Sets Spread Diff

Click to expand! ``` 11963a11964 > /Users/home/vite-bug/node_modules/lodash/_WeakMap.js?commonjs-proxy 12018a12020 > /Users/home/vite-bug/node_modules/lodash/_baseIsNative.js?commonjs-proxy 12075a12078 > /Users/home/vite-bug/node_modules/lodash/_coreJsData.js?commonjs-proxy 12118a12122 > /Users/home/vite-bug/node_modules/lodash/_getNative.js?commonjs-proxy 12128a12133 > /Users/home/vite-bug/node_modules/lodash/_getValue.js?commonjs-proxy 12160a12166 > /Users/home/vite-bug/node_modules/lodash/_isMasked.js?commonjs-proxy ```

Validations

kudla commented 1 year ago

I'm not really sure the reproduction example is a minimal. It looks like the difference is alway about a lodash submodules. Yet including only the lodash to a project didn't have any reported defects. So for now I've added the main deps I had in the original project. Didn't have a time to bisect a root cause.

wmassa commented 1 year ago

I created a minimal repro for this issue based on @kudla's code. Hopefully, this helps to gain traction for this issue.

I created a vanilla Vite project and copy pasted the following lines in main.js:

import flow from 'lodash/flow';
import split from 'lodash/fp/split';

export const confNumbers = flow(split(/[;,\s]+/))('e');

Multiple yarn build give different outputs for me.

git clone https://github.com/wmassa/vite-inconsistency-repro
cd vite-inconsistency-repro
yarn
yarn build
yarn build
yarn build
yarn build
yarn build

It outputs index-58937766.js, index-2df03ccf.js, or index-1939915d.js for me.

kudla commented 1 year ago

I created a minimal repro for this issue

Cool so that now it is 7/7 validated Thank you @wmassa

wmassa commented 1 year ago

A workaround for me is setting maxParallelFileOps: 1, within the rollup options of the vite.config.js file:

import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    rollupOptions: {
      maxParallelFileOps: 1,
    },
  },
});
rogatty commented 1 year ago

maxParallelFileOps: 1 workaround doesn't work for me if I delete node_modules/.vite after each build.

derheld42 commented 11 months ago

The workaround in #10506 doesn't seem to work for me. I created a repo to try testing the workaround using the basic out-of-the-box base sveltekit app. Test repo is here https://github.com/derheld42/repro-vite-10506

sapphi-red commented 1 month ago

This repro (https://github.com/vitejs/vite/issues/10506#issuecomment-1363616954) was a duplicate of #13672 as the workaround in https://github.com/vitejs/vite/issues/13672#issuecomment-1784110536 worked.

https://github.com/vitejs/vite/issues/10506#issuecomment-1751343335 was because svelte kit was injecting non-deterministic value (https://github.com/sveltejs/kit/issues/12260). See https://github.com/sveltejs/kit/issues/12260#issuecomment-2130361086.

Closing as a duplicate.