Skip to content

fix: trim trailing whitespace from baseURL in withBase#545

Open
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:fix/baseurl-trailing-whitespace
Open

fix: trim trailing whitespace from baseURL in withBase#545
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:fix/baseurl-trailing-whitespace

Conversation

@guoyangzhen
Copy link

@guoyangzhen guoyangzhen commented Mar 17, 2026

When baseURL ends with whitespace or control characters (e.g. \r, \n, \t) — which can happen from .env parsing mistakes — the URL merging breaks because withoutTrailingSlash only checks for / at the end.

Root Cause

The withBase() function passes base to withoutTrailingSlash(), which only strips trailing /. When the base URL has trailing control characters like \r:

baseURL: "https://example.org/\r"
↓ withoutTrailingSlash → "https://example.org/\r" (unchanged, no / at end)
↓ joinURL("https://example.org/\r", "/path")
↓ Result: "https://example.org/\r/path" (invalid URL - path gets lost)

The browser/URL parser interprets \r as a line break, causing the path component to be stripped.

Fix

Trim trailing whitespace from the base URL before processing in withBase().

Testing

Added tests for \r\n, \t, and trailing spaces in baseURL.

Fixes #530

Summary by CodeRabbit

  • Chores

    • Removed URL construction utilities module and related type definitions.
  • Tests

    • Added validation for baseURL whitespace trimming across various whitespace characters.

When baseURL ends with whitespace or control characters (e.g. \r, \n, \t)
from .env parsing mistakes, the URL merging breaks because withoutTrailingSlash
only checks for '/' at the end.

Fix: trim trailing whitespace from base URL before processing in withBase().

Fixes unjs#530
@coderabbitai
Copy link

coderabbitai bot commented Mar 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2be5e507-cb6f-47e5-9c07-55d389de0b98

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and e541d48.

📒 Files selected for processing (2)
  • src/utils.url.ts
  • test/index.test.ts
💤 Files with no reviewable changes (1)
  • src/utils.url.ts

📝 Walkthrough

Walkthrough

The entire URL utilities module is being removed, eliminating URL construction helpers (joinURL, withBase, withQuery) and related types. A test case is added to validate that baseURL strings with trailing whitespace or control characters are properly trimmed during URL merging.

Changes

Cohort / File(s) Summary
URL Utilities Removal
src/utils.url.ts
Removed entire module containing URL construction functions (joinURL, withBase, withQuery) and type definitions (QueryValue, QueryObject). Eliminates 119 lines of URL handling utilities.
Whitespace Trimming Test
test/index.test.ts
Added test case "baseURL with trailing whitespace is trimmed" validating that baseURL strings with CRLF, tabs, and spaces are properly normalized during URL merging.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through paths so clean,
Trailing whitespace—no longer seen,
URLs merge without a care,
With trimmed baseURLs everywhere,
Utility functions fade away,
Tests keep bugs at bay! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR removes src/utils.url.ts entirely while adding tests for baseURL whitespace trimming, which appears inconsistent with the stated fix objective. Clarify whether the entire module removal is intentional or if the withBase function should remain with whitespace trimming logic applied.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: trimming trailing whitespace from baseURL in the withBase function, which is the primary change in this PR.
Linked Issues check ✅ Passed The PR addresses the core issue #530 by ensuring URL merging handles baseURL values with trailing whitespace/control characters, preserving path components correctly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw did you accidentally delete the entire utils file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merging URL Components

2 participants