Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): only prefer ~/.local/bin when its composio entry is this executable

jkomyno6d0e14f15512
diff --git a/ts/packages/cli/src/commands/install.cmd.ts b/ts/packages/cli/src/commands/install.cmd.ts
index 54f5aef93..a46a0f444 100644
--- a/ts/packages/cli/src/commands/install.cmd.ts
+++ b/ts/packages/cli/src/commands/install.cmd.ts
@@ -85,13 +85,31 @@ const isDirOnPath = (pathEnv: string, dir: string): boolean =>
pathEnv.split(':').some(entry => entry === dir);
/**
- * Bin-dir resolution order: explicit env, then `~/.local/bin` when it already
- * holds a `composio` entry point, then the real binary's own directory.
+ * True when `candidate` resolves (following symlinks) to the running
+ * executable. A bare exists-check is not enough for bin-dir resolution: a
+ * leftover `composio` from another installer (e.g. pip) would win, and the
+ * PATH line would point shells at the wrong program.
+ */
+const resolvesToCurrentExecutable = (
+ candidate: string,
+ execPath: string,
+ fs: FileSystem.FileSystem
+): Effect.Effect<boolean> =>
+ Effect.zipWith(
+ fs.realPath(candidate),
+ fs.realPath(execPath).pipe(Effect.orElseSucceed(() => execPath)),
+ (candidateReal, execReal) => candidateReal === execReal
+ ).pipe(Effect.orElseSucceed(() => false));
+
+/**
+ * Bin-dir resolution order: explicit env, then `~/.local/bin` when its
+ * `composio` entry point is the running executable, then the real binary's
+ * own directory.
*/
const resolveBinDir = (params: {
readonly envBinDir: string | undefined;
readonly localBinDir: string;
- readonly localBinComposioExists: boolean;
+ readonly localBinComposioIsCurrentExecutable: boolean;
readonly execPath: string;
readonly path: Path.Path;
}): string => {
@@ -99,7 +117,7 @@ const resolveBinDir = (params: {
if (trimmedEnvBinDir && trimmedEnvBinDir.length > 0) {
return trimmedEnvBinDir;
}
- if (params.localBinComposioExists) {
+ if (params.localBinComposioIsCurrentExecutable) {
return params.localBinDir;
}
return params.path.dirname(params.execPath);
@@ -250,17 +268,22 @@ export const installShellIntegration = (params: {
yield* ui.intro('composio install');
- // Resolve the entry-point bin dir: explicit env, then an existing
- // ~/.local/bin/composio, then the runtime executable's own directory.
+ // Resolve the entry-point bin dir: explicit env, then a ~/.local/bin/composio
+ // that is this executable, then the runtime executable's own directory.
const envBinDir = yield* readOptionalEnv('COMPOSIO_BIN_DIR');
const localBinDir = path.join(os.homedir, '.local', 'bin');
- const localBinComposioExists = yield* fs.exists(path.join(localBinDir, 'composio'));
+ const execPath = params.execPath ?? nodeProcess.execPath;
+ const localBinComposioIsCurrentExecutable = yield* resolvesToCurrentExecutable(
+ path.join(localBinDir, 'composio'),
+ execPath,
+ fs
+ );
const binDir = path.normalize(
resolveBinDir({
envBinDir,
localBinDir,
- localBinComposioExists,
- execPath: params.execPath ?? nodeProcess.execPath,
+ localBinComposioIsCurrentExecutable,
+ execPath,
path,
})
);
diff --git a/ts/packages/cli/test/src/commands/install.cmd.test.ts b/ts/packages/cli/test/src/commands/install.cmd.test.ts
index 51521d006..87efc55c7 100644
--- a/ts/packages/cli/test/src/commands/install.cmd.test.ts
+++ b/ts/packages/cli/test/src/commands/install.cmd.test.ts
@@ -837,7 +837,7 @@ describe('CLI: composio install', () => {
});
});
- describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin already has a composio entry point', () => {
+ describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin/composio is the running executable', () => {
layer(TestLive())(it => {
it.scoped('[Then] the PATH line targets ~/.local/bin via a literal $HOME prefix', () =>
Effect.gen(function* () {
@@ -846,10 +846,11 @@ describe('CLI: composio install', () => {
vi.stubEnv('SHELL', '/bin/zsh');
const localBinDir = path.join(os.homedir, '.local', 'bin');
+ const localBinComposio = path.join(localBinDir, 'composio');
yield* fs.makeDirectory(localBinDir, { recursive: true });
- yield* fs.writeFileString(path.join(localBinDir, 'composio'), '');
+ yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');
- yield* install();
+ yield* install({ execPath: localBinComposio });
const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');
@@ -859,7 +860,57 @@ describe('CLI: composio install', () => {
});
});
- describe('[When] COMPOSIO_BIN_DIR is set and ~/.local/bin also already has a composio entry point', () => {
+ describe('[When] ~/.local/bin/composio is a symlink chain to the running executable', () => {
+ layer(TestLive())(it => {
+ it.scoped('[Then] the PATH line still targets ~/.local/bin', () =>
+ Effect.gen(function* () {
+ const os = yield* NodeOs;
+ const fs = yield* FileSystem.FileSystem;
+ vi.stubEnv('SHELL', '/bin/zsh');
+
+ const execDir = path.join(os.homedir, '.composio-dist');
+ const execFile = path.join(execDir, 'composio');
+ const intermediateLink = path.join(os.homedir, '.composio-launcher');
+ const localBinDir = path.join(os.homedir, '.local', 'bin');
+ yield* fs.makeDirectory(execDir, { recursive: true });
+ yield* fs.makeDirectory(localBinDir, { recursive: true });
+ yield* fs.writeFileString(execFile, '#!/bin/sh\n');
+ yield* fs.symlink(execFile, intermediateLink);
+ yield* fs.symlink(intermediateLink, path.join(localBinDir, 'composio'));
+
+ yield* install({ execPath: execFile });
+
+ const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
+ expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');
+ })
+ );
+ });
+ });
+
+ describe('[When] ~/.local/bin/composio is a foreign program (e.g. a leftover pip install)', () => {
+ layer(TestLive())(it => {
+ it.scoped('[Then] the PATH line targets the running executable directory instead', () =>
+ Effect.gen(function* () {
+ const os = yield* NodeOs;
+ const fs = yield* FileSystem.FileSystem;
+ vi.stubEnv('SHELL', '/bin/zsh');
+ const expectedBinDir = expectedRuntimeBinDir();
+
+ const localBinDir = path.join(os.homedir, '.local', 'bin');
+ yield* fs.makeDirectory(localBinDir, { recursive: true });
+ yield* fs.writeFileString(path.join(localBinDir, 'composio'), '#!/usr/bin/env python\n');
+
+ yield* install();
+
+ const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
+ expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);
+ expect(contents).not.toContain('.local/bin');
+ })
+ );
+ });
+ });
+
+ describe('[When] COMPOSIO_BIN_DIR is set and ~/.local/bin/composio is also the running executable', () => {
layer(TestLive())(it => {
it.scoped('[Then] the env var wins over the ~/.local/bin fallback', () =>
Effect.gen(function* () {
@@ -869,10 +920,11 @@ describe('CLI: composio install', () => {
vi.stubEnv('COMPOSIO_BIN_DIR', '/custom/bin');
const localBinDir = path.join(os.homedir, '.local', 'bin');
+ const localBinComposio = path.join(localBinDir, 'composio');
yield* fs.makeDirectory(localBinDir, { recursive: true });
- yield* fs.writeFileString(path.join(localBinDir, 'composio'), '');
+ yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');
- yield* install();
+ yield* install({ execPath: localBinComposio });
const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));
expect(contents).toContain('export PATH="/custom/bin:$PATH"');