Skip to content

Commit

Permalink
fix(es): Improve sourcemap url error messages. (#9422)
Browse files Browse the repository at this point in the history
**Description:**

Parse based on the provided prefix: inline urls must start with "data:",
so only trying one will ensure the correct and more helpful error
message will be shown.

Currently, any error while trying to load a sourcemap file will give the
unhelpful error:

```
  ERROR  failed to read input source map: failed to parse inline source map url
index.js.map

 Caused by:
     relative URL without a base
     at <snip>/swc-<ver>/src/lib.rs:386
```

Further, for the common "missing file" error, give a better message that
hopefully describes the two paths tried, rather than increasing
confusion by talking about a `.js.map.map` file if it was correct.

Now it will show, for example:

```
  ERROR  failed to read input source map: failed to find input source map file "index.js.map" in "file:///D:/github/skilitics/billing/api-client/lib/services/index.js" file as either "file:///D:/github/skilitics/billing/api-client/lib/services\\index.js.map" or with appended .map
    at D:\github\swc-project\swc\crates\swc\src\lib.rs:400
```

(This example appears to be an issue in swc-node, where it is passing a
file URL which is treated as a path, and therefore never exists, I'm
working on a PR for there too)

There are more improvements that could make this code clearer and more
reliable, but this should resolve many of the confused users (including
me!)

**Related issue:**

See (maybe can be considered to fix): #8944, #8910

[PR in swc-node to fix the originating
issue](swc-project/swc-node#840)
  • Loading branch information
simonbuchan authored Aug 14, 2024
1 parent 55f7268 commit 230d1d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .changeset/sweet-birds-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc: patch
swc_core: patch
---

fix(common): Better sourcemap url error messages.
73 changes: 32 additions & 41 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,38 +256,27 @@ impl Compiler {
let name = &fm.name;

let read_inline_sourcemap =
|data_url: Option<&str>| -> Result<Option<sourcemap::SourceMap>, Error> {
match data_url {
Some(data_url) => {
let url = Url::parse(data_url).with_context(|| {
format!("failed to parse inline source map url\n{}", data_url)
})?;
|data_url: &str| -> Result<Option<sourcemap::SourceMap>, Error> {
let url = Url::parse(data_url).with_context(|| {
format!("failed to parse inline source map url\n{}", data_url)
})?;

let idx = match url.path().find("base64,") {
Some(v) => v,
None => {
bail!(
"failed to parse inline source map: not base64: {:?}",
url
)
}
};
let idx = match url.path().find("base64,") {
Some(v) => v,
None => {
bail!("failed to parse inline source map: not base64: {:?}", url)
}
};

let content = url.path()[idx + "base64,".len()..].trim();
let content = url.path()[idx + "base64,".len()..].trim();

let res = BASE64_STANDARD
.decode(content.as_bytes())
.context("failed to decode base64-encoded source map")?;
let res = BASE64_STANDARD
.decode(content.as_bytes())
.context("failed to decode base64-encoded source map")?;

Ok(Some(sourcemap::SourceMap::from_slice(&res).context(
"failed to read input source map from inlined base64 encoded \
string",
)?))
}
None => {
bail!("failed to parse inline source map: `sourceMappingURL` not found")
}
}
Ok(Some(sourcemap::SourceMap::from_slice(&res).context(
"failed to read input source map from inlined base64 encoded string",
)?))
};

let read_file_sourcemap =
Expand All @@ -312,14 +301,17 @@ impl Compiler {
// code.
// Map files are for internal troubleshooting
// convenience.
map_path =
let fallback_map_path =
PathBuf::from(format!("{}.map", filename.display()));
if !map_path.exists() {
if fallback_map_path.exists() {
map_path = fallback_map_path;
} else {
bail!(
"failed to find input source map file {:?} in \
{:?} file",
{:?} file as either {:?} or with appended .map",
data_url,
filename.display(),
map_path.display(),
filename.display()
)
}
}
Expand Down Expand Up @@ -397,17 +389,16 @@ impl Compiler {
Some(url.trim())
});

match read_inline_sourcemap(text) {
// Load original source map if possible
let result = match text {
Some(text) if text.starts_with("data:") => read_inline_sourcemap(text),
_ => read_file_sourcemap(text),
};
match result {
Ok(r) => r,
Err(err) => {
// Load original source map if possible
match read_file_sourcemap(text) {
Ok(v) => v,
Err(_) => {
tracing::error!("failed to read input source map: {:?}", err);
None
}
}
tracing::error!("failed to read input source map: {:?}", err);
None
}
}
};
Expand Down

0 comments on commit 230d1d9

Please sign in to comment.