Skip to content

Commit 8df3baa

Browse files
committed
fix: correctly discard .gitignore if .npmignore is present out of tree
this backports the fix from #108
1 parent ecc64cc commit 8df3baa

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ const strictDefaults = [
5151
const normalizePath = (path) => path.split('\\').join('/')
5252

5353
const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
54-
for (const file of ['.gitignore', '.npmignore']) {
54+
for (const file of ['.npmignore', '.gitignore']) {
5555
try {
5656
const ignoreContent = readFile(join(root, file), { encoding: 'utf8' })
5757
result.push(ignoreContent)
58+
// break the loop immediately after reading, this allows us to prioritize
59+
// the .npmignore and discard the .gitignore if one is present
60+
break
5861
} catch (err) {
5962
// we ignore ENOENT errors completely because we don't care if the file doesn't exist
6063
// but we throw everything else because failing to read a file that does exist is

test/workspace.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,67 @@ t.test('packing a workspace root does not include children', async (t) => {
111111
'workspaces/foo/package.json',
112112
])
113113
})
114+
115+
t.test('.gitignore is discarded if .npmignore exists outside of tree', async (t) => {
116+
const root = t.testdir({
117+
'package.json': JSON.stringify({
118+
name: 'workspace-root',
119+
version: '1.0.0',
120+
main: 'root.js',
121+
workspaces: ['./workspaces/foo'],
122+
}),
123+
'root.js': `console.log('hello')`,
124+
'.gitignore': 'dont-ignore-me',
125+
'.npmignore': 'only-ignore-me',
126+
'dont-ignore-me': 'should not be ignored',
127+
'only-ignore-me': 'should be ignored',
128+
workspaces: {
129+
'.gitignore': 'dont-ignore-me-either',
130+
'.npmignore': 'ignore-me-also',
131+
'dont-ignore-me': 'should not be ignored',
132+
'dont-ignore-me-either': 'should not be ignored',
133+
'only-ignore-me': 'should be ignored',
134+
'ignore-me-also': 'should be ignored',
135+
foo: {
136+
'package.json': JSON.stringify({
137+
name: 'workspace-child',
138+
version: '1.0.0',
139+
main: 'child.js',
140+
}),
141+
'child.js': `console.log('hello')`,
142+
'dont-ignore-me': 'should not be ignored',
143+
'dont-ignore-me-either': 'should not be ignored',
144+
'only-ignore-me': 'should be ignored',
145+
'ignore-me-also': 'should also be ignored',
146+
},
147+
},
148+
})
149+
150+
const workspacePath = path.join(root, 'workspaces', 'foo')
151+
// this simulates what it looks like when a user does i.e. npm pack -w ./workspaces/foo
152+
const files = await packlist({
153+
path: workspacePath,
154+
prefix: root,
155+
workspaces: [workspacePath],
156+
})
157+
t.same(files, [
158+
'dont-ignore-me',
159+
'dont-ignore-me-either',
160+
'child.js',
161+
'package.json',
162+
])
163+
164+
// here we leave off workspaces to satisfy coverage
165+
const secondFiles = await packlist({
166+
path: workspacePath,
167+
prefix: root,
168+
})
169+
t.same(secondFiles, [
170+
'dont-ignore-me',
171+
'dont-ignore-me-either',
172+
'ignore-me-also',
173+
'only-ignore-me',
174+
'child.js',
175+
'package.json',
176+
])
177+
})

0 commit comments

Comments
 (0)