Skip to content

Conversation

@potofpie
Copy link
Member

@potofpie potofpie commented Sep 23, 2025

Summary by CodeRabbit

  • New Features
    • Automatic detection of npm, Yarn, pnpm, and Bun workspaces with workspace-aware installs.
    • Installs dependencies at the correct workspace root in monorepos.
    • Smarter command selection across package managers in both local and CI environments.
  • Bug Fixes
    • More reliable installs in monorepos by avoiding hardcoded paths.
    • Null-safe handling when no workspace is present.
  • Tests
    • Added comprehensive tests for workspace glob pattern matching.
    • Updated tests to cover workspace-aware command generation and CI scenarios.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 23, 2025

Walkthrough

Adds workspace-aware JavaScript dependency installation to the bundler. Detects npm/pnpm/yarn/bun workspaces, determines workspace root, matches workspace patterns, selects install directory accordingly, and updates install command generation and usage. Extends function signatures to include isWorkspace/production flags. Adds tests for command selection and glob/pattern matching.

Changes

Cohort / File(s) Summary
Workspace detection & install dir resolution
internal/bundler/bundler.go
Introduces WorkspaceConfig, detection of workspace root/types/patterns, path-to-pattern matching (incl. negation and recursive globs), and logic to choose installDir (workspace root vs agent dir).
Install command API updates
internal/bundler/bundler.go
Extends jsInstallCommandSpec and getJSInstallCommand to accept isWorkspace/production; bundleJavascript now computes and passes isWorkspace and uses installDir for installs, type-checking, and SDK path resolution.
Tests for commands and patterns
internal/bundler/bundler_test.go
Propagates new isWorkspace parameter in helpers; updates call sites; adds tests for matchesWorkspacePattern with diverse patterns and for npm-style glob behaviors; adds PNPM CI cases.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer/CI
  participant Bundler as Bundler
  participant FS as Filesystem
  participant PM as Package Manager

  Dev->>Bundler: bundleJavascript(agentDir)
  Bundler->>FS: detectWorkspaceRoot(start=agentDir)
  FS-->>Bundler: WorkspaceConfig or nil

  alt Workspace detected
    Bundler->>Bundler: isAgentInWorkspace(agentDir, WorkspaceConfig)
    alt Agent in workspace
      Bundler->>Bundler: installDir = workspace.root
      note right of Bundler: New flow: workspace-aware install
    else Agent not in workspace
      Bundler->>Bundler: installDir = agentDir
    end
  else No workspace
    Bundler->>Bundler: installDir = agentDir
  end

  Bundler->>Bundler: getJSInstallCommand(ctx, installDir, runtime, isWorkspace)
  Bundler->>PM: run install command in installDir
  PM-->>Bundler: install result

  Bundler->>Bundler: type check / resolve SDK using installDir
  Bundler-->>Dev: bundle output
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Adds support for pnpm as a bundler #438 — Also modifies internal/bundler/bundler.go to enhance package manager detection and command specs; this PR builds on that area by adding workspace awareness and signature changes.

Suggested reviewers

  • jhaynie
  • pec1985
  • robindiddams

Poem

I hop through workspaces, nose to the ground,
Globs like stars, in patterns I’ve found.
Root to agent, I map the trail,
Install commands set full sail.
With pnpm, yarn, or npm too—
Bundles bloom fresh as morning dew. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Fix npm workspaces" is concise and directly related to the changes, which address workspace detection and installation behavior for npm workspaces (the PR also expands support to pnpm/yarn/bun, but the primary fix described is for npm workspaces), so it communicates the main intent clearly for reviewers scanning history.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch npm-workspace-fix

Comment @coderabbitai help to get the list of available commands and usage tips.

@potofpie potofpie marked this pull request as ready for review September 24, 2025 12:52
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
internal/bundler/bundler.go (1)

241-265: Consider adding validation for empty or malformed patterns.

While the function handles pattern matching well, it could benefit from validating that patterns are not empty strings or contain invalid characters that might cause issues with glob matching.

Add validation at the beginning of the function:

 func isAgentInWorkspace(logger logger.Logger, agentDir string, workspace *WorkspaceConfig) bool {
+	if workspace == nil || len(workspace.Patterns) == 0 {
+		logger.Debug("no workspace patterns to check")
+		return false
+	}
 	// Get relative path from workspace root to agent directory
 	relPath, err := filepath.Rel(workspace.Root, agentDir)
internal/bundler/bundler_test.go (2)

163-239: Consider adding tests for workspace detection and install directory resolution.

While the pattern matching is well tested, consider adding integration tests for the detectWorkspaceRoot, isAgentInWorkspace, and findWorkspaceInstallDir functions to ensure the complete workspace flow works correctly.

Would you like me to generate comprehensive test cases for the workspace detection and install directory resolution functions? These tests would help ensure the entire workspace feature works correctly end-to-end.


41-53: Consider testing workspace-aware installation commands.

The tests currently only verify non-workspace scenarios. Consider adding tests that verify the correct installation commands are generated when isWorkspace=true.

Add test cases for workspace scenarios:

func TestWorkspaceInstallCommands(t *testing.T) {
	tests := []struct {
		name         string
		runtime      string
		isWorkspace  bool
		production   bool
		expectedArgs []string
	}{
		{
			name:        "npm workspace development",
			runtime:     "nodejs",
			isWorkspace: true,
			production:  false,
			expectedArgs: []string{"install", "--no-audit", "--no-fund", "--ignore-scripts"},
		},
		{
			name:        "pnpm workspace development",
			runtime:     "pnpm",
			isWorkspace: true,
			production:  false,
			expectedArgs: []string{"install", "--ignore-scripts", "--silent"},
		},
		// Add more test cases...
	}
	// Implementation...
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c251d77 and 0d8c5df.

📒 Files selected for processing (2)
  • internal/bundler/bundler.go (11 hunks)
  • internal/bundler/bundler_test.go (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
internal/bundler/bundler_test.go (1)
internal/bundler/bundler.go (1)
  • BundleContext (40-50)
internal/bundler/bundler.go (4)
internal/util/io.go (1)
  • Exists (14-19)
internal/project/project.go (1)
  • Bundler (493-502)
internal/util/process_nix.go (1)
  • ProcessSetup (14-17)
internal/util/process_windows.go (1)
  • ProcessSetup (22-28)
🔇 Additional comments (15)
internal/bundler/bundler.go (11)

148-152: LGTM! Well-structured workspace configuration type.

The WorkspaceConfig struct is clearly documented and appropriately captures the essential workspace information with proper field names.


154-207: Good workspace detection implementation with proper traversal logic.

The function correctly walks up the directory tree looking for workspace configurations, properly handles both npm/yarn and pnpm workspace formats, and safely stops at the root directory. The debug logging is helpful for troubleshooting.


209-238: Robust handling of npm workspace formats.

The function correctly handles both array and object formats for npm workspaces, with proper type checking and error handling.


267-286: Well-implemented workspace pattern matching with cross-platform support.

Good use of doublestar library for robust glob matching and proper path normalization for cross-platform compatibility. The negation pattern handling is correctly implemented.


288-308: Solid implementation of workspace install directory resolution.

The function properly determines whether to use the workspace root or agent directory for installation, with appropriate error handling and debug logging.


325-354: Workspace-aware installation commands look correct.

The function properly adjusts installation flags based on workspace context, correctly avoiding production-only flags in workspace development mode to ensure TypeScript types are available.


423-427: Good integration of workspace detection into the bundle flow.

The workspace detection is properly integrated, determining the correct install directory and passing the workspace flag through the installation pipeline.


123-123: Type checking now correctly uses workspace-aware install directory.

The change ensures TypeScript compiler is found in the correct location within workspaces.


457-457: Source map support installation correctly targets workspace root.

The change ensures bun's source-map-js is installed in the correct directory for workspace scenarios.


6-6: Appropriate dependency additions for workspace support.

The new dependencies (encoding/json for package.json parsing, doublestar for glob pattern matching, and gopkg.in/yaml.v3 for pnpm-workspace.yaml) are all necessary and well-chosen for implementing workspace support.

Also applies to: 24-24, 26-26


491-491: Approve change — using installDir for SDK resolution is correct.
resolveAgentuity walks up parent directories from the provided startDir to find node_modules/@agentuity/sdk/package.json and returns that path, so calling resolveAgentuity(ctx.Logger, installDir) correctly supports workspace installs (impl: internal/bundler/bundler.go:712; call: internal/bundler/bundler.go:491).

internal/bundler/bundler_test.go (4)

41-53: Test helper properly updated for workspace parameter.

The testPackageManagerCommand helper function correctly accepts and passes through the new isWorkspace parameter.


133-133: Existing tests properly updated with workspace flag.

All existing test calls have been correctly updated to include the isWorkspace parameter set to false, maintaining backward compatibility.

Also applies to: 147-147, 157-157


163-202: Comprehensive test coverage for workspace pattern matching.

Excellent test coverage including exact matches, glob patterns, recursive patterns, negation, and cross-platform path handling.


204-239: Thorough npm-style glob pattern tests.

Good additional coverage for complex npm workspace patterns including deep nesting, universal globs, and edge cases.

@potofpie potofpie merged commit a70571b into main Sep 24, 2025
14 checks passed
@potofpie potofpie deleted the npm-workspace-fix branch September 24, 2025 13:12
devin-ai-integration bot added a commit that referenced this pull request Sep 24, 2025
- Added: [AGENT-684] Check if zsh is installed before adding autocomplete in the CLI (#450)
- Added: [AGENT-628] Unit tests (#441)
- Added: feat: automatically add AGENTUITY_SDK_KEY and AGENTUITY_PROJECT_KEY to .env file when running dev command (#442)
- Changed: Dont sort releases by commit msg (#447)
- Changed: [AGENT-628] prevent local development env files from syncing to production (#440)
- Fixed: Fix npm workspaces (#451)
- Fixed: Fix 'Press any key to continue' to accept any key, not just Enter (#445)

Co-Authored-By: unknown <>
jhaynie pushed a commit that referenced this pull request Sep 24, 2025
- Added: [AGENT-684] Check if zsh is installed before adding autocomplete in the CLI (#450)
- Added: [AGENT-628] Unit tests (#441)
- Added: feat: automatically add AGENTUITY_SDK_KEY and AGENTUITY_PROJECT_KEY to .env file when running dev command (#442)
- Changed: Dont sort releases by commit msg (#447)
- Changed: [AGENT-628] prevent local development env files from syncing to production (#440)
- Fixed: Fix npm workspaces (#451)
- Fixed: Fix 'Press any key to continue' to accept any key, not just Enter (#445)

Co-Authored-By: unknown <>

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

2 participants