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

SourceMapBuilder behaves wrongly if I add same source multiple times #89

Closed
kdy1 opened this issue Jun 14, 2024 · 8 comments
Closed

SourceMapBuilder behaves wrongly if I add same source multiple times #89

kdy1 opened this issue Jun 14, 2024 · 8 comments
Assignees

Comments

@kdy1
Copy link
Contributor

kdy1 commented Jun 14, 2024

Environment

Context: swc-project/swc#8789

What version are you running: latest

Steps to Reproduce

  1. What
  2. you
  3. did.

I'm trying to accept https://github.com/noyobo/swc-issues/blob/676db973ad2d5085adbb9e46188ae5c8a2a2a935/a.js.map as inputSourceMap for SWC.

But

        let mut builder = SourceMapBuilder::new(None);

        let mut src_id = 0u32;

        if let Some(orig) = orig {
            dbg!(orig.sources().count());
            for src in orig.sources() {
                let id = builder.add_source(src);
                if src_id != 0 && src_id >= id {
                    panic!("source id is not unique: {}", id);
                }
                dbg!(id);
                if id == 192 {
                    dbg!(src);
                }

                src_id = id;

                builder.set_source_contents(id, orig.get_source_contents(id));
            }
        }

this code fails with

[crates/swc_common/src/source_map.rs:1231:21] src = "/Users/noyobo/home/gitlab/tuya-public/illuminate-base-mp/dist/../dist/src/components.ts"
[crates/swc_common/src/source_map.rs:1229:17] id = 193
[crates/swc_common/src/source_map.rs:1229:17] id = 194
[crates/swc_common/src/source_map.rs:1229:17] id = 195
[crates/swc_common/src/source_map.rs:1229:17] id = 196
[crates/swc_common/src/source_map.rs:1229:17] id = 197
[crates/swc_common/src/source_map.rs:1229:17] id = 198
[crates/swc_common/src/source_map.rs:1229:17] id = 199
[crates/swc_common/src/source_map.rs:1229:17] id = 200
[crates/swc_common/src/source_map.rs:1229:17] id = 201
thread 'fixture_tests__fixture__issues_8xxx__8789__input' panicked at /Users/kdy1/projects/s/issues/crates/swc_common/src/source_map.rs:1227:21:
source id is not unique: 192

because SourceMapBuilder uses HashMap for sources internally.

Expected Result

SourceMapBuilder.add_sources should return a different value for the same input, if it's call multiple times.

Actual Result

192 is returned multiple times, even though the original source map uses both 202 and 192 for source_id and the number of total sources is 300. So the tokens uses 0299, but source_id is 0298.

@loewenheim loewenheim self-assigned this Jun 14, 2024
@loewenheim
Copy link
Contributor

If I understand correctly, you are reconstructing an existing sourcemap with SourcemapBuilder, right? Is there a reason you can't parse the existing sourcemap?

@kdy1
Copy link
Contributor Author

kdy1 commented Jun 14, 2024

I need to create a new source map based on the provided source map. Is there a way to apply remapping of source map without adding them to the SourceMapBuilder for the final sourcemap?

@loewenheim
Copy link
Contributor

What do you mean by remapping? Would it be correct to say that you want to take an existing sourcemap and edit it somehow?

@kdy1
Copy link
Contributor Author

kdy1 commented Jun 14, 2024

I found adjust_mapping. Sorry for the noise, I'll try it first.

@loewenheim
Copy link
Contributor

Not at all, please let me know if I can answer any more questions.

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 19, 2024

I tried using adjust_mapping with swc-project/swc#9052 but it has a bug so I reverted it with swc-project/swc#9437

@kdy1
Copy link
Contributor Author

kdy1 commented Aug 19, 2024

Oh, I should reversing the source map order. I'll report back.

@loewenheim
Copy link
Contributor

Closing this for now, please let us know if we can assist you further.

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

No branches or pull requests

2 participants