Non-deterministic build
Problem
Describe the bug Executing builds multiple times with the same code generates different hashes. Reproduction https://github.com/ttionya/vite-non-deterministic-build-repro Steps to reproduce Clone the aforementioned repro, install dependencies, and execute `npm run build` multiple times. Verify the hash of the vendor file. System Info [code block] Used Package Manager npm Logs _No response_ Validations - [X] Follow our Code of Conduct - [X] Read the Contributing Guidelines. - [X] Read the docs. - [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate. - [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead. - [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server. - [X] The provided reproduction is a minimal reproducible example of the bug.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Ensure Deterministic Builds in Vite Configuration
The non-deterministic builds are likely caused by the inclusion of timestamps, random values, or non-deterministic algorithms in the build process. This can occur due to plugins or configurations that generate unique identifiers or hashes based on the current time or other variable states.
Awaiting Verification
Be the first to verify this fix
- 1
Update Vite Configuration
Modify the Vite configuration to ensure that the build process does not include any non-deterministic elements. This can include setting specific options for plugins that may introduce randomness.
javascriptexport default defineConfig({ build: { rollupOptions: { output: { entryFileNames: '[name].js', chunkFileNames: '[name].js', assetFileNames: '[name].[ext]' } } } } }); - 2
Lock Dependency Versions
Ensure that all dependencies are locked to specific versions in your package.json file to prevent any changes in behavior due to updates. Use npm shrinkwrap or package-lock.json to maintain consistent installs.
bashnpm install --save-exact <package-name> - 3
Disable Source Maps in Production
Disable source maps in the production build to avoid any variability introduced by source map generation. This can be done in the Vite configuration.
javascriptexport default defineConfig({ build: { sourcemap: false } }); - 4
Use a Consistent Build Environment
Ensure that the build is executed in a consistent environment. Use Docker or a similar tool to create a reproducible build environment that eliminates discrepancies due to local machine differences.
dockerfileFROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "run", "build"]
Validation
After applying the above steps, run 'npm run build' multiple times and verify that the hash of the vendor file remains consistent across builds. If the hash is the same, the issue is resolved.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep