Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/rstack/src/fmt/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
}

const cacheDirPath = cacheLocation ? path.resolve(cwd, cacheLocation) : undefined;
if (cacheDirPath) {
const cacheDirPrefix = cacheDirPath.endsWith(path.sep)
? cacheDirPath
: `${cacheDirPath}${path.sep}`;
if (cwd === cacheDirPath || cwd.startsWith(cacheDirPrefix)) {
Comment thread
chenjiahan marked this conversation as resolved.
throw new Error(
'The --cache-location directory cannot be the current working directory or an ancestor.',
);
}
}

const config = await loadFmtConfig(cwd);
const files = await discoverFmtFiles({
cwd,
Expand Down
10 changes: 10 additions & 0 deletions packages/rstack/tests/cli/fmt/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ test.each(['relative', 'absolute'] as const)('uses a %s custom cache location',
expect(existsSync(path.join(projectPath, '.rstack'))).toBe(false);
});

test.each(['.', '..'])('rejects a custom cache location at %s', (cacheLocation) => {
const result = runFmt(['--cache-location', cacheLocation, '.']);

expect(result.status).toBe(2);
expect(result.stdout).toBe('');
expect(result.stderr).toContain(
'The --cache-location directory cannot be the current working directory or an ancestor.',
);
});

test('excludes the custom cache directory from formatting', () => {
const cacheLocation = 'custom-cache';
writeProjectFile('index.ts', 'const value = 1;\n');
Expand Down