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

Normalize glob patterns with trailing slashes #2788

Merged
merged 4 commits into from Sep 18, 2021
Merged

Conversation

asaid-0
Copy link
Contributor

@asaid-0 asaid-0 commented Jul 19, 2021

Fixes #2781

@asaid-0
Copy link
Contributor Author

asaid-0 commented Jul 21, 2021

@novemberborn could you please review this

@novemberborn
Copy link
Member

@asaid-0 thanks for the PR, I may have a chance to look at this on the weekend.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

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

@asaid-0 it's been a while since I've had to think about glob patterns and file paths. Why does this solution do the trick?

Comment on lines 120 to 121
function normalizePattern(pattern) {
const cwd = process.cwd();
Copy link
Member

Choose a reason for hiding this comment

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

cwd needs to be an argument here. AVA determines the directory to which patterns are relevant and it may not be the CWD.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thank you for pointing this out, I see now I think we should receive the CWD as an argument in the normalizeGlobs here:

export function normalizeGlobs({extensions, files: filePatterns, ignoredByWatcher: ignoredByWatcherPatterns, providers}) {

Then we pass it to normalizePatterns call in the same function.

Also i noticed this line in cli.js

ava/lib/cli.js

Line 396 in dc93f37

pattern: normalizePattern(path.relative(projectDir, path.resolve(process.cwd(), pattern))),

which using nearly the same function path.relative, but path.resolve is being used in the second parameter to get the absolute path

if (pattern.startsWith('!./')) {
return `!${pattern.slice(3)}`;
if (pattern.startsWith('!')) {
return normalizeNegatedPattern(cwd, pattern);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the helper function is all that useful, especially since it hides the critical path.relative() bit which is then called directly below.

@asaid-0
Copy link
Contributor Author

asaid-0 commented Aug 27, 2021

@asaid-0 it's been a while since I've had to think about glob patterns and file paths. Why does this solution do the trick?

@novemberborn Thank you for your feedback, actually, I used this solution assuming that we should also solve the issue with such patterns tests//test1, tests/sub/../test2 not only the trailing slash tests/test/

if we need to just solve the trailing slash issue I wouldn't use path.relative()
what do you think?

@novemberborn
Copy link
Member

Thank you for your feedback, actually, I used this solution assuming that we should also solve the issue with such patterns tests//test1, tests/sub/../test2 not only the trailing slash tests/test/

if we need to just solve the trailing slash issue I wouldn't use path.relative()
what do you think?

@asaid-0 sorry I lost track of this.

These are glob patterns so I'm not sure how safe it is to pass them to path functions. I don't think we have to worry about those edge cases. Trailing slashes though are easier to type, and it's surprising if that breaks the pattern.

@novemberborn novemberborn changed the title Fix glob patterns with trailing slashes not working Normalize glob patterns with trailing slashes Sep 18, 2021
@novemberborn novemberborn merged commit 6d9c4d7 into avajs:main Sep 18, 2021
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 this pull request may close these issues.

Trailing slashes break directory matching
2 participants