Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lauriharpf opened this issue Oct 18, 2024 · 2 comments · May be fixed by #8172
Open

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

lauriharpf opened this issue Oct 18, 2024 · 2 comments · May be fixed by #8172
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@lauriharpf
Copy link

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

@lauriharpf lauriharpf added bug Something isn't working pending triage The issue/PR is currently untouched. labels Oct 18, 2024
@inottn
Copy link
Contributor

inottn commented Oct 19, 2024

I will try to fix it.

@lauriharpf
Copy link
Author

Thank you! 🙏 As an additional observation, noticed that this only seems to affect node: { __dirname: true }. node: { __filename: true } appears to work as expected in RsPack (escapes the backslash path separators).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants