Skip to content

Commit d5a4a73

Browse files
committed
fix(ccusage): create tmpdir per-user on *nix systems to allow for multiple concurrent users
1 parent d86c011 commit d5a4a73

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

‎apps/ccusage/src/commands/statusline.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Formatter } from 'picocolors/types';
22
import { mkdirSync } from 'node:fs';
3-
import { tmpdir } from 'node:os';
3+
import { tmpdir, userInfo } from 'node:os';
44
import { join } from 'node:path';
55
import process from 'node:process';
66
import { formatCurrency } from '@ccusage/terminal/table';
@@ -39,8 +39,17 @@ function formatRemainingTime(remaining: number): string {
3939
* Gets semaphore file for session-specific caching and process coordination
4040
* Uses time-based expiry and transcript file modification detection for cache invalidation
4141
*/
42+
function getSemaphoreDirName(uid: number): string {
43+
return uid === -1 ? 'ccusage-semaphore' : `ccusage-semaphore-${uid}`;
44+
}
45+
46+
function getSemaphoreDir(): string {
47+
const { uid } = userInfo();
48+
return join(tmpdir(), getSemaphoreDirName(uid));
49+
}
50+
4251
function getSemaphore(sessionId: string): ReturnType<typeof createLimoJson<SemaphoreType | undefined>> {
43-
const semaphoreDir = join(tmpdir(), 'ccusage-semaphore');
52+
const semaphoreDir = getSemaphoreDir();
4453
const semaphorePath = join(semaphoreDir, `${sessionId}.lock`);
4554

4655
// Ensure semaphore directory exists
@@ -538,3 +547,27 @@ export const statuslineCommand = define({
538547
}
539548
},
540549
});
550+
551+
if (import.meta.vitest != null) {
552+
describe('getSemaphoreDirName', () => {
553+
it('includes uid in directory name on Unix (uid >= 0)', () => {
554+
const result = getSemaphoreDirName(1000);
555+
expect(result).toBe('ccusage-semaphore-1000');
556+
});
557+
558+
it('excludes uid in directory name on Windows (uid === -1)', () => {
559+
const result = getSemaphoreDirName(-1);
560+
expect(result).toBe('ccusage-semaphore');
561+
});
562+
563+
it('handles root uid (0) correctly', () => {
564+
const result = getSemaphoreDirName(0);
565+
expect(result).toBe('ccusage-semaphore-0');
566+
});
567+
568+
it('handles various positive uid values', () => {
569+
expect(getSemaphoreDirName(501)).toBe('ccusage-semaphore-501');
570+
expect(getSemaphoreDirName(65534)).toBe('ccusage-semaphore-65534');
571+
});
572+
});
573+
}

0 commit comments

Comments
 (0)