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
20 changes: 17 additions & 3 deletions src/utils/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs } from 'fs';
import { promises as fs, constants as fsConstants } from 'fs';
import path from 'path';

function isMarkerOnOwnLine(content: string, markerIndex: number, markerLength: number): boolean {
Expand Down Expand Up @@ -93,10 +93,24 @@ export class FileSystemUtils {
return true;
}

return (stats.mode & 0o222) !== 0;
// On Windows, stats.mode doesn't reliably indicate write permissions.
// Use fs.access with W_OK to check actual write permissions cross-platform.
try {
await fs.access(filePath, fsConstants.W_OK);
return true;
} catch {
return false;
}
} catch (error: any) {
if (error.code === 'ENOENT') {
return true;
// File doesn't exist; check if we can write to the parent directory
const parentDir = path.dirname(filePath);
try {
await fs.access(parentDir, fsConstants.W_OK);
return true;
} catch {
return false;
}
}

console.debug(`Unable to determine write permissions for ${filePath}: ${error.message}`);
Expand Down
15 changes: 12 additions & 3 deletions test/core/completions/installers/zsh-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ describe('ZshInstaller', () => {

it('should handle installation errors gracefully', async () => {
// Create installer with non-existent/invalid home directory
const invalidInstaller = new ZshInstaller('/root/invalid/nonexistent/path');
// Use a path that will fail on both Unix and Windows
const invalidPath = process.platform === 'win32'
? 'Z:\\nonexistent\\invalid\\path' // Non-existent drive letter on Windows
: '/root/invalid/nonexistent/path'; // Permission-denied path on Unix
const invalidInstaller = new ZshInstaller(invalidPath);

const result = await invalidInstaller.install(testScript);

Expand Down Expand Up @@ -503,7 +507,11 @@ describe('ZshInstaller', () => {

it('should handle write permission errors gracefully', async () => {
// Create installer with path that can't be written
const invalidInstaller = new ZshInstaller('/root/invalid/path');
// Use a path that will fail on both Unix and Windows
const invalidPath = process.platform === 'win32'
? 'Z:\\nonexistent\\invalid\\path' // Non-existent drive letter on Windows
: '/root/invalid/path'; // Permission-denied path on Unix
const invalidInstaller = new ZshInstaller(invalidPath);

const result = await invalidInstaller.configureZshrc(completionsDir);

Expand Down Expand Up @@ -641,7 +649,8 @@ describe('ZshInstaller', () => {
if (exists) {
const content = await fs.readFile(zshrcPath, 'utf-8');
expect(content).toContain('fpath=');
expect(content).toContain('custom/completions');
// Check for custom/completions or custom\completions (Windows path separator)
expect(content).toMatch(/custom[/\\]completions/);
}
});

Expand Down
Loading