Skip to content

Commit 2efb4d4

Browse files
authored
fix(pack): handle excluding from included parent directory (#4317)
1 parent 6a1600f commit 2efb4d4

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

.yarn/versions/06e43b84.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-pack": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm"
14+
- "@yarnpkg/plugin-npm-cli"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"

packages/acceptance-tests/pkg-tests-specs/sources/commands/pack.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ describe(`Commands`, () => {
142142
}),
143143
);
144144

145+
test(
146+
`it should support excluding patterns in included parent directory from the "files" field`,
147+
makeTemporaryEnv({
148+
files: [
149+
`/lib`,
150+
`!/lib/b.js`,
151+
],
152+
}, async ({path, run, source}) => {
153+
await fsUtils.writeFile(`${path}/lib/a.js`, `module.exports = 42;\n`);
154+
await fsUtils.writeFile(`${path}/lib/b.js`, `module.exports = 42;\n`);
155+
156+
await run(`install`);
157+
158+
const {stdout} = await run(`pack`, `--dry-run`);
159+
expect(stdout).toContain(`lib/a.js`);
160+
expect(stdout).not.toContain(`lib/b.js`);
161+
}),
162+
);
163+
145164
test(
146165
`it should support excluding patterns from the "files" field`,
147166
makeTemporaryEnv({

packages/plugin-pack/sources/packUtils.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,27 +334,38 @@ function addIgnorePattern(target: Array<string>, pattern: string, {cwd}: {cwd: P
334334
target.push(normalizePattern(trimed, {cwd}));
335335
}
336336

337+
enum MatchType {
338+
None,
339+
Match,
340+
NegatedMatch,
341+
}
342+
337343
function isIgnored(cwd: string, {globalList, ignoreLists}: {globalList: IgnoreList, ignoreLists: Array<IgnoreList> | null}) {
338-
if (isMatch(cwd, globalList.accept))
339-
return false;
340-
if (isMatch(cwd, globalList.reject))
341-
return true;
344+
const globalAcceptMatchType = matchPatternType(cwd, globalList.accept);
345+
if (globalAcceptMatchType !== MatchType.None)
346+
return globalAcceptMatchType === MatchType.NegatedMatch;
347+
348+
const globalRejectMatchType = matchPatternType(cwd, globalList.reject);
349+
if (globalRejectMatchType !== MatchType.None)
350+
return globalRejectMatchType === MatchType.Match;
342351

343352
if (ignoreLists !== null) {
344353
for (const ignoreList of ignoreLists) {
345-
if (isMatch(cwd, ignoreList.accept))
346-
return false;
354+
const acceptMatchType = matchPatternType(cwd, ignoreList.accept);
355+
if (acceptMatchType !== MatchType.None)
356+
return acceptMatchType === MatchType.NegatedMatch;
347357

348-
if (isMatch(cwd, ignoreList.reject)) {
349-
return true;
358+
const rejectMatchType = matchPatternType(cwd, ignoreList.reject);
359+
if (rejectMatchType !== MatchType.None) {
360+
return rejectMatchType === MatchType.Match;
350361
}
351362
}
352363
}
353364

354365
return false;
355366
}
356367

357-
function isMatch(path: string, patterns: Array<string>) {
368+
function matchPatternType(path: string, patterns: Array<string>) {
358369
let inclusives = patterns;
359370
const exclusives = [];
360371

@@ -372,11 +383,11 @@ function isMatch(path: string, patterns: Array<string>) {
372383
}
373384

374385
if (isMatchBasename(path, exclusives))
375-
return false;
386+
return MatchType.NegatedMatch;
376387
if (isMatchBasename(path, inclusives))
377-
return true;
388+
return MatchType.Match;
378389

379-
return false;
390+
return MatchType.None;
380391
}
381392

382393
function isMatchBasename(path: string, patterns: Array<string>) {

0 commit comments

Comments
 (0)