Skip to content

Commit 3a6d97a

Browse files
fixup! fix(@angular/build): prevent deleting parent directories of project root
1 parent 9d3f5d4 commit 3a6d97a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

packages/angular/build/src/utils/delete-output-dir.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { readdir, rm } from 'node:fs/promises';
10-
import { isAbsolute, join, relative, resolve } from 'node:path';
10+
import { isAbsolute, join, resolve, sep } from 'node:path';
1111

1212
/**
1313
* Delete an output directory, but error out if it's the root of the project.
@@ -18,12 +18,13 @@ export async function deleteOutputDir(
1818
emptyOnlyDirectories?: string[],
1919
): Promise<void> {
2020
const resolvedOutputPath = resolve(root, outputPath);
21-
const relativePath = relative(resolvedOutputPath, root);
22-
// When relative() returns an absolute path, the paths are on different drives/roots
23-
// (e.g. Windows drive letters or UNC paths), so it cannot be an ancestor.
24-
if (!relativePath || (!isAbsolute(relativePath) && !relativePath.startsWith('..'))) {
21+
if (resolvedOutputPath === root) {
22+
throw new Error('Output path MUST not be the workspace root directory.');
23+
}
24+
25+
if (!isAbsolute(outputPath) && !resolvedOutputPath.startsWith(root + sep)) {
2526
throw new Error(
26-
`Output path "${resolvedOutputPath}" MUST not be the project root directory or its parent.`,
27+
`Output path "${resolvedOutputPath}" MUST not be a parent of the workspace root directory.`,
2728
);
2829
}
2930

packages/angular/build/src/utils/delete-output-dir_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ describe('deleteOutputDir', () => {
2020

2121
it('should throw when output path is the project root', async () => {
2222
await expectAsync(deleteOutputDir(root, '.')).toBeRejectedWithError(
23-
/MUST not be the project root directory or its parent/,
23+
/MUST not be the workspace root directory/,
2424
);
2525
});
2626

2727
it('should throw when output path is a parent of the project root', async () => {
2828
await expectAsync(deleteOutputDir(root, '..')).toBeRejectedWithError(
29-
/MUST not be the project root directory or its parent/,
29+
/MUST not be a parent of the workspace root directory/,
3030
);
3131
});
3232

3333
it('should throw when output path is a grandparent of the project root', async () => {
3434
await expectAsync(deleteOutputDir(root, '../..')).toBeRejectedWithError(
35-
/MUST not be the project root directory or its parent/,
35+
/MUST not be a parent of the workspace root directory/,
3636
);
3737
});
3838

0 commit comments

Comments
 (0)