fix: improve Windows compatibility for CLI commands and sandbox (#261)

## Fix Windows compatibility issues (#248)

This PR addresses the Windows compatibility issues reported in #248:

1. **Fix sandbox initialization failure on Windows**
- Modified `getSandbox()` to return `SandboxType.NONE` on Windows
instead of throwing an error
- Added a warning log message to inform the user that sandbox is not
available on Windows

2. **Fix Unix commands not working on Windows**
- Created a new module
[platform-commands.ts](cci:7://file:///c:/Users/HP%20840%20G6/workflow/codex/codex-cli/src/utils/agent/platform-commands.ts:0:0-0:0)
that automatically adapts Unix commands to their Windows equivalents
   - Implemented a mapping table for common commands and their options
   - Integrated this functionality into the command execution process

### Testing
Tested on Windows 10 with the following commands:
- `ls -R .` (now automatically translates to `dir /s .`)
- Other Unix commands like `grep`, `cat`, etc.

The CLI no longer crashes when running these commands on Windows.

I have read the CLA Document and I hereby sign the CLA

---------

Signed-off-by: Alpha Diop <alphakhoss@gmail.com>
This commit is contained in:
Alpha Diop
2025-04-17 18:31:19 +00:00
committed by GitHub
parent f9c15523e7
commit 3a71175236
3 changed files with 111 additions and 2 deletions

View File

@@ -0,0 +1,86 @@
/**
* Utility functions for handling platform-specific commands
*/
import { log, isLoggingEnabled } from "./log.js";
/**
* Map of Unix commands to their Windows equivalents
*/
const COMMAND_MAP: Record<string, string> = {
ls: "dir",
grep: "findstr",
cat: "type",
rm: "del",
cp: "copy",
mv: "move",
touch: "echo.>",
mkdir: "md",
};
/**
* Map of common Unix command options to their Windows equivalents
*/
const OPTION_MAP: Record<string, Record<string, string>> = {
ls: {
"-l": "/p",
"-a": "/a",
"-R": "/s",
},
grep: {
"-i": "/i",
"-r": "/s",
},
};
/**
* Adapts a command for the current platform.
* On Windows, this will translate Unix commands to their Windows equivalents.
* On Unix-like systems, this will return the original command.
*
* @param command The command array to adapt
* @returns The adapted command array
*/
export function adaptCommandForPlatform(command: Array<string>): Array<string> {
// If not on Windows, return the original command
if (process.platform !== "win32") {
return command;
}
// Nothing to adapt if the command is empty
if (command.length === 0) {
return command;
}
const cmd = command[0];
// If cmd is undefined or the command doesn't need adaptation, return it as is
if (!cmd || !COMMAND_MAP[cmd]) {
return command;
}
if (isLoggingEnabled()) {
log(`Adapting command '${cmd}' for Windows platform`);
}
// Create a new command array with the adapted command
const adaptedCommand = [...command];
adaptedCommand[0] = COMMAND_MAP[cmd];
// Adapt options if needed
const optionsForCmd = OPTION_MAP[cmd];
if (optionsForCmd) {
for (let i = 1; i < adaptedCommand.length; i++) {
const option = adaptedCommand[i];
if (option && optionsForCmd[option]) {
adaptedCommand[i] = optionsForCmd[option];
}
}
}
if (isLoggingEnabled()) {
log(`Adapted command: ${adaptedCommand.join(" ")}`);
}
return adaptedCommand;
}