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

fix(turbopack-ecmascript): support urlrewritebehavior::full #6413

Merged
merged 2 commits into from
Nov 17, 2023

Conversation

kwonoj
Copy link
Contributor

@kwonoj kwonoj commented Nov 9, 2023

Description

This PR tries to correct the behavior of UrlRewriteBehavior::Full.

With UrlRewriteBehavior::Full, it is expected new URL(url, base) resolves to the absolute full path to the requested url. Previously, this didn't work for 2 reasons

  • replacing url to the emitted output path
  • replacing base to non-relative to the above output path

in result resolving won't give the correct path.

I tried a couple of approaches if it's possible to construct full path directly when there's a referenced asset, but wasn't able to make it work. Instead this PR relies on the new runtime fn wraps __turbopack_require__, calculate the full path with the absolute path runtime already have + resolved asset path. Since emitted asset contains its output with assetPrefix, exports it into the runtime as well to strips it out.

Overall behaviors across different condition are illustarted here:

https://github.com/vercel/turbo/compare/url-full-resolve-context?expand=1#diff-bb24150ae91f0fa7b3001d17e597f89d3e16f5e0f4894acb4bd0499fbcd73793R134

Closes PACK-1924

Copy link

vercel bot commented Nov 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

11 Ignored Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-cra-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-nonmonorepo ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am
turbo-site ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:15am

Copy link
Contributor

github-actions bot commented Nov 9, 2023

✅ This change can build next-swc

Copy link
Contributor

github-actions bot commented Nov 9, 2023

🟢 CI successful 🟢

Thanks

Copy link
Contributor

github-actions bot commented Nov 9, 2023

Linux Benchmark for 10232f6

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 21.68ms ± 0.85ms 21.81ms ± 0.89ms +0.63%
bench_hmr_to_eval/Turbopack CSR/1000 modules 21.39ms ± 0.85ms 21.29ms ± 0.84ms -0.46%
bench_startup/Turbopack CSR/1000 modules 1165.59ms ± 7.98ms 1178.26ms ± 19.42ms +1.09%

Copy link
Contributor

github-actions bot commented Nov 9, 2023

⚠️ Turbopack Benchmark CI failed ⚠️

The following steps have failed in CI:

  • Turbopack Rust benchmark tests (linux)
  • Turbopack Rust benchmark tests (mac/win, non-blocking)

See workflow summary for details

crates/turbopack-ecmascript/src/references/esm/url.rs Outdated Show resolved Hide resolved
├───────────────────────────────┼─────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────┼───────────────────────┤
│ Relative │ new URL(__turbopack_relative_url__(__turbopack_require__(urlId)), base) │ new URL(__turbopack_relative_url__(url), base) │ new URL(url, base) │
│ Full(RenderingClient::Client) │ new URL(__turbopack_require__(urlId), "location.origin") │ new URL(url, "location.origin") │ new URL(url, base) │
│ Full(RenderingClient::..) │ new URL(__turbopack_resolve_module_id_path__(urlId)) │ new URL(url, base) │ new URL(url, base) │
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need another module wrapper function for this? Why can't we derive this from CWD or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have cwd, but that's not composable with relative path url to its output asset.

The simpliest example is like new URL('../../public/img.png', import.meta.url): we have correct import.meta.url points to yhe cwd of the module, but _turbopack_require'd module points to the output distdir's asset with asset prefix so can't relate those two directlt.

This is the actual problem I struggled to solve without these wrapper, if there's a way to get a full phsyical path without asset prefix it'd be great. I feel I may miss something.

Copy link
Contributor

Linux Benchmark for b30a55a

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 23.23ms ± 0.82ms 22.79ms ± 0.97ms -1.91%
bench_hmr_to_eval/Turbopack CSR/1000 modules 22.59ms ± 1.00ms 22.48ms ± 0.97ms -0.46%
bench_startup/Turbopack CSR/1000 modules 1170.79ms ± 5.61ms 1163.40ms ± 7.58ms -0.63%

Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

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

you can add an execution turbopack-test to test this

Copy link
Contributor

Linux Benchmark for 0410e52

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 22.53ms ± 0.95ms 22.51ms ± 0.95ms -0.12%
bench_hmr_to_eval/Turbopack CSR/1000 modules 22.24ms ± 0.83ms 21.97ms ± 0.89ms -1.20%
bench_startup/Turbopack CSR/1000 modules 1158.85ms ± 2.41ms 1154.94ms ± 10.77ms -0.34%

@kwonoj kwonoj force-pushed the url-full-resolve-context branch 2 times, most recently from 63a1305 to 8010a70 Compare November 14, 2023 17:02
@kwonoj
Copy link
Contributor Author

kwonoj commented Nov 14, 2023

@sokra

you can add an execution turbopack-test to test this

Gave a quick peek, and then realized test uses different runtime to the runtime next-swc's turbopack uses, which doesn't have any foundation support such as runtime's absolute path, etcs. I'd like to tackle this as separate PR later as have to port runtime's feature other than my changes.

@sokra
Copy link
Member

sokra commented Nov 14, 2023

Gave a quick peek, and then realized test uses different runtime to the runtime next-swc's turbopack uses, which doesn't have any foundation support such as runtime's absolute path, etcs. I'd like to tackle this as separate PR later as have to port runtime's feature other than my changes.

Fine to me, at least run it against the next.js test case to verify it's working

@kwonoj
Copy link
Contributor Author

kwonoj commented Nov 14, 2023

Fine to me, at least run it against the next.js test case to verify it's working

Yes, as shared in the slack thread vercel/next.js#58404 verifies new url tests are passing (vercel/next.js@4e63956#diff-2731c6687251176e46f1aa219fe2c059c9a13b61f0a5035d9b5073c9fc10258e) . Failed test is flaky I believe.

Copy link
Contributor

Linux Benchmark for 9c4b9bc

Click to view benchmark
Test Base PR % Significant %
bench_hmr_to_commit/Turbopack CSR/1000 modules 21.70ms ± 0.86ms 21.77ms ± 0.86ms +0.32%
bench_hmr_to_eval/Turbopack CSR/1000 modules 21.28ms ± 0.84ms 21.43ms ± 0.87ms +0.68%
bench_startup/Turbopack CSR/1000 modules 1155.75ms ± 4.46ms 1145.05ms ± 7.92ms -0.93%

@kwonoj kwonoj merged commit e26a307 into main Nov 17, 2023
46 of 49 checks passed
@kwonoj kwonoj deleted the url-full-resolve-context branch November 17, 2023 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants