|
| 1 | +# CLI Usage Issue |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +The directory-indexer CLI commands fail to execute properly when installed via npm or used with npx. Commands hang indefinitely and produce no output, including basic help and version commands. |
| 6 | + |
| 7 | +## Symptoms |
| 8 | + |
| 9 | +- `directory-indexer --help` hangs with no output |
| 10 | +- `directory-indexer --version` hangs with no output |
| 11 | +- `npx directory-indexer --help` hangs with no output |
| 12 | +- All CLI commands hang indefinitely (tested with 3-5 second timeouts) |
| 13 | +- Commands exit with code 0 but produce no stdout or stderr |
| 14 | +- Issue occurs both with global npm install and npx usage |
| 15 | + |
| 16 | +## Expected Behavior |
| 17 | + |
| 18 | +CLI commands should execute normally and display output: |
| 19 | +```bash |
| 20 | +$ directory-indexer --help |
| 21 | +Usage: directory-indexer [options] [command] |
| 22 | + |
| 23 | +AI-powered directory indexing with semantic search |
| 24 | +... |
| 25 | +``` |
| 26 | + |
| 27 | +## Current Status - RESOLVED ✅ |
| 28 | + |
| 29 | +- ✅ **Package structure**: Correct `bin` configuration in package.json |
| 30 | +- ✅ **Shebang**: Proper `#!/usr/bin/env node` in CLI script |
| 31 | +- ✅ **Symlink**: npm creates `/usr/bin/directory-indexer` symlink correctly |
| 32 | +- ✅ **Permissions**: CLI script has execute permissions |
| 33 | +- ✅ **Dependencies**: Qdrant and Ollama services running |
| 34 | +- ✅ **Execution**: Commands now work properly with CLI launcher pattern |
| 35 | + |
| 36 | +## Resolution Implemented (v0.0.13) |
| 37 | + |
| 38 | +### Root Cause Identified |
| 39 | +The CLI script called `main()` unconditionally at module load time, causing hanging when the module was imported/executed via npm symlinks. |
| 40 | + |
| 41 | +### Solution Applied |
| 42 | +Implemented industry-standard CLI launcher pattern used by Vite ecosystem: |
| 43 | + |
| 44 | +1. **CLI Launcher**: Created `bin/directory-indexer.js` that dynamically imports the CLI module |
| 45 | +2. **Clean Module Structure**: Removed unconditional execution from CLI module |
| 46 | +3. **Dynamic Version**: Both CLI and MCP server now read version from package.json |
| 47 | +4. **Updated Package Config**: Changed bin path and added proper file includes |
| 48 | + |
| 49 | +### New Package Configuration |
| 50 | +```json |
| 51 | +{ |
| 52 | + "bin": { |
| 53 | + "directory-indexer": "bin/directory-indexer.js" |
| 54 | + }, |
| 55 | + "files": ["dist/", "bin/"], |
| 56 | + "type": "module" |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### CLI Launcher (`bin/directory-indexer.js`) |
| 61 | +```javascript |
| 62 | +#!/usr/bin/env node |
| 63 | +import('../dist/cli.js').then(({ main }) => { |
| 64 | + main().catch((error) => { |
| 65 | + console.error('CLI Error:', error); |
| 66 | + process.exit(1); |
| 67 | + }); |
| 68 | +}).catch((error) => { |
| 69 | + console.error('Failed to load CLI:', error); |
| 70 | + process.exit(1); |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +### CLI Module (`src/cli.ts`) |
| 75 | +```typescript |
| 76 | +// No unconditional execution - safely importable |
| 77 | +export async function main() { |
| 78 | + // CLI logic here |
| 79 | +} |
| 80 | +// Main function is exported for launcher to use |
| 81 | +``` |
| 82 | + |
| 83 | +## Reproduction Steps |
| 84 | + |
| 85 | +### Prerequisites |
| 86 | +1. Start development services: |
| 87 | + ```bash |
| 88 | + ./scripts/start-dev-services.sh |
| 89 | + ``` |
| 90 | + |
| 91 | +2. Set up debug container: |
| 92 | + ```bash |
| 93 | + ./scripts/docker-debug/setup-debug-container.sh |
| 94 | + ``` |
| 95 | + |
| 96 | +### Test Commands |
| 97 | +```bash |
| 98 | +# Test direct CLI usage (hangs) |
| 99 | +docker exec test-directory-indexer directory-indexer --help |
| 100 | + |
| 101 | +# Test npx usage (hangs) |
| 102 | +docker exec test-directory-indexer timeout 5 npx directory-indexer --help |
| 103 | + |
| 104 | +# Test with timeout to confirm hanging |
| 105 | +docker exec test-directory-indexer timeout 3 directory-indexer --version |
| 106 | +``` |
| 107 | + |
| 108 | +### Debug Information |
| 109 | +```bash |
| 110 | +# Check symlink |
| 111 | +docker exec test-directory-indexer ls -la /usr/bin/directory-indexer |
| 112 | + |
| 113 | +# Check package structure |
| 114 | +docker exec test-directory-indexer ls -la /usr/lib/node_modules/directory-indexer/ |
| 115 | + |
| 116 | +# Check mounted dist |
| 117 | +docker exec test-directory-indexer ls -la /usr/lib/node_modules/directory-indexer/dist/ |
| 118 | +``` |
| 119 | + |
| 120 | +## Development Workflow |
| 121 | + |
| 122 | +The debug container allows live code iteration: |
| 123 | + |
| 124 | +1. **Make changes** to `src/cli.ts` |
| 125 | +2. **Rebuild**: `npm run build` |
| 126 | +3. **Test immediately**: `docker exec test-directory-indexer directory-indexer --help` |
| 127 | + |
| 128 | +No container restart needed due to mounted dist directory. |
| 129 | + |
| 130 | +## Environment |
| 131 | + |
| 132 | +- **Container**: Ubuntu 22.04 with Node.js 20.19.3 |
| 133 | +- **Package**: directory-indexer@0.0.12 installed globally |
| 134 | +- **Services**: Qdrant (localhost:6333) and Ollama (localhost:11434) running |
| 135 | +- **Test Data**: Available at `/test-data` in container |
| 136 | + |
| 137 | +## Testing After v0.0.13 Publication |
| 138 | + |
| 139 | +Once version 0.0.13 is published to npm: |
| 140 | + |
| 141 | +1. **Test with Docker container**: |
| 142 | + ```bash |
| 143 | + ./scripts/docker-debug/setup-debug-container.sh |
| 144 | + docker exec test-directory-indexer directory-indexer --help |
| 145 | + ``` |
| 146 | + |
| 147 | +2. **Test npx usage**: |
| 148 | + ```bash |
| 149 | + npx directory-indexer@0.0.13 --help |
| 150 | + npx directory-indexer@0.0.13 --version |
| 151 | + ``` |
| 152 | + |
| 153 | +3. **Test global install**: |
| 154 | + ```bash |
| 155 | + npm install -g directory-indexer@0.0.13 |
| 156 | + directory-indexer --help |
| 157 | + ``` |
| 158 | + |
| 159 | +## Known Issues Post-Fix |
| 160 | + |
| 161 | +- Integration tests need updates to work with new CLI launcher pattern |
| 162 | +- Test setup may need adjustment for CLI execution testing |
0 commit comments