-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Fix crash during upgrade when content globs escape root of project #14896
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f485345
add failing test
RobinMalfait bfcef20
add `braces` as a dependency
RobinMalfait 722fe77
add `hoistStaticGlobParts`
RobinMalfait 346f5e1
update changelog
RobinMalfait 17c7bda
Update packages/@tailwindcss-upgrade/src/utils/hoist-static-glob-part…
RobinMalfait a7fae59
update integration test with new sort order
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add
hoistStaticGlobParts
This is an implementation similar to what we already have in Rust. The idea is that we want to move the static parts to the `base` path. This allows us to use `..` in the pattern and the glob would be adjusted correctly. Without this, globby will error if your pattern escapes the base path by using `..` in the pattern.
- Loading branch information
commit 722fe77c8ff84fd1884c5929d6ca9ce5eb7de65c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/@tailwindcss-upgrade/src/utils/hoist-static-glob-parts.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { expect, it } from 'vitest' | ||
| import { hoistStaticGlobParts } from './hoist-static-glob-parts' | ||
|
|
||
| it.each([ | ||
| // A basic glob | ||
| [ | ||
| { base: '/projects/project-a', pattern: './src/**/*.html' }, | ||
| [{ base: '/projects/project-a/src', pattern: '**/*.html' }], | ||
| ], | ||
|
|
||
| // A glob pointing to a folder should result in `**/*` | ||
| [ | ||
| { base: '/projects/project-a', pattern: './src' }, | ||
| [{ base: '/projects/project-a/src', pattern: '**/*' }], | ||
| ], | ||
|
|
||
| // A glob pointing to a file, should result in the file as the pattern | ||
| [ | ||
| { base: '/projects/project-a', pattern: './src/index.html' }, | ||
| [{ base: '/projects/project-a/src', pattern: 'index.html' }], | ||
| ], | ||
|
|
||
| // A glob going up a directory, should result in the new directory as the base | ||
| [ | ||
| { base: '/projects/project-a', pattern: '../project-b/src/**/*.html' }, | ||
| [{ base: '/projects/project-b/src', pattern: '**/*.html' }], | ||
| ], | ||
|
|
||
| // A glob with curlies, should be expanded to multiple globs | ||
| [ | ||
| { base: '/projects/project-a', pattern: '../project-{b,c}/src/**/*.html' }, | ||
| [ | ||
| { base: '/projects/project-b/src', pattern: '**/*.html' }, | ||
| { base: '/projects/project-c/src', pattern: '**/*.html' }, | ||
| ], | ||
| ], | ||
| [ | ||
| { base: '/projects/project-a', pattern: '../project-{b,c}/src/**/*.{js,html}' }, | ||
| [ | ||
| { base: '/projects/project-b/src', pattern: '**/*.js' }, | ||
| { base: '/projects/project-b/src', pattern: '**/*.html' }, | ||
| { base: '/projects/project-c/src', pattern: '**/*.js' }, | ||
| { base: '/projects/project-c/src', pattern: '**/*.html' }, | ||
| ], | ||
| ], | ||
| ])('should hoist the static parts of the glob: %s', (input, output) => { | ||
| expect(hoistStaticGlobParts(input)).toEqual(output) | ||
| }) |
78 changes: 78 additions & 0 deletions
78
packages/@tailwindcss-upgrade/src/utils/hoist-static-glob-parts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import braces from 'braces' | ||
| import path from 'node:path' | ||
|
|
||
| interface GlobEntry { | ||
| base: string | ||
| pattern: string | ||
| } | ||
|
|
||
| export function hoistStaticGlobParts(entry: GlobEntry): GlobEntry[] { | ||
| return braces(entry.pattern, { expand: true }).map((pattern) => { | ||
| let clone = { ...entry } | ||
| let [staticPart, dynamicPart] = splitPattern(pattern) | ||
|
|
||
| // Move static part into the `base`. | ||
| if (staticPart !== null) { | ||
| clone.base = path.resolve(entry.base, staticPart) | ||
| } else { | ||
| clone.base = path.resolve(entry.base) | ||
| } | ||
|
|
||
| // Move dynamic part into the `pattern`. | ||
| if (dynamicPart === null) { | ||
| clone.pattern = '**/*' | ||
| } else { | ||
| clone.pattern = dynamicPart | ||
| } | ||
|
|
||
| // If the pattern looks like a file, move the file name from the `base` to | ||
| // the `pattern`. | ||
| let file = path.basename(clone.base) | ||
| if (file.includes('.')) { | ||
| clone.pattern = file | ||
| clone.base = path.dirname(clone.base) | ||
| } | ||
|
|
||
| return clone | ||
| }) | ||
| } | ||
|
|
||
| // Split a glob pattern into a `static` and `dynamic` part. | ||
| // | ||
| // Assumption: we assume that all globs are expanded, which means that the only | ||
| // dynamic parts are using `*`. | ||
| // | ||
| // E.g.: | ||
| // Original input: `../project-b/**/*.{html,js}` | ||
| // Expanded input: `../project-b/**/*.html` & `../project-b/**/*.js` | ||
| // Split on first input: ("../project-b", "**/*.html") | ||
| // Split on second input: ("../project-b", "**/*.js") | ||
| function splitPattern(pattern: string): [staticPart: string | null, dynamicPart: string | null] { | ||
| // No dynamic parts, so we can just return the input as-is. | ||
| if (!pattern.includes('*')) { | ||
| return [pattern, null] | ||
| } | ||
|
|
||
| let lastSlashPosition: number | null = null | ||
|
|
||
| for (let [i, c] of pattern.split('').entries()) { | ||
| if (c === '/') { | ||
| lastSlashPosition = i | ||
| } | ||
|
|
||
| if (c === '*' || c === '!') { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // Very first character is a `*`, therefore there is no static part, only a | ||
| // dynamic part. | ||
| if (lastSlashPosition === null) { | ||
| return [null, pattern] | ||
| } | ||
|
|
||
| let staticPart = pattern.slice(0, lastSlashPosition).trim() | ||
| let dynamicPart = pattern.slice(lastSlashPosition + 1).trim() | ||
|
|
||
| return [staticPart || null, dynamicPart || null] | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.