Skip to content

Non-deterministic build output: identical source produces different JS filenames #16347

Closed

Description

@ocavue

Astro Info

Astro                    v6.1.7
Node                     v24.11.1
System                   macOS (arm64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             @astrojs/react
                         @astrojs/vue
                         @astrojs/svelte

If this issue only occurs in one browser, which browser is a problem?

N/A

Describe the Bug

astro build produces different output filenames across consecutive runs with identical source code. This breaks CDN caching — every deploy invalidates cached JS files even when nothing changed.

Reproduction

https://github.com/issueset/astro_build_order_issue.git

pnpm install
pnpm test  # runs 10 builds, compares output
Running 10 builds and comparing output...
Temp dir: /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn

Build 1: 7c17b92b2df44911065cc8e19abf7622
Build 2: 7c17b92b2df44911065cc8e19abf7622
Build 3: 8bc47ea55d5d13a6359f0d39b2461712
  ^^^ DIFFERS from build 2:
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build2/_astro: SvelteWidget0.CHZtD1E3.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build3/_astro: SvelteWidget0.DNGJBpKz.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build2/_astro: SvelteWidget1.C_-Zw1cq.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build3/_astro: SvelteWidget1.Ch6lmidG.js
    (4 total differences)
Build 4: 8bc47ea55d5d13a6359f0d39b2461712
Build 5: 8bc47ea55d5d13a6359f0d39b2461712
Build 6: 8bc47ea55d5d13a6359f0d39b2461712
Build 7: 8bc47ea55d5d13a6359f0d39b2461712
Build 8: 7c17b92b2df44911065cc8e19abf7622
  ^^^ DIFFERS from build 7:
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build8/_astro: SvelteWidget0.CHZtD1E3.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build7/_astro: SvelteWidget0.DNGJBpKz.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build8/_astro: SvelteWidget1.C_-Zw1cq.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build7/_astro: SvelteWidget1.Ch6lmidG.js
    (4 total differences)
Build 9: 8bc47ea55d5d13a6359f0d39b2461712
  ^^^ DIFFERS from build 8:
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build8/_astro: SvelteWidget0.CHZtD1E3.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build9/_astro: SvelteWidget0.DNGJBpKz.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build8/_astro: SvelteWidget1.C_-Zw1cq.js
    Only in /var/folders/00/y54gq_b567l2cd0qtqqqlysm0000gn/T/tmp.MlsGPfeaxn/build9/_astro: SvelteWidget1.Ch6lmidG.js
    (4 total differences)
Build 10: 8bc47ea55d5d13a6359f0d39b2461712

RESULT: Non-deterministic! 3 of 9 consecutive comparisons differed.

Cause

In static-build.ts#L374-L376, client build entry points are passed to Rollup via Array.from(internals.clientInput). clientInput is a Set whose iteration order depends on the order components were discovered during prerendering. That discovery order is non-deterministic because this.getModuleIds() in the plugin-analyzer reflects async module resolution timing (Rollup resolves dependencies in parallel via Promise.all).

When Rollup receives entry points in different order, chunk assignment and hash collision resolution produce different output filenames.

Fix

Sort the array before passing it to Rollup:

See #16348

What's the expected result?

Deterministic build output.

Link to Minimal Reproducible Example

https://github.com/issueset/astro_build_order_issue.git

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. astrobot-houston commented on Apr 16, 2026

    @astrobot-houston
    Contributor

    I was able to reproduce this issue. The root cause is in packages/astro/src/core/build/static-build.ts, where client build entry points are passed to Rollup via Array.from(internals.clientInput) without sorting. The clientInput Set's iteration order depends on insertion order from the plugin-analyzer, which uses this.getModuleIds() — an API whose iteration order is non-deterministic due to parallel async module resolution. When Rollup receives entry points in different orders, chunk assignment and content-hash computation produce different output filenames.

    I was able to fix this issue. The fix adds .sort() to the Array.from(internals.clientInput) call at line 374 of static-build.ts, ensuring Rollup always receives entry points in a stable lexicographic order regardless of module discovery timing. View Suggested Fix

    P4: important. This affects any project using multiple client-side framework components (e.g. React + Vue + Svelte with hydration directives) — every deploy can invalidate CDN caches even when source code hasn't changed. While it doesn't cause runtime errors, it violates the expected behavior that identical source produces identical output, and it significantly impacts caching and deployment pipelines for production sites.

    Full Triage Report

    No report.md was generated by prior triage steps. The following analysis was performed directly from the source code and issue description.

    Root Cause

    In packages/astro/src/core/build/plugins/plugin-analyzer.ts:23, the generateBundle hook iterates over this.getModuleIds(). The order of IDs returned by this Rollup/Vite API depends on the order modules were resolved, which is non-deterministic because Rollup resolves dependencies in parallel.

    As the analyzer discovers hydrated components, client-only components, and scripts, it inserts them into Maps/Sets on BuildInternals (discoveredHydratedComponents, discoveredClientOnlyComponents, discoveredScripts). These are later collected by getClientInput() (static-build.ts:610-630) into a single Set<string>.

    At static-build.ts:374, this Set was converted to an array via Array.from(internals.clientInput) and passed directly as Rollup's input. Since Set preserves insertion order, and insertion order is non-deterministic, Rollup received entry points in varying order across builds, producing different chunk names/hashes.

    Fix

    A one-line change adding .sort() to the Array.from() call:

      builder.environments.client.config.build.rollupOptions.input = Array.from(
        internals.clientInput,
    - );
    + ).sort();

    This ensures Rollup always receives entry points in a stable lexicographic order, producing deterministic output filenames.

    Verification

    The fix is on branch flue/fix-16347 (commit a17a59f4ed). The change is minimal and isolated to the client build input ordering — it does not affect SSR, prerender, or any other build phase.

    This report was made by an LLM. Mistakes happen, check important info.

  2. added
    - P4: important
    pkg: astro
    and removed
    triage: needs triage
    on Apr 16, 2026
  3. leifmarcus commented on Apr 16, 2026

    @leifmarcus
    Contributor

    We had the same issue and found a solution for this. Here is how we changed our config

    export default defineConfig({
      vite: {
        build: {
          /**
           * Force sequential file resolution to ensure deterministic module ordering
           * across builds, eliminating Rollup's parallel resolution race condition.
           * @see https://github.com/rollup/rollup/issues/5902
           */
          rollupOptions: {
            maxParallelFileOps: 1,
            output: {
              /**
               * Disable esbuild's short export aliasing to prevent non-deterministic
               * minified names that cascade into different content hashes.
               * @see https://github.com/vitejs/vite/issues/6773
               */
              minifyInternalExports: false,
            },
          },
        },
    });
  4. ocavue commented on Apr 16, 2026

    @ocavue
    ContributorAuthor

    @leifmarcus Thanks for letting me know. I guess maxParallelFileOps: 1 will slow down the build, and minifyInternalExports: false will increase the file size.

    quoted from vitejs/vite#6773 (comment)

    Down side is the increase of file size of around 2% .

    I wonder if you still need these two workarounds after my patch is released. If you can still reproduce the non-deterministic issue, perhaps there is another place that needs .sort().

  5. pdig-tobiasbreit commented on Apr 16, 2026

    @pdig-tobiasbreit

    @ocavue is your change/suggestion already merged?

  6. ocavue commented on Apr 16, 2026

    @ocavue
    ContributorAuthor

    @ocavue is your change/suggestion already merged?

    Not yet. See #16348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P4: importantpkg: astro

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions