Skip to content

Commit

Permalink
fix: startsWith cannot determine subdirectories correctly (#4916)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe authored Nov 7, 2023
1 parent 96ede9e commit 7555827
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/shaggy-bears-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/app-tools': patch
---

fix: startsWith cannot determine subdirectories correctly
fix: startsWith 函数不能正确地判断子目录
5 changes: 3 additions & 2 deletions packages/solutions/app-tools/src/analyze/getBundleEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Entrypoint } from '@modern-js/types';
import type { AppNormalizedConfig, IAppContext } from '../types';
import { getFileSystemEntry } from './getFileSystemEntry';
import { JS_EXTENSIONS } from './constants';
import { isSubDirOrEqual } from './utils';

const ensureExtensions = (file: string) => {
if (!path.extname(file)) {
Expand Down Expand Up @@ -38,8 +39,8 @@ const ifAlreadyExists = (
}
// filesystem routes entrypoint conflict with normal entrypoint.
if (
entrypoint.entry.startsWith(checked.entry) ||
checked.entry.startsWith(entrypoint.entry)
isSubDirOrEqual(entrypoint.entry, checked.entry) ||
isSubDirOrEqual(checked.entry, entrypoint.entry)
) {
throw new Error(
`Entry configuration conflicts\n Your configuration: ${checked.entry}.\n Default entrypoint: ${entrypoint.entry}\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`,
Expand Down
10 changes: 10 additions & 0 deletions packages/solutions/app-tools/src/analyze/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ export const checkIsBuildCommands = () => {

return buildCommands.includes(command);
};

export const isSubDirOrEqual = (parent: string, child: string): boolean => {
if (parent === child) {
return true;
}
const relative = path.relative(parent, child);
const isSubdir =
relative && !relative.startsWith('..') && !path.isAbsolute(relative);
return Boolean(isSubdir);
};
28 changes: 28 additions & 0 deletions packages/solutions/app-tools/tests/analyze/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path';
import { isSubDirOrEqual } from '../../src/analyze/utils';

describe('isSubDirOrEqual', () => {
it('should return true for the same directories', () => {
const parent = path.resolve('/Users/test');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(true);
});

it('should return true for a child directory', () => {
const parent = path.resolve('/Users');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(true);
});

it('should return false for a non-child directory', () => {
const parent = path.resolve('/Users/test');
const child = path.resolve('/Users/anotherTest');
expect(isSubDirOrEqual(parent, child)).toBe(false);
});

it('should return false for a directory at a higher level', () => {
const parent = path.resolve('/Users/test/deeper');
const child = path.resolve('/Users/test');
expect(isSubDirOrEqual(parent, child)).toBe(false);
});
});

0 comments on commit 7555827

Please sign in to comment.