#9817: fix(media): resolve relative paths before reading local files (#8759)
channel: whatsapp-web
stale
Cluster:
Media Handling Improvements
Fixes #8759
## Problem
When an agent reply contains a `MEDIA:./path` directive with a relative path (e.g., `MEDIA:./avatars/default.jpg`), the file read fails with ENOENT even though the file exists.
## Root Cause
`loadWebMedia()` in `src/web/media.ts` was calling `fs.readFile(mediaUrl)` directly for local paths without resolving relative paths to absolute paths first.
## Solution
Resolve relative paths starting with `./` or `../` against `process.cwd()` before reading:
```typescript
if (mediaUrl.startsWith('./') || mediaUrl.startsWith('../')) {
mediaUrl = path.resolve(process.cwd(), mediaUrl);
}
```
## Testing
Added regression tests for both `./` and `../` relative paths.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes local `MEDIA:` loads when the directive uses a relative path by resolving `./` and `../` paths against `process.cwd()` before calling `fs.readFile` in `src/web/media.ts`. It also adds regression tests in `src/web/media.test.ts` intended to cover both `./` and `../` relative paths.
The change fits into the existing `loadWebMediaInternal` flow by keeping remote URLs handled via `fetchRemoteMedia`, expanding `~` via `resolveUserPath`, and then normalizing local paths prior to reading/sniffing MIME.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge once the regression test correctly exercises `../`-prefixed paths.
- The runtime change is small and localized (path resolution before local reads). The main issue is test coverage: the added `../` test doesn’t actually start with `../`, so it doesn’t guard the new branch and could miss regressions.
- src/web/media.test.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22401: fix: resolve relative media paths against workspace and fix /tmp on...
by derrickburns · 2026-02-21
84.7%
#18811: fix(media): require file extension for ambiguous MEDIA: path detection
by aldoeliacim · 2026-02-17
83.8%
#22356: test(web): fix media test fixture local root handling
by AIflow-Labs · 2026-02-21
81.1%
#22178: test(web): allow fixture roots in media local file tests
by Kansodata · 2026-02-20
80.5%
#16938: fix(media): reject unsupported URL schemes with clear error message
by zerone0x · 2026-02-15
79.7%
#8076: fix(web): handle data URLs in loadWebMedia to prevent ENAMETOOLONG
by batumilove · 2026-02-03
79.7%
#14794: fix: parse inline MEDIA: tokens in agent replies
by explainanalyze · 2026-02-12
79.3%
#7400: media: allow temp-dir MEDIA paths for tool outputs
by grammakov · 2026-02-02
77.9%
#19868: fix: prevent media token regex from matching markdown bold text
by sanketgautam · 2026-02-18
77.1%
#19171: fix(feishu): pass mediaLocalRoots to sendMediaFeishu for agent-scop...
by whiskyboy · 2026-02-17
76.8%