Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/core/__tests__/common/getFilesList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,41 @@ it('should return files for project root with omitting gitignore', async () => {
)
})

it('should use .gitignore only within the current repository', async () => {
mockFs({
[root]: {
'.git': {},
project: {
'fileA.ts': 'content',
'.gitignore': 'packageA/*',
packageA: {
'.git': {},
'.env': 'content',
'.gitignore': `
.env
`,
src: {
'fileE.json': '',
},
},
},
},
})

const filesList = removeCwd(
await getFilesList({
searchRoot: toPlatformSpecificPath(`${root}/project/packageA`),
}),
)
mockFs.restore()

expect(filesList.sort()).toMatchObject(
[`${root}/project/packageA/src/fileE.json`]
.sort()
.map(toPlatformSpecificPath),
)
Comment on lines +304 to +308

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently, this returns an empty array because the implementation will look at the .gitignore on line 282

})

// We will add option to search in ignored files, so that exception is not needed
it.skip('should return files for project root ignored by parent gitignore, but ignore the nested directories', async () => {
mockFs({
Expand Down Expand Up @@ -321,6 +356,7 @@ it.skip('should return files for project root ignored by parent gitignore, but i
it('should ignore files from parent .gitignore', async () => {
mockFs({
[root]: {
'.git': {},
project: {
'fileA.ts': 'content',
'fileB.js': 'content',
Expand Down
46 changes: 32 additions & 14 deletions packages/core/src/getFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import { promises as fs } from 'fs'

import ignore from 'ignore'
import { asyncFilter, measureStart } from './utils'
import { measureStart } from './utils'
import minimatch from 'minimatch'
import { parseDependencyTree } from 'dpdm/lib/index.js'
import { spawnSync } from 'child_process'
Expand Down Expand Up @@ -179,6 +179,29 @@ export type GetFilesListArgs = {
extensionTester?: RegExp
}

const findRepoRoot = async ({
searchRoot,
fsRoot,
}: {
searchRoot: string
fsRoot: string
}) => {
let currentDir = path.resolve(searchRoot)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure if the code should be defensive - if the user passes a path that doesn't exist this will throw an error

while (currentDir !== fsRoot) {
try {
const gitDir = path.join(currentDir, '.git')
const stat = await fs.lstat(gitDir)
if (stat.isDirectory()) {
return currentDir
}
} catch (_e) {
// .git not found, continue
}
currentDir = path.dirname(currentDir)
}
return searchRoot
}

export const getFilesList = async ({
searchRoot: _searchRoot,
entryPoint = undefined,
Expand All @@ -204,25 +227,20 @@ export const getFilesList = async ({
} else {
const InitialIgnore = ignoreNodeModules ? ['node_modules'] : []

// Get parent to root gitignore
if (!omitGitIgnore) {
const searchRootSegments = searchRoot
.replace(fsRoot, '')
.split(pathSeparatorChar)

const pathSegmentsToSystemRoot = []

for (let i = 0; i < searchRootSegments.length; i++) {
let currentPath = searchRootSegments.slice(0, i).join(pathSeparatorChar)

currentPath = fsRoot + currentPath
const repoRoot = await findRepoRoot({ searchRoot, fsRoot })
const parentPaths = []
let currentPath = searchRoot

pathSegmentsToSystemRoot.push(currentPath)
while (currentPath !== repoRoot) {
parentPaths.push(currentPath)
currentPath = path.dirname(currentPath)
}
parentPaths.push(repoRoot)

const parentDirsIgnore = (
await Promise.all(
pathSegmentsToSystemRoot.map((parentPath) =>
parentPaths.map((parentPath) =>
getGitIgnoreContentForDirectory(parentPath),
),
)
Expand Down