Skip to content

mergeConfig doesn't merge null or undefined values #18943

@cmd-johnson

Description

@cmd-johnson

Describe the bug

When using mergeConfig, it is impossible to merge null or undefined values into the base config.

I've encountered this while using Remix ≥ 2.14.0, where vite.mergeConfig is used to merge a value for server.watch into the base config. This doesn't work for null or undefined values, which are the only two values Remix tries to merge.
(See here for the mergeConfig call and here for the data that's passed into it).
This results in the server.watch value never actually being set to anything in the final config, which in turn leads to a whole bunch of files being watched by vite that don't actually need watching during a build.

I'm not sure what the rationale was for excluding null and undefined values when merging values, but I do see some use-cases for allowing them to be merged, like how remix tries (and fails) to do it.

Without additional context, I would expect any values in the overrides record to overwrite the default value, no matter what its value is. If I don't want to override a value, I shouldn't include its key in the overrides in the first place, so even undefined shouldn't receive special treatment as far as I'm concerned.

I've not submitted a PR because I'm not sure what other unforeseen side-effects the removal of that check would have.

I'd also be fine with keeping the check as-is, but then at the very least this quirk should be included in the documentation.

Reproduction

https://stackblitz.com/edit/vitejs-vite-66qrvrft?file=index.js

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^6.0.3 => 6.0.3

Used Package Manager

npm

Logs

{
  baseConfig: { server: { preTransformRequests: false, hmr: false } },
  overrideConfig: { server: { watch: null, otherKey: undefined } },
  mergedConfig: { server: { preTransformRequests: false, hmr: false } }
}

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions