[Bug]: CJS `import.meta.url` shim does not percent-encode `#` or `%` in paths

オープン 初心者向け
#2,014 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
node.js, typescript
領域
build-system

調査の方向性

プロジェクト内でデフォルトの shims.cjs['import.meta.url'] 定義を検索し、CJS shim がファイル URL をどのように構築するかを確認してください。# と % を含むパスで再現し、shim が期待どおりにエンコードされた URL を生成すること、および fileURLToPath と new URL('./asset', import.meta.url) が正しく解決されることを確認してください。Issue にはテストファイルの指定がないため、既存の shim テストを探し、そこでカバレッジを追加してください。

索引モデルが issue の本文から書いたものです。

説明

Version
System:
  OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
  CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
  Memory: 217.67 GB / 251.88 GB
  Container: Yes
  Shell: 5.2.15 - /bin/bash
npmPackages:
  @rslib/core: ^1.0.6 => 1.0.6 (@rspack/core 2.3.1, main b6645e20)
Details

The default shims.cjs['import.meta.url'] builds the file URL as new (require('url'.replace('', '')).URL)('file:' + __filename).href. Concatenating 'file:' + __filename does not percent-encode # or % in the filesystem path, so # is parsed as a URL fragment delimiter and %41 is treated as a percent-encoded sequence. Passing the shimmed import.meta.url to fileURLToPath(import.meta.url) or new URL('./asset', import.meta.url) then resolves to the wrong path (truncated at # or with % sequences decoded).

Using url.pathToFileURL(__filename).href encodes special characters in the path properly.

Reproduce link

https://gist.github.com/trackoor/33741ecfe4ad7ae3958c7de54f4a1e38

Reproduce Steps

In a directory named we#ird %41 dir with { "name": "repro", "type": "module" } in package.json:

// src/index.js
import { fileURLToPath } from "node:url";
export const url = import.meta.url;
export const file = fileURLToPath(import.meta.url);

// rslib.config.mjs
import { defineConfig } from "@rslib/core";
export default defineConfig({ lib: [{ format: "cjs" }] });

Run npx rslib build, then run:

node -e 'console.log(require("./dist/index.cjs"))'

Actual output:

{
  file: '/.../we',
  url: 'file:///.../we#ird%20%41%20dir/dist/index.cjs'
}

Expected output:

{
  file: '/.../we#ird %41 dir/dist/index.cjs',
  url: 'file:///.../we%23ird%20%2541%20dir/dist/index.cjs'
}

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

主要言語
TypeScript
スター
1k
フォーク
68
平均マージ
9時間 13分
マージ済み PR(30日)
52

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

web-infra-dev/rslib のほかの issue

web-infra-dev/rslib の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。