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(dynamicImportVars): correct glob pattern for paths with parentheses #17940

Merged
merged 5 commits into from
Sep 9, 2024

Conversation

sunnylost
Copy link
Contributor

@sunnylost sunnylost commented Aug 24, 2024

Description

fixes #11824

In the transformDynamicImport method, the newRawPattern is generated using posix.relative(from, to). Since to is generated through toAbsoluteGlob(), if it contains parentheses, they will be escaped, resulting in an incorrect relative path when executed.

For example:

const from = 'src/routes/(app)/dir-1/[slug]'
const to = 'src/routes/\\(app\\)/dir-1/${params.slug}.svelte'

posix.relative(from, to) === '../../../\(app\)/dir-1/${params.slug}.svelte' // error
posix.relative(from, 'src/routes/(app)/dir-1/${params.slug}.svelte') === '../${params.slug}.svelte' // right

Since both parameters are valid paths when calling this method, parentheses should be allowed.

Copy link

stackblitz bot commented Aug 24, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

The fix looks great 👍 Can we also add a test for this in playground/dynamic-import?

@bluwy bluwy changed the title fix: Correct glob pattern handling for folder names with parentheses fix(dynamicImportVars): correct glob pattern for paths with parentheses Sep 1, 2024
@sunnylost
Copy link
Contributor Author

@bluwy Hi, I ran the tests on my Mac, and they all passed. I'm not sure why there's a failing on CI: Windows.

@sapphi-red
Copy link
Member

I'm not sure why there's a failing on CI: Windows.

It seems there's two dist/assets/index-hash.js and the test is not picking up the right one for the failing test. Changing the file name of playground/dynamic-import/(app)/index.js to something else like playground/dynamic-import/(app)/main.js should fix the fail.

Comment on lines 151 to 153
normalizeGlobPattern(
await toAbsoluteGlob(rawPattern, root, importer, resolve),
),
Copy link
Member

Choose a reason for hiding this comment

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

Given that the problem is that toAbsoluteGlob escaping the characters unnecessary, how about not using that function?
Checking the toAbsoluteGlob function, most of the code is handling about globs and escaping the paths. I think it would be trimmed down to:

// replace `normalizeGlobPattern` + `toAbsoluteGlob` with
const dir = importer ? dirname(importer) : root
const normalized = rawPattern[0] === '/' ? posix.join(root, rawPattern.slice(1)) : posix.join(dir, rawPattern)

// pass it to `newRawPattern`
let newRawPattern = posix.relative(posix.dirname(importer), normalized)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'll fix this.

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Sep 9, 2024
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thank you!

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

@patak-dev
Copy link
Member

/ecosystem-ci run redwoodjs

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on e80a049: Open

redwoodjs

@patak-dev patak-dev merged commit 2a391a7 into vitejs:main Sep 9, 2024
12 checks passed
@sunnylost sunnylost deleted the fix/normalize-glob-pattern branch September 9, 2024 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic import fails in route with brackets or parenthesis
4 participants