aider.el icon indicating copy to clipboard operation
aider.el copied to clipboard

Unexpected behaviour when starting aider.el from buffer with file

Open magthe opened this issue 7 months ago • 22 comments

Steps

  1. Visit a project (I'm using projectile)
  2. Open a file in the project, e.g. tests/test_am_types.py
  3. Start aider.el (transient menu -> a)
  4. Add the current buffer to aider (transient menu -> f)
  5. Answer "no" to question about creating the file that's being added.

Expected behaviour

The question in step 5 isn't asked, and the file visited in the buffer is added to aider's context.

Observed behaviour

No file is added to aider's context.

Contents of the aider buffer

This is what I get in the aider buffer when executing the steps above

Detected dumb terminal, disabling fancy input and pretty output.

Aider v0.82.2
Main model: claude-3-7-sonnet-latest with diff edit format, infinite output
Weak model: claude-3-5-haiku-20241022
Git repo: none
Repo-map: disabled
Note: in-chat filenames are always relative to the git working dir, not the current working dir.
Cur working dir: /home/elrond/Work/py-am/tests
Git working dir: /home/elrond/Work/py-am

> /add tests/test_am_types.py

> > > 
No files matched 'tests/test_am_types.py'. Do you want to create /home/elrond/Work/py-am/tests/tests/test_am_types.py? (Y)es/(N)o [Yes]: no

> /ls


No files in chat, git repo, or read-only list.

Comparison to running aider in a terminal

Since I'll start aider in the project's root folder I don't see the same behaviour.

Behaviour when answering "yes" when asked about creating the file

If I answer "yes" in step 5 above, i.e. I tell aider to create the file, then an empty file is created at tests/tests/test_am_types.py. It's also added to the context.

Related issues

I think that both #23 and #26 could be related issues.

magthe avatar May 01 '25 07:05 magthe

I've tried to instead start aider from a dired buffer visiting the project's root folder. That works for a while, but after adding a few files aider gets confused and starts asking about creating files.

This behaviour makes aider.el useless for me at the moment and I've had to go back to running aider in a terminal.

magthe avatar May 01 '25 07:05 magthe

Thanks for the feedback! Your Steps are very clear. I tried to reproduce it using aider.el repo with these steps, but I cannot reproduce the issue.

  1. Have .projectile in aider.el home dir (bottom right in screenshot)
  2. Opened ./examples/connect4/game.py (top left, path in top right)
  3. C-c a a open aider session from game.py buffer, we can see the Cur dir and Working dir are different (bottom left)
  4. Switch back to game.py, C-c a f add it to aider session
  5. The session do shows: Added examples/connect4/game.py to the chat. It didn't ask me to create a new file. (bottom left)

screenshot below:

Image

tninja avatar May 01 '25 13:05 tninja

Tried same thing in aider repo, not able to reproduce the issue as well, screenshot below

Image

tninja avatar May 01 '25 14:05 tninja

Could you pleases provide a public github repo where I can reproduce the issue; or you can attach your repo zip file and I'll check if I can reproduce this on my laptop?

tninja avatar May 01 '25 14:05 tninja

It happens to me in all repos... but I'll find a specific one and reproduce it there.

In the mean time... what happens if you jump to a second file, a file in another folder, and add it?

magthe avatar May 01 '25 14:05 magthe

In aider.el repo, under example directory, I jump from test_game.py of connect4 game to game.py of battle ship, with projectile, the file add work for me:

Image

tninja avatar May 01 '25 16:05 tninja

  1. Open the aider.el project
  2. Open aider.el/examples/battleship/fleet.py
  3. Start aider, transient menu -> a
  4. Add the current file, transient menu -> f

Image

magthe avatar May 01 '25 17:05 magthe

I'll see if I can put together a minimal Emacs config and see if I can reproduce it then as well.

magthe avatar May 01 '25 17:05 magthe

I just installed aider.el from melpa (to avoid potential discrepancy with the one I used directly from working repo) and try to reproduce the issue following your above steps, still cannot reproduce it. I'll try the straight installation later

Image

tninja avatar May 01 '25 18:05 tninja

I tried the straight installation one (same as yours), still cannot reproduce the issue..

Image

But I do hit some weird dependency installation error msg with straight. I'll update the README and put melpa installation one to the first.

tninja avatar May 01 '25 19:05 tninja

From your above screenshot, it seems that the elisp code build the right relative path to the git root, (/add examples/battleship/fleet.py), and sent it to aider session. But that aider session seems rebuild the path given the work dir, instead of git root dir (examples/battleship/examples/battleship/fleet.py)

Wonder if you can try two things:

  1. will manual copy paste this command in aider session still not work? /add examples/battleship/fleet.py
  2. if you start aider session in the git root directory. (eg. open README.org, and then start the aider session). will the above command work?

This is the line derive the git root relative path: https://github.com/tninja/aider.el/blob/main/aider-file.el#L24. I guess for some reason /add in aider session didn't work on your laptop.

tninja avatar May 01 '25 19:05 tninja

  1. will manual copy paste this command in aider session still not work? /add examples/battleship/fleet.py

If I do that I get asked about creating the file, i.e. it doesn't work as expected.

  1. if you start aider session in the git root directory. (eg. open README.org, and then start the aider session). will the above command work?

When I do that I don't get asked about creating the file, i.e. it does work as expected.

magthe avatar May 02 '25 19:05 magthe

For fun I tried one other thing

  1. Open examples/battleship/fleet.py in aider.el
  2. Start aider session
  3. Manually add the file with the full absolute path, /add /home/elrond/.emacs.d/straight/repos/aider.el/examples/battleship/fleet.py
  4. Manually add one of the elisp files with the full absolute path, /add /home/elrond/.emacs.d/straight/repos/aider.el/aider-agile.el

Image

The behaviour supports your observation that

aider session seems rebuild the path given the work dir, instead of git root dir

So, how can I force the aider session to start in the project root? (Ideally without having to remember to always start the session from a file in the project root.)

magthe avatar May 02 '25 19:05 magthe

Another thing that I find confusing is that when I start aider in the project root, where the .git/ folder is, then aider claims there is no git repo:

Image

That's got nothing to do with aider.el though:

Image

I still find it confusing though.

magthe avatar May 02 '25 19:05 magthe

  1. will manual copy paste this command in aider session still not work? /add examples/battleship/fleet.py

If I do that I get asked about creating the file, i.e. it doesn't work as expected.

  1. if you start aider session in the git root directory. (eg. open README.org, and then start the aider session). will the above command work?

When I do that I don't get asked about creating the file, i.e. it does work as expected.

Seems the temporary solution on your laptop, without changing anything, is to start aider from any file (eg. README.org) in git root directory. Aider.el start the aider session based on the directory of the current file buffer.

tninja avatar May 03 '25 03:05 tninja

I followed these steps, both files can be added to aider session on my laptop. If same thing also happens in your terminal aider (start aider in this directory: /home/elrond/.emacs.d/straight/repos/aider.el/examples/battleship/, and then do others), I suspect something wrong with this aider installation.

Image

For fun I tried one other thing

1. Open `examples/battleship/fleet.py` in aider.el

2. Start aider session

3. Manually add the file with the full absolute path, `/add /home/elrond/.emacs.d/straight/repos/aider.el/examples/battleship/fleet.py`

4. Manually add one of the elisp files with the full absolute path, `/add /home/elrond/.emacs.d/straight/repos/aider.el/aider-agile.el`

Image

The behaviour supports your observation that

aider session seems rebuild the path given the work dir, instead of git root dir

So, how can I force the aider session to start in the project root? (Ideally without having to remember to always start the session from a file in the project root.)

tninja avatar May 03 '25 03:05 tninja

Aider started from git root directory does't have this problem on my laptop. I suspect sth wrong with your aider installation. Maybe reinstall it, or try it on a different computer and see if it can resolve the problem?

Image

Another thing that I find confusing is that when I start aider in the project root, where the .git/ folder is, then aider claims there is no git repo:

Image

That's got nothing to do with aider.el though:

Image

I still find it confusing though.

tninja avatar May 03 '25 03:05 tninja

Ah, yes indeed... there's something strange with aider itself I guess:

Image

its claim that

Note: in-chat filenames are always relative to the git working dir, not the current working dir.

Is clearly not true. If it were I wouldn't be able to /add fleet.py, right?

magthe avatar May 03 '25 07:05 magthe

I created ticket on aider: https://github.com/Aider-AI/aider/issues/3941

magthe avatar May 03 '25 16:05 magthe

Yes. In past months, wherever aider start (cur working dir), with or without --subtree-only, it can add file, with git root relative path format. So aider.el use this logic to add file. If in future something changed in aider, we can adapt that change in aider.el.

Ah, yes indeed... there's something strange with aider itself I guess:

Image

its claim that

Note: in-chat filenames are always relative to the git working dir, not the current working dir.

Is clearly not true. If it were I wouldn't be able to /add fleet.py, right?

tninja avatar May 03 '25 17:05 tninja

Yes. In past months, wherever aider start (cur working dir), with or without --subtree-only, it can add file, with git root relative path format.

Isn't the problem I have exactly the opposite of this statement?

When I run aider, directly in a terminal or via aider.el, it never adds anything relative to the git root. Instead it always only adds relative to the working dir.

magthe avatar May 03 '25 18:05 magthe

Correct. The problem you see opposite of this statement (what I saw on my laptop, and expected aider behavior from aider.el)

Tried it again on my laptop, aider in terminal, still need the git root relative path, and path relative to the working dir doesn't work.

Image

Isn't the problem I have exactly the opposite of this statement?

When I run aider, directly in a terminal or via aider.el, it never adds anything relative to the git root. Instead it always only adds relative to the working dir.

tninja avatar May 03 '25 18:05 tninja

Close this since it doesn't seems to be an aider.el issue. Free free reopen this if you think we should address it from aider.el side.

tninja avatar Jun 13 '25 01:06 tninja

Ah, sorry for not coming back with an update.

At some point I realised I didn't want to make git commits so I added

export AIDER_GIT=false

to my shell setup. I'm not sure if it was me reading too quickly, or that it at the time was the only git-related envvar, probably the former 😅 , but what I should have set is

export GIT_AUTO_COMMIT=false

So 👍 to closing this.

(I've already passed this on to one of my co-workers who had the same setting, and had noticed the same behaviour but was too lazy to dig into it. 😁 )

magthe avatar Jun 13 '25 05:06 magthe

I see.. Thanks for feedback on git auto commit preference. Yes seems that lots of people prefer to turn it off. I personally still like it since it help me track the code change history, the commit history can also indicate if it is by AI. If I don't like it, I can easily HARD reset to check point with X key in magit.

I thought it might be helpful to protect main branch with this change https://github.com/tninja/aider.el/pull/194, but it might not be necessary for everyone, and people prefer directly change on main branch.

In that case, we can (setq aider-confirm-on-main-branch nil) to bypass that protection.

tninja avatar Jun 13 '25 14:06 tninja