Skip to content

Commit aa8ed53

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): disable glob mounting for patterns that start with a forward slash
By default, a glob pattern starting with a forward slash will be "mounted" onto the system root. This causes globs to escape the workspace root. With this change we configure disable glob "mounting" and also change the root to the same setting of the `cwd`. Closes #23467 (cherry picked from commit 7a24609)
1 parent 8894b84 commit aa8ed53

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

packages/angular_devkit/build_angular/src/builders/karma/find-tests.ts

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ async function findMatchingTests(
6666

6767
return globPromise(normalizedPattern, {
6868
cwd: projectSourceRoot,
69+
root: projectSourceRoot,
70+
nomount: true,
6971
});
7072
}
7173

packages/angular_devkit/build_angular/src/builders/karma/tests/options/code-coverage-exclude_spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
3737
harness.expectFile(coveragePath).content.not.toContain('app.component.ts');
3838
});
3939

40+
it('should exclude file from coverage when set when glob starts with a forward slash', async () => {
41+
harness.useTarget('test', {
42+
...BASE_OPTIONS,
43+
codeCoverage: true,
44+
codeCoverageExclude: ['/**/app.component.ts'],
45+
});
46+
47+
const { result } = await harness.executeOnce();
48+
49+
expect(result?.success).toBeTrue();
50+
51+
await setTimeoutPromise(1000);
52+
harness.expectFile(coveragePath).content.not.toContain('app.component.ts');
53+
});
54+
4055
it('should not exclude file from coverage when set', async () => {
4156
harness.useTarget('test', {
4257
...BASE_OPTIONS,

packages/angular_devkit/build_angular/src/builders/karma/tests/options/include_spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
5454
test: 'glob with spec suffix',
5555
input: ['**/*.pipe.spec.ts', '**/*.pipe.spec.ts', '**/*test.service.spec.ts'],
5656
},
57+
{
58+
test: 'glob with forward slash and spec suffix',
59+
input: ['/**/*test.service.spec.ts'],
60+
},
5761
].forEach((options, index) => {
5862
it(`should work with ${options.test} (${index})`, async () => {
5963
await harness.writeFiles({

packages/angular_devkit/build_angular/src/utils/copy-assets.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
import * as fs from 'fs';
1010
import glob from 'glob';
1111
import * as path from 'path';
12+
import { promisify } from 'util';
1213

13-
function globAsync(pattern: string, options: glob.IOptions) {
14-
return new Promise<string[]>((resolve, reject) =>
15-
glob(pattern, options, (e, m) => (e ? reject(e) : resolve(m))),
16-
);
17-
}
14+
const globPromise = promisify(glob);
1815

1916
export async function copyAssets(
2017
entries: {
@@ -33,10 +30,12 @@ export async function copyAssets(
3330

3431
for (const entry of entries) {
3532
const cwd = path.resolve(root, entry.input);
36-
const files = await globAsync(entry.glob, {
33+
const files = await globPromise(entry.glob, {
3734
cwd,
3835
dot: true,
3936
nodir: true,
37+
root: cwd,
38+
nomount: true,
4039
ignore: entry.ignore ? defaultIgnore.concat(entry.ignore) : defaultIgnore,
4140
follow: entry.followSymlinks,
4241
});

packages/angular_devkit/build_angular/src/webpack/utils/helpers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ export function assetNameTemplateFactory(hashFormat: HashFormat): (resourcePath:
121121
}
122122

123123
export function getInstrumentationExcludedPaths(
124-
sourceRoot: string,
124+
root: string,
125125
excludedPaths: string[],
126126
): Set<string> {
127127
const excluded = new Set<string>();
128128

129129
for (const excludeGlob of excludedPaths) {
130130
glob
131-
.sync(excludeGlob, { nodir: true, cwd: sourceRoot })
132-
.forEach((p) => excluded.add(path.join(sourceRoot, p)));
131+
.sync(excludeGlob, { nodir: true, cwd: root, root, nomount: true })
132+
.forEach((p) => excluded.add(path.join(root, p)));
133133
}
134134

135135
return excluded;

0 commit comments

Comments
 (0)