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

Lookup alias and import maps using Pattern instead of String #8852

Closed
wants to merge 33 commits into from

Conversation

mischnic
Copy link
Contributor

@mischnic mischnic commented Jul 26, 2024

The changes I had to make got out of hand quite quickly, because operating on Patterns everywhere is more cumbersome than strings...
Furthermore, some structs contained strings (but unrelated to requests being treated as strings, rather because import maps are specified as strings with wildcards), so now we needed a version with patterns.

This is almost working, namely the dynamic @/* alias is already rewritten correctly now:

//Request:
Pattern::Concatenation([Pattern::Constant("@/messages/"), Pattern::Dynamic, Pattern::Constant(".json")]);

//ImportMap::lookup result:
ReplacedImportMapping::PrimaryAlternative(
    Pattern::Concatenation([Pattern::Constant("./messages/"), Pattern::Dynamic]),
    Some(
        FileSystemPath {
            fs: DiskFileSystem {
                name: "project",
                root: "/Users/niklas/Desktop/issue-turbopack-imports",
            },
            path: ,
        },
    ),
);

Closes PACK-2801
Related to vercel/next.js#63372

Copy link

vercel bot commented Jul 26, 2024

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

Name Status Preview Comments Updated (UTC)
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 30, 2024 5:19pm
rust-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 30, 2024 5:19pm
8 Skipped Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Jul 30, 2024 5:19pm

Copy link
Contributor

github-actions bot commented Jul 26, 2024

🟢 Turbopack Benchmark CI successful 🟢

Thanks

Copy link
Contributor

✅ This change can build next-swc

Copy link
Contributor

github-actions bot commented Jul 26, 2024

⚠️ CI failed ⚠️

The following steps have failed in CI:

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

See workflow summary for details

@mischnic mischnic force-pushed the mischnic/pack-2801-pattern-in-alias-map branch from dd8d510 to cdf9cf7 Compare July 29, 2024 12:34
@mischnic mischnic force-pushed the mischnic/pack-2801-pattern-in-alias-map branch from cdf9cf7 to 5e13760 Compare July 29, 2024 13:50
@mischnic mischnic force-pushed the mischnic/pack-2801-pattern-in-alias-map branch from 74e54a0 to 5c0005f Compare July 29, 2024 15:41
@mischnic mischnic force-pushed the mischnic/pack-2801-pattern-in-alias-map branch from 5c0005f to 4d7072a Compare July 30, 2024 09:05
@mischnic mischnic force-pushed the mischnic/pack-2801-pattern-in-alias-map branch from 4d7072a to c4e171e Compare July 30, 2024 09:57
@mischnic
Copy link
Contributor Author

mischnic commented Jul 30, 2024

So now the output of

    const messages = await import(`@/messages/${locale}.json`);

is

   const messages = await __turbopack_module_context__({
        "./messages/de.json": {
            id: ()=>"[project]/messages/de.json (json, async loader)",
            module: ()=>__turbopack_require__("[project]/messages/de.json (json, async loader)")(__turbopack_import__)
        },
        "./messages/en.json": {
            id: ()=>"[project]/messages/en.json (json, async loader)",
            module: ()=>__turbopack_require__("[project]/messages/en.json (json, async loader)")(__turbopack_import__)
        }
    }).import(`@/messages/${locale}.json`)

which fails because __turbopack_module_context__ receives the alias-resolved paths while the looked-up path still has the @/

But apart from that, this PR is ready

let common_prefixes = self.map.common_prefixes(request.as_bytes());
let common_prefixes = self
.map
.common_prefixes(request.constant_prefix().as_bytes());
Copy link
Member

Choose a reason for hiding this comment

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

This will lead to also returning non-matching results. e. g. Pattern could be "a" | "b" which has no constant_prefix so it would return "c" too.

You probably want:

  • Do multiple lookups internally when you have Alternatives
  • Filter the results by matching with the Pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this doesn't happen, map.common_prefixes("") is just empty. That does align with example on https://docs.rs/patricia_tree/latest/patricia_tree/map/type.PatriciaMap.html#method.common_prefixes

But still, map.lookup(Pattern::Alternative(...)) currently never returns anything even if some/all alternatives match. And even then, just returning enum AliasMatch<R> {Exact(R), Replaced(R)} is wrong anyway because some might be exact and some might be replaced matches

let common_prefixes = self.map.common_prefixes(request.as_bytes());
let common_prefixes = self
.map
.common_prefixes(request.constant_prefix().as_bytes());
Copy link
Member

Choose a reason for hiding this comment

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

Same

@mischnic
Copy link
Contributor Author

mischnic commented Aug 2, 2024

-> vercel/next.js#68274

@mischnic mischnic closed this Aug 2, 2024
@mischnic mischnic deleted the mischnic/pack-2801-pattern-in-alias-map branch August 2, 2024 14:48
mischnic added a commit to vercel/next.js that referenced this pull request Aug 16, 2024
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.

2 participants