Skip to content

Commit 9ccac48

Browse files
rhendricarcanis
authored andcommitted
fix(pack): make globs match dotfiles (#4956)
This makes globs in the files array in package.json and in .*ignore files match dotfiles, consistent with npm's behavior and how git processes .gitignore lines.
1 parent 91253c8 commit 9ccac48

File tree

7 files changed

+26
-2
lines changed

7 files changed

+26
-2
lines changed

__tests__/commands/pack.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ test.skip('pack should include bundled dependencies', (): Promise<void> => {
164164
expect(files.sort()).toEqual(expected.sort());
165165
});
166166
});
167+
168+
test.concurrent('pack should match dotfiles with globs', (): Promise<void> => {
169+
return runPack([], {}, 'glob-dotfile', async (config): Promise<void> => {
170+
const {cwd} = config;
171+
const files = await getFilesFromArchive(
172+
path.join(cwd, 'glob-dotfile-v1.0.0.tgz'),
173+
path.join(cwd, 'glob-dotfile-v1.0.0'),
174+
);
175+
const expected = ['index.js', 'package.json', 'dir', path.join('dir', '.dotfile')];
176+
expect(files.sort()).toEqual(expected.sort());
177+
});
178+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* @flow */
2+
console.log('hello world');

__tests__/fixtures/pack/glob-dotfile/dir/.dotfile

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* @flow */
2+
console.log('hello world');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "glob-dotfile",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"files": ["index.js", "dir/*"]
7+
}

src/cli/commands/pack.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export async function packTarball(
7575
if (onlyFiles) {
7676
let lines = [
7777
'*', // ignore all files except those that are explicitly included with a negation filter
78-
'.*', // files with "." as first character have to be excluded explicitly
7978
];
8079
lines = lines.concat(
8180
onlyFiles.map((filename: string): string => `!${filename}`),

src/util/filter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export function matchesFilter(filter: IgnoreFilter, basename: string, loc: strin
9898
if (filter.base && filter.base !== '.') {
9999
loc = path.relative(filter.base, loc);
100100
}
101+
// the micromatch regex expects unix path separators
102+
loc = loc.replace('\\', '/');
101103
return (
102104
filter.regex.test(loc) ||
103105
filter.regex.test(`/${loc}`) ||
@@ -129,7 +131,7 @@ export function ignoreLinesToRegex(lines: Array<string>, base: string = '.'): Ar
129131
// remove trailing slash
130132
pattern = removeSuffix(pattern, '/');
131133

132-
const regex: ?RegExp = mm.makeRe(pattern.trim(), {nocase: true});
134+
const regex: ?RegExp = mm.makeRe(pattern.trim(), {dot: true, nocase: true});
133135

134136
if (regex) {
135137
return {

0 commit comments

Comments
 (0)