Skip to content

fix(model): use user shell for ccusage command execution#128

Merged
AnnatarHe merged 1 commit intomainfrom
claude/issue-127-20251005-0754
Oct 5, 2025
Merged

fix(model): use user shell for ccusage command execution#128
AnnatarHe merged 1 commit intomainfrom
claude/issue-127-20251005-0754

Conversation

@AnnatarHe
Copy link
Copy Markdown
Contributor

Changed npx/bunx command execution to use the user's shell environment instead of minimal Go environment. This ensures proper PATH and environment variables are available for node package managers.

Fixes #127

🤖 Generated with Claude Code

Changed npx/bunx command execution to use the user's shell environment
instead of minimal Go environment. This ensures proper PATH and
environment variables are available for node package managers.

- Added getUserShell() to detect user's shell from $SHELL with fallbacks
- Added shellEscapeArgs() for safe shell argument escaping
- Modified exec.CommandContext to use shell -c for command execution

Fixes #127

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Le He <AnnatarHe@users.noreply.github.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @AnnatarHe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where npx and bunx commands were failing to execute correctly due to an insufficient environment. By routing these commands through the user's default shell, the application now properly inherits necessary environment variables and PATH configurations, significantly improving the reliability of node package manager operations.

Highlights

  • Command Execution Environment: The execution of npx and bunx commands has been updated to utilize the user's shell environment instead of a minimal Go environment. This ensures that commands inherit the correct PATH and other environment variables.
  • Shell Detection: A new getUserShell function has been introduced to dynamically determine the appropriate shell for command execution, checking the SHELL environment variable first and falling back to platform-specific defaults (e.g., PowerShell/cmd on Windows, /bin/sh on Unix-like systems).
  • Argument Escaping: A shellEscapeArgs helper function was added to safely escape command arguments, preventing issues when passing them to the shell for execution.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 5, 2025

Codecov Report

❌ Patch coverage is 0% with 31 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
model/ccusage_service.go 0.00% 31 Missing ⚠️
Flag Coverage Δ
unittests 23.37% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
model/ccusage_service.go 0.00% <0.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly identifies the need to execute commands within a user's shell to ensure the proper environment is loaded. However, the current implementation has significant cross-platform issues that need to be addressed. The argument escaping logic is specific to POSIX shells and will fail on Windows, introducing both functional bugs and potential security vulnerabilities. Additionally, the way commands are constructed and executed is not compatible with Windows shells like PowerShell or cmd.exe. I have provided specific feedback on how to make this implementation more robust and secure across different operating systems.

Comment thread model/ccusage_service.go
Comment on lines +446 to +454
// shellEscapeArgs joins arguments with spaces and escapes them for safe shell execution
func shellEscapeArgs(args []string) string {
escaped := make([]string, len(args))
for i, arg := range args {
// Simple shell escaping: wrap in single quotes and escape single quotes
escaped[i] = "'" + strings.ReplaceAll(arg, "'", "'\"'\"'") + "'"
}
return strings.Join(escaped, " ")
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This shell escaping implementation is specific to POSIX-compliant shells (like sh or bash) and is not compatible with Windows shells. The getUserShell function can return powershell or cmd.exe on Windows, both of which have different and complex quoting rules.

Using this POSIX-specific escaping on Windows will lead to command execution failures for arguments containing spaces or special characters. More critically, it can introduce command injection vulnerabilities.

This function must be made platform-aware. You should check runtime.GOOS and implement escaping logic appropriate for the target shell. For example, PowerShell escapes single quotes within a single-quoted string by doubling them (''). A fully robust, cross-shell implementation is non-trivial and may require refactoring to pass the detected shell type into this function.

Comment thread model/ccusage_service.go
Comment on lines +242 to +249
cmdStr := bunxPath + " " + shellEscapeArgs(args)
cmd = exec.CommandContext(ctx, shell, "-c", cmdStr)
slog.Debug("Using bunx to collect ccusage data", "shell", shell)
} else {
// Fall back to npx with --yes flag to auto-accept prompts
npxArgs := append([]string{"--yes"}, args...)
cmd = exec.CommandContext(ctx, npxPath, npxArgs...)
slog.Debug("Using npx to collect ccusage data")
cmdStr := npxPath + " " + shellEscapeArgs(npxArgs)
cmd = exec.CommandContext(ctx, shell, "-c", cmdStr)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The command construction and execution logic has a couple of issues that will cause it to fail on Windows and on any OS if paths contain spaces:

  1. Unescaped Command Path: The command path (bunxPath or npxPath) is not escaped. If the path contains spaces (which is common on Windows, e.g., C:\Program Files\...), the shell will misinterpret the command, leading to execution failure.
  2. Incorrect Shell Flag: The -c flag is hardcoded to pass the command string to the shell. This is incorrect for PowerShell, which expects -Command, and for cmd.exe, which expects /c. Since getUserShell can return any of these on Windows, command execution will fail.

To fix this, you should treat the executable path as the first argument to be escaped along with the other arguments. You also need to use the correct flag for the detected shell. A good approach would be to modify getUserShell to return both the shell path and the appropriate flag.

This issue applies to both the bunx and npx command construction.

@AnnatarHe AnnatarHe merged commit a3a0d96 into main Oct 5, 2025
5 of 6 checks passed
@AnnatarHe AnnatarHe deleted the claude/issue-127-20251005-0754 branch October 5, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: call ccusage with user shell instead of golang minimal env

1 participant