Skip to content

Macro values Infinity, NaN and -0 are inlined as null, null and 0 #183

Open

Description

@trackoor

Describe the bug

Number values returned by a macro are serialized with JSON.stringify, so non-finite numbers (Infinity, -Infinity, NaN) become null and -0 becomes 0. The build succeeds and the bundle silently uses the wrong value.

stringifyValue in src/core/index.ts falls through to return JSON.stringify(value) for numbers (including inside arrays and plain objects returned by a macro, so [Infinity] becomes [null]). Emitting Infinity, -Infinity, NaN and -0 for those cases (as stringifyValue already does for bigint, RegExp and Date) would preserve the returned number.

Reproduction

https://gist.github.com/trackoor/640d2cb62b2fb44bbb76b524613f38e8

// macros.js
export const maxUploadSize = Infinity;
export function parseRatio() { return Number.parseFloat("n/a"); }
export const offset = -0;
// main.js
import { maxUploadSize, parseRatio, offset } from "./macros.js" with { type: "macro" };
console.log(maxUploadSize, parseRatio(), Object.is(offset, -0));
// rollup.config.mjs
import Macros from "unplugin-macros/rollup";
export default { input: "main.js", output: { file: "dist/main.js", format: "es" }, plugins: [Macros()] };

Run npx rollup -c rollup.config.mjs && cat dist/main.js && node dist/main.js.

Actual:

console.log(null, null, Object.is(0, -0));
null null false

Expected:

console.log(Infinity, NaN, Object.is(-0, -0));
Infinity NaN true

Tested on unplugin-macros 0.23.4 and on main (0ce043f).

System Info

System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
    Memory: 181.66 GB / 251.88 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 26.13.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/node
    npm: 11.20.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/npm
    pnpm: 12.18.0 - /home/hanzhi1/eval-tools/node-v26.13.0-linux-x64/bin/pnpm
  npmPackages:
    rollup: ^4.68.0 => 4.68.0
    unplugin-macros: ^0.23.4 => 0.23.4

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Credit: https://ucsbplse.github.io/

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions