Skip to content

[bug] syscall.Rmdir is not cross-platform and may lead to unexpected behavior on Windows #1884

@docker-agent

Description

@docker-agent

🟡 medium - bug

File: pkg/tools/builtin/filesystem.go (line 624)

Code

	for _, path := range args.Paths {
		resolvedPath := t.resolvePath(path)

		if err := syscall.Rmdir(resolvedPath); err != nil {
			if errors.Is(err, syscall.ENOTDIR) {
				return tools.ResultError(fmt.Sprintf("Error: %s is not a directory", path)), nil
			}
			return tools.ResultError(fmt.Sprintf("Error removing directory %s: %s", path, err)), nil
		}
		results = append(results, fmt.Sprintf("Directory removed successfully: %s", path))
	}

Problem

The syscall.Rmdir function is a Unix-specific system call. While os.Remove on Go can handle directory removal (if empty) in a cross-platform way, using syscall.Rmdir directly makes this function non-portable to Windows. On Windows, syscall.Rmdir would likely fail with an ENOSYS error or similar, meaning the "remove_directory" tool would not work as expected on Windows systems.

Suggested Fix

Replace syscall.Rmdir with os.Remove(resolvedPath). The os.Remove function is cross-platform and handles both files and empty directories. It returns an error if the directory is not empty, which is consistent with the intent of "remove empty directories".

		if err := os.Remove(resolvedPath); err != nil {
			if errors.Is(err, os.ErrNotExist) { // Handle "not found" explicitly if desired
				return tools.ResultError(fmt.Sprintf("Error: directory %s not found", path)), nil
			}
			if errors.Is(err, os.ErrInvalid) || strings.Contains(err.Error(), "directory not empty") { // Check for non-empty or not a directory
				return tools.ResultError(fmt.Sprintf("Error removing directory %s: %s (directory might not be empty or is not a directory)", path, err)), nil
			}
			return tools.ResultError(fmt.Sprintf("Error removing directory %s: %s", path, err)), nil
		}

Found by nightly codebase scan

Metadata

Metadata

Assignees

No one assigned

    Labels

    automatedIssues created by cagentkind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions