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

[plugin-dynamic-import-vars] non-normalized paths no longer work #1791

Closed
bearfriend opened this issue Oct 17, 2024 · 15 comments
Closed

[plugin-dynamic-import-vars] non-normalized paths no longer work #1791

bearfriend opened this issue Oct 17, 2024 · 15 comments

Comments

@bearfriend
Copy link
Contributor

bearfriend commented Oct 17, 2024

Expected Behavior

Imports passed non-normalized paths resolve the bundled file

Actual Behavior

Imports passed non-normalized paths produce a Unknown variable dynamic import error

Additional Information

#1780 breaks any code that doesn't build normalized paths because tinyglobby always outputs normalized glob paths but the plugin doesn't touch the original code.

Given this code:

import(`./../${myFile}.js`)

tinyglobby produces ../*.js -> ../myFile.js-> case '../myFile.js', which now doesn't match ./../myFile.js.

I think the only reasonable options are:

  1. Revert
  2. Ask tinyglobby to make normalization optional (will take time and maybe isn't even feasible/desired)
  3. Normalize the path inside __variableDynamicImportRuntime__ before the switch with path-normalize
@shellscape
Copy link
Collaborator

@benmccann looks like you missed an edge case here. please take a look.

@benmccann
Copy link
Contributor

CC @SuperchupuDev it looks like there's a behavior difference between fast-glob and tinyglobby that has broken things here

@shellscape
Copy link
Collaborator

@benmccann if it's a upstream issue reverting the change until we can get the tinyglobby issue resolved is probably the wise play here.

@SuperchupuDev
Copy link

SuperchupuDev commented Oct 17, 2024

tinyglobby intentionally doesn't change the format of the glob results depending on the pattern provided for better consistency and also due to the way it works (tinyglobby doesn't transform results except on very specific circumstances). i believe vite had to apply a workaround here to make it work, but in theory using path.normalize here should work

@bearfriend
Copy link
Contributor Author

tinyglobby intentionally doesn't change the format of the glob results depending on the pattern provided

What do you mean by "depending on the pattern"? It seems the pattern itself is normalized before generating results, here, from processPattern.

@SuperchupuDev
Copy link

SuperchupuDev commented Oct 17, 2024

that normalizes the pattern to match, not the way the results from the directory crawler are generated. the only case where the patterns can modify the result is if any of the patterns start with ../, but that's just because the crawler can't find any files outside the root without a workaround

the crawler library generates a result that's always the exact same no matter the patterns, and each possible result that's generated is filtered by the (normalized) patterns

if you have a directory that has a/a.txt for example, no matter what pattern you use, the crawler will see that file as a/a.txt when filtering and producing results

@bearfriend
Copy link
Contributor Author

the crawler library generates a result that's always the exact same no matter the patterns, and each possible result is generated is filtered by the (normalized) patterns

So then the issue is with the crawler then. I don't think it's actually an issue, though. Certainly not from its perspective. It might be best to just normalize the paths at runtime in the plugin.

@SuperchupuDev
Copy link

IIRC vite fixed this by appending ./ to all the results if the pattern starts with ./

@benmccann
Copy link
Contributor

Just FYI I am very heads down prepping for Svelte Summit this week. If someone is able to look at this issue that would be great. Otherwise I'll take a look next week

@shellscape
Copy link
Collaborator

Good to know. We'll give it a day to see if anyone can step in and if not I'll revert.

@bearfriend
Copy link
Contributor Author

@SuperchupuDev
That may have worked for them because they could ultimately control what the inputs were, but here it could be anything so there's no static approach that will guarantee we reconstruct the original path. For instance:

// you can imagine this path being constructed by a series of `concats`
import('./${myHost}/./${myProject}/./${myComponent}.js`')

turns into ./*/*/*.js, which results in ./my-host/my-project/my-component.js, so the only way I see to make this work is to run it through a port of the posix normalize method at runtime.

@bearfriend
Copy link
Contributor Author

If we're ok adding path-normalize I can do that work now.

@shellscape
Copy link
Collaborator

@bearfriend not opposed to it in the plugin. we'll want a regression test for this car as well.

@bearfriend
Copy link
Contributor Author

bearfriend commented Oct 18, 2024

@shellscape I believe I have something working that I'm ready to open a PR for, but I'm having an issue when trying to commit. Dependencies are updated, but I don't use pnpm so maybe I'm missing something.

Edit: Got it sorted. I must have had some junk in node_modules from an old branch and maybe accidentally using npm to install.

✔ Preparing...
⚠ Running tasks...
  ❯ Running tasks for *.{ts,js}
    ✖ eslint --cache --fix [FAILED]
  ↓ No staged files match **/(package|tsconfig(.*)?).json [SKIPPED]
  ↓ No staged files match (pnpm-workspace|.github/**/*).{yml,yaml} [SKIPPED]
  ↓ No staged files match ((.github/**/*)|(README|CHANGELOG)|(**/(README|CHANGELOG))).md [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up...

✖ eslint --cache --fix:

Oops! Something went wrong! :(

ESLint: 8.57.1

TypeError: prettier.resolveConfig.sync is not a function

@shellscape
Copy link
Collaborator

Making the decision to revert the changes. The only tangible benefit was a reduction of dependencies, but the juice isn't worth the squeeze.

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

Successfully merging a pull request may close this issue.

4 participants