Skip to content

Use sudo if required to read or stat a file#4141

Open
SollyBunny wants to merge 2 commits into
micro-editor:masterfrom
SollyBunny:master
Open

Use sudo if required to read or stat a file#4141
SollyBunny wants to merge 2 commits into
micro-editor:masterfrom
SollyBunny:master

Conversation

@SollyBunny

@SollyBunny SollyBunny commented Jul 8, 2026

Copy link
Copy Markdown

If trying to modify a file which requires elevated permissions you have to make micro elevated which

  1. my config!
  2. not required

Draft because tests are failing and I would like to add more for this as there are lots of cases

First time doing anything proper with go

@SollyBunny SollyBunny force-pushed the master branch 2 times, most recently from a3b6f48 to e702f28 Compare July 8, 2026 12:02
@SollyBunny

SollyBunny commented Jul 8, 2026

Copy link
Copy Markdown
Author

Everything seems to work, here's what I have tested

For every file: make an edit and save it

sudo rm -rf /tmp/microtest
mkdir /tmp/microtest && cd /tmp/microtest

touch file

touch readonly
sudo chown root:root readonly
sudo chmod 444 readonly

touch writeonly
sudo chown root:root writeonly
sudo chmod 222 writeonly

touch root
sudo chown root:root
sudo chmod 777 writeonly

mkdir dir

mkdir rootdir
sudo chown root:root rootdir
sudo chmod 000 rootdir

sudo touch rootdir/file
sudo mkdir rootdir/dir

micro file
micro dir
micro readonly # new behaviour
micro writeonly # new behaviour
micro root # new behaviour
micro noexist

micro rootdir
micro rootdir/file # save doesnt support sudo inside unstatable yet
micro rootdir/dir # save doesnt support sudo inside unstatable yet

# micro rootdir/noexist/a # mkparents doesnt use sudo yet

@SollyBunny SollyBunny marked this pull request as ready for review July 8, 2026 14:32
@SollyBunny

Copy link
Copy Markdown
Author

Now that loading is async, the tests need to wait for the buffers to be available.
There is the problem that there is no way of knowing when a command is done that I know off.
I have bodged it by just waiting 0.1s.
The cleanest solution I can think off is to add proper callbacks to everything but that's 100s of lines of code for just a tiny fix

@Andriamanitra Andriamanitra left a comment

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.

I'm not convinced this feature is a good idea:

  • no other editor I know of does it (I checked nano, nvim, emacs, helix)
  • the implementation is fiddly and the sudo feature for writing files is already a common source of issues in micro (especially on Windows)
  • you should probably be using sudo -e / sudoedit (or equivalent) if you want to edit something that requires superuser privileges 1

Footnotes

  1. https://superuser.com/questions/785187/sudoedit-why-use-it-over-sudo-vi

Comment thread cmd/micro/micro_test.go
}

func findBuffer(file string) *buffer.Buffer {
time.Sleep(time.Second / 10) // TODO wait for open command to be done instead of waiting

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.

I really do not like this line

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