Skip to content

[Bug]: node.__dirname: true causes broken output on Windows #8170

@lauriharpf

Description

@lauriharpf

System Info

System:
OS: Windows 10 10.0.19045
CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Memory: 5.78 GB / 15.82 GB
Binaries:
Node: 20.18.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
pnpm: 9.12.2 - ~\AppData\Roaming\npm\pnpm.CMD
Browsers:
Chrome: 129.0.6668.101
Edge: Spartan (44.19041.1266.0), Chromium (129.0.2792.89)
Internet Explorer: 11.0.19041.1566

Details

Note: This issue only reproduces when building on Windows, as Windows uses the \ path separator.

If

  1. node: { __dirname: true } (webpack documentation) is used in the RsPack config and
  2. The code being built is in a subdirectory and refers to __dirname
  3. The RsPack build is run on Windows via the Windows cmd command prompt

then RsPack output contains unescaped \ characters (e.g. __dirname: ${'src\foo\bar\baz'}), resulting in an unexpected string and not matching the webpack output (e.g. var __dirname = "src\\foo\\bar\\baz"; ... `__dirname: ${__dirname}).

(Thank you for your awesome work on this project 🙏❤ . Hope this report proves to be useful!)

Reproduce link

https://github.com/lauriharpf/rspack-node-dirname/tree/main

Reproduce Steps

  1. Use node: { __dirname: true } in the RsPack configuration.
  2. Refer to __dirname in a subdirectory file in the code being built (for example, like in the reproduce repo).
  3. Run the build on Windows via the cmd command prompt

What happens
RsPack does not escape the \-separator, output is e.g. src\foo\bar\baz. See https://github.com/lauriharpf/rspack-node-dirname/blob/main/rspack-dist/src_foo_bar_baz_render_js.js#L15

Image

webpack escapes the \-separator, output is e.g. src\\foo\\bar\\baz. See https://github.com/lauriharpf/rspack-node-dirname/blob/main/webpack-dist/src_foo_bar_baz_render_js.js#L10

Image

Desired result
RsPack and webpack build results match in terms of __dirname: both produce escaped output, e.g. src\\foo\\bar\\baz

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpending triageThe issue/PR is currently untouched.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions