Skip to content

Commit

Permalink
fix: correctly discard .gitignore if .npmignore is present out of tree
Browse files Browse the repository at this point in the history
this backports the fix from #108
  • Loading branch information
nlf committed Aug 25, 2022
1 parent a02b875 commit 70662c2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ const strictDefaults = [
const normalizePath = (path) => path.split('\\').join('/')

const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
for (const file of ['.gitignore', '.npmignore']) {
for (const file of ['.npmignore', '.gitignore']) {
try {
const ignoreContent = readFile(join(root, file), { encoding: 'utf8' })
result.push(ignoreContent)
// break the loop immediately after reading, this allows us to prioritize
// the .npmignore and discard the .gitignore if one is present
break
} catch (err) {
// we ignore ENOENT errors completely because we don't care if the file doesn't exist
// but we throw everything else because failing to read a file that does exist is
Expand Down
64 changes: 64 additions & 0 deletions test/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,67 @@ t.test('packing a workspace root does not include children', async (t) => {
'workspaces/foo/package.json',
])
})

t.test('.gitignore is discarded if .npmignore exists outside of tree', async (t) => {
const root = t.testdir({
'package.json': JSON.stringify({
name: 'workspace-root',
version: '1.0.0',
main: 'root.js',
workspaces: ['./workspaces/foo'],
}),
'root.js': `console.log('hello')`,
'.gitignore': 'dont-ignore-me',
'.npmignore': 'only-ignore-me',
'dont-ignore-me': 'should not be ignored',
'only-ignore-me': 'should be ignored',
workspaces: {
'.gitignore': 'dont-ignore-me-either',
'.npmignore': 'ignore-me-also',
'dont-ignore-me': 'should not be ignored',
'dont-ignore-me-either': 'should not be ignored',
'only-ignore-me': 'should be ignored',
'ignore-me-also': 'should be ignored',
foo: {
'package.json': JSON.stringify({
name: 'workspace-child',
version: '1.0.0',
main: 'child.js',
}),
'child.js': `console.log('hello')`,
'dont-ignore-me': 'should not be ignored',
'dont-ignore-me-either': 'should not be ignored',
'only-ignore-me': 'should be ignored',
'ignore-me-also': 'should also be ignored',
},
},
})

const workspacePath = path.join(root, 'workspaces', 'foo')
// this simulates what it looks like when a user does i.e. npm pack -w ./workspaces/foo
const files = await packlist({
path: workspacePath,
prefix: root,
workspaces: [workspacePath],
})
t.same(files, [
'dont-ignore-me',
'dont-ignore-me-either',
'child.js',
'package.json',
])

// here we leave off workspaces to satisfy coverage
const secondFiles = await packlist({
path: workspacePath,
prefix: root,
})
t.same(secondFiles, [
'dont-ignore-me',
'dont-ignore-me-either',
'ignore-me-also',
'only-ignore-me',
'child.js',
'package.json',
])
})

0 comments on commit 70662c2

Please sign in to comment.