Skip to content

Commit 4afcda5

Browse files
committedJan 20, 2022
fix: always search config from cwd first
1 parent 36b9546 commit 4afcda5

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed
 

‎lib/getConfigGroups.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import { validateConfig } from './validateConfig.js'
1313
* @param {Object} [options.configObject] - Explicit config object from the js API
1414
* @param {string} [options.configPath] - Explicit path to a config file
1515
* @param {string} [options.cwd] - Current working directory
16+
* @param {string} [options.files] - List of staged files
1617
* @param {Logger} logger
1718
*/
18-
export const getConfigGroups = async ({ configObject, configPath, files }, logger = console) => {
19+
export const getConfigGroups = async (
20+
{ configObject, configPath, cwd, files },
21+
logger = console
22+
) => {
1923
// Return explicit config object from js API
2024
if (configObject) {
2125
const config = validateConfig(configObject, 'config object', logger)
@@ -52,23 +56,24 @@ export const getConfigGroups = async ({ configObject, configPath, files }, logge
5256
// { '.lintstagedrc.json': { config: {...}, files: [...] } }
5357
const configGroups = {}
5458

55-
await Promise.all(
56-
Object.entries(filesByDir).map(([dir, files]) => {
57-
// Discover config from the base directory of the file
58-
return loadConfig({ cwd: dir }, logger).then(({ config, filepath }) => {
59-
if (!config) return
60-
61-
if (filepath in configGroups) {
62-
// Re-use cached config and skip validation
63-
configGroups[filepath].files.push(...files)
64-
return
65-
}
66-
67-
const validatedConfig = validateConfig(config, filepath, logger)
68-
configGroups[filepath] = { config: validatedConfig, files }
69-
})
70-
})
71-
)
59+
const searchConfig = async (cwd, files = []) => {
60+
const { config, filepath } = await loadConfig({ cwd }, logger)
61+
if (!config) return
62+
63+
if (filepath in configGroups) {
64+
// Re-use cached config and skip validation
65+
configGroups[filepath].files.push(...files)
66+
} else {
67+
const validatedConfig = validateConfig(config, filepath, logger)
68+
configGroups[filepath] = { config: validatedConfig, files }
69+
}
70+
}
71+
72+
// Start by searching from cwd
73+
await searchConfig(cwd)
74+
75+
// Discover configs from the base directory of each file
76+
await Promise.all(Object.entries(filesByDir).map(([dir, files]) => searchConfig(dir, files)))
7277

7378
// Throw if no configurations were found
7479
if (Object.keys(configGroups).length === 0) {

‎lib/runAll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const runAll = async (
114114
return ctx
115115
}
116116

117-
const configGroups = await getConfigGroups({ configObject, configPath, files }, logger)
117+
const configGroups = await getConfigGroups({ configObject, configPath, cwd, files }, logger)
118118

119119
// lint-staged 10 will automatically add modifications to index
120120
// Warn user when their command includes `git add`

‎test/getConfigGroups.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ describe('getConfigGroups', () => {
3737
})
3838

3939
it('should find config files for all staged files', async () => {
40+
// Base cwd
41+
loadConfig.mockResolvedValueOnce({ config, filepath: '/.lintstagedrc.json' })
4042
// '/foo.js' and '/bar.js'
4143
loadConfig.mockResolvedValueOnce({ config, filepath: '/.lintstagedrc.json' })
4244
// '/deeper/foo.js'
@@ -55,6 +57,8 @@ describe('getConfigGroups', () => {
5557
})
5658

5759
it('should find config for one file, and not care about other', async () => {
60+
// Base cwd
61+
loadConfig.mockResolvedValueOnce({})
5862
// '/foo.js'
5963
loadConfig.mockResolvedValueOnce({})
6064
// '/deeper/foo.js'

‎test/integration.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,35 @@ describe('lint-staged', () => {
11471147
// 'a/very/deep/file/path/file.js' matched '.lintstagedrc.json'
11481148
expect(await readFile('a/very/deep/file/path/file.js')).toMatch('level-0')
11491149
})
1150+
1151+
it('should not care about staged file outside current cwd with another staged file', async () => {
1152+
await writeFile('file.js', testJsFileUgly)
1153+
await writeFile('deeper/file.js', testJsFileUgly)
1154+
await writeFile('deeper/.lintstagedrc.json', JSON.stringify(fixJsConfig.config))
1155+
await execGit(['add', '.'])
1156+
1157+
// Run lint-staged in "deeper/""
1158+
expect(await gitCommit({ cwd: path.join(cwd, 'deeper') })).resolves
1159+
1160+
// File inside deeper/ was fixed
1161+
expect(await readFile('deeper/file.js')).toEqual(testJsFilePretty)
1162+
// ...but file outside was not
1163+
expect(await readFile('file.js')).toEqual(testJsFileUgly)
1164+
})
1165+
1166+
it('should not care about staged file outside current cwd without any other staged files', async () => {
1167+
await writeFile('file.js', testJsFileUgly)
1168+
await writeFile('deeper/.lintstagedrc.json', JSON.stringify(fixJsConfig.config))
1169+
await execGit(['add', '.'])
1170+
1171+
// Run lint-staged in "deeper/""
1172+
expect(await gitCommit({ cwd: path.join(cwd, 'deeper') })).resolves
1173+
1174+
expect(console.printHistory()).toMatch('No staged files match any configured task')
1175+
1176+
// File outside deeper/ was not fixed
1177+
expect(await readFile('file.js')).toEqual(testJsFileUgly)
1178+
})
11501179
})
11511180

11521181
describe('lintStaged', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.