Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to save files that need user elevation #1614

Closed
felixfbecker opened this issue Dec 23, 2015 · 55 comments · Fixed by #40107
Closed

Allow to save files that need user elevation #1614

felixfbecker opened this issue Dec 23, 2015 · 55 comments · Fixed by #40107
Assignees
Labels
feature-request Request for new features or functionality on-testplan workbench-run-as-admin Issues concerning running as administrator
Milestone

Comments

@felixfbecker
Copy link
Contributor

I often need to edit config files (MySQL, PHP, Apache, ...) in directories like C:\Program Files that require administrative rights. When you open them with code and save the file it says "Permission denied". So I have to run Code with right click as administrator and then choose "open file" to open the file. Once, it is not possible to simply drag&drop the file into the elevated Code. The other problem is, once I already have code instance running without elevated rights (say I have my project open and now I need to edit a config) I cannot get another instance with elevated permissions. UAC asks for permissions and a new Code window opens, but it is not elevated, when I save it says permission denied. I have to close my project before I can get an elevated Code instance.

I don't know if that is a restriction of Windows but seeing that you can, for example, run a normal Powershell and an elevated Powershell side-by-side I think this should be possible. The optimal case of course would be if Code could elevate itself if it can't save a file because of insufficient permissions. But being able to run elevated and non-elevated side-by-side would already help a lot.

@bpasero bpasero added the feature-request Request for new features or functionality label Dec 23, 2015
@bpasero bpasero added this to the Backlog milestone Dec 23, 2015
@felixfbecker
Copy link
Contributor Author

Notepad++ does this very well:
image

@sstjohn
Copy link

sstjohn commented Jan 25, 2016

I've got a debugger extension that needs to be run with privilege. On Linux, sudo code is sufficient to launch both vscode and the debugger as root; but due to this issue, my extension can't work on Windows.

@felixfbecker
Copy link
Contributor Author

felixfbecker commented Jan 25, 2016

@sstjohn You can run Code as administrator on Windows, and that should inherit to all child processes. It is just not very good UX at the moment:

  • close all open windows of Code
  • right click on Code and click "run as administrator" or type code in an elevated command line
  • open the desired file from inside Code with File > Open (drag and drop will not work anymore)

@sstjohn
Copy link

sstjohn commented Jan 26, 2016

Oh I get it, I was trying to run multiple instances in the same login session with different privileges. Thanks for the pointer, I'd suggest this could use a little documentation at least.

@joggienl
Copy link

This is somewhat related to this question http://stackoverflow.com/questions/31458077/mac-user-running-mamp-issues-trying-to-modify-files-inside-root-folder-eacce?noredirect=1#comment50887510_31458077

An item on uservoice also has been created: http://visualstudio.uservoice.com/forums/293070-visual-studio-code/suggestions/8915236-visual-code-w-terminal-integrated-and-super-admin

Seems like there is a need for a os-independant solution to be able to edit files which need administrator password / elevated rights.

@regs01
Copy link

regs01 commented Apr 1, 2016

@felixfbecker
just a prompt to elevate on save will save a lot of time.

@gerane
Copy link

gerane commented Apr 4, 2016

Even if I close all Code Instances, I cannot get an elevated Code on Windows. This makes Code unusable for my use cases. It doesn't matter if I use RunAs or execute code in an elevated cmd or powershell prompt, it is not elevated.

@felixfbecker
Copy link
Contributor Author

I think I found something that should help with this: https://www.npmjs.com/package/electron-sudo
It allows to run a process with privileged rights, cross-platform, by opening a platform-dependend prompt.
You could save all unsaved files in the temporary folder, then use electron-sudo to restart Code as admin, and re-open and save the unsaved files (I assume this is how Notepad++ does it).

@bpasero bpasero changed the title Running Code as administrator Better support running Code as administrator Jul 5, 2016
@CasperTDK
Copy link

+1

@artisticcheese
Copy link

Please implement. That the only thing prevents me from deploying as default editor on servers instead of notepad++ which does it out of the box.

@third774
Copy link

third774 commented Sep 8, 2016

Would love to see VS Code handle this more gracefully!

@jontro
Copy link

jontro commented Nov 24, 2016

Sublime Text does this in a very nice way. It will only pop up an OS prompt asking to type in the password. The editor doesn't even restart (I guess it runs the save in a forked background process)

@emilast
Copy link

emilast commented Dec 5, 2016

I hit this problem every single day, it would be really nice if there was a way to fix this. My main problem is not being able to open new Code instances if already opened and running elevated. Opening a second instance non-elevated when choosing "Open with Code" in File Explorer would solve the main problem, but handling saves similarly to what Notepad++ is doing would be really neat as well.

@mikegwhit
Copy link

I go to the executable directory, goto compatability, switch on "run as administrator". I am prompted to authorize elevated privileges when opening a directory in code (via context menu). However, when running terminal inside elevated Code.exe process, the terminal still is not admin.

@Gringox
Copy link

Gringox commented Jan 25, 2017

Any updates on this? It could be nice to have a prompt asking for the password like sublime or atom does

@kamil-kielczewski
Copy link

kamil-kielczewski commented Dec 1, 2017

I like vscode - however in atom you open "admin read-only" file as normal user eg. /etc/hosts, and when you try to save, atom ask you (using some system dialog) about admin password - you type it and file is just saved (look on image - (polish lang: Użytkoninik=user name, Hasło=password)). Why this simple behaviour is impossible in vscode ?

zrzut ekranu 2017-12-01 o 21 56 26

@bpasero bpasero changed the title Better support running Code as administrator Allow to save files that need user elevation Dec 3, 2017
@bpasero bpasero self-assigned this Dec 12, 2017
@bpasero bpasero added this to the December 2017/January 2018 milestone Dec 12, 2017
@bpasero
Copy link
Member

bpasero commented Dec 12, 2017

Using sudo-prompt you can now retry to save as admin when saving fails. This will work on macOS, Linux and Windows:

flicker_chrome58

There are some design decisions that I took that are worth mentioning where behaviour is maybe different from what others do:

  • you will always first get the error message and from there you can save as admin
  • each time you save you will have to authenticate again as admin
  • you cannot create a file on disk as admin, the file has to exist on disk
  • you cannot save readonly or hidden files

I think this covers the scenario where you quickly need to make a change to a file that needs elevation. It is not meant as a way to work on a bunch of files for a longer period and where you have to create files and folders as admin. For that I think VS Code is not suitable. Rather, if you need to do that, use a file manager with admin rights.

This will be available in insiders as soon as we have released our 1.19 version this week.

@tenpaiyomi
Copy link

tenpaiyomi commented Dec 12, 2017

@bpasero Any particular reason for those design decisions vs the standard behavior? Namely, it automatically prompting if you don't have permission to save a file and allowing you to create/save hidden files, so long as you supply the permissions? It seems like a step back in terms of UX to have those limitations.

@bpasero
Copy link
Member

bpasero commented Dec 12, 2017

@tenpaiyomi not sure what would define the "standard" behaviour really. E.g. I am not aware of UX guidelines for how to do this in a standard way. The only typical flow I know of is to run the tool as super user / admin (something we do not encourage for VS Code and now you no longer have to in most cases).

I chose this way because I think executing an operation as root should be done with great care. I do not think an editor is the right tool for creating file/folder structures as admin user.

As to getting repeated prompts and errors for each save, that is maybe something we could revisit later based on feedback. Here I am maybe overly cautious to prevent harm. However to justify my rationale: I find it a bit weird to just popup a login dialog out of nowhere. As a user you could have auto save enabled and that would mean typing and waiting for a short moment would suddenly bring up a (scary) modal dialog for logging in.

All in all, I suggest that people give it a try once this hits insider and then we can continue the discussion in here. After all, its just the beginning of the milestone so there is plenty of time to talk about.

@tenpaiyomi
Copy link

tenpaiyomi commented Dec 12, 2017

@bpasero When I say standard behavior, I more refer to how things generally tend to work in the scenarios that I have experienced. For example, in Sublime Text, if I am editing a file that requires sudo to save, when I go to save it will automatically prompt me if my current permissions are not adequate. It seems more intuitive to just prompt the user versus saying "Hey, you can't do that, you need to click this button to attempt to save again and then get prompted."

@kamil-kielczewski
Copy link

kamil-kielczewski commented Dec 12, 2017

@bpasero you write:

I do not think an editor is the right tool for creating file/folder structures as admin user.

Can you explain why you think in such way (where is problem)?

@bpasero
Copy link
Member

bpasero commented Dec 12, 2017

@tenpaiyomi yeah I get that, however there can be many reasons why I file cannot be written to. For example it could be readonly (which is unrelated to permissions), would you then expect that when you save as admin, the readonly flag should go away? And what if the file belongs to another user, would'nt it make more sense to ask to log in as that user instead of logging in as admin?

@kamil-kielczewski I think writing files as admin gets messy when it comes to permissions: Should the file/folder only be readable by the admin or any user? Should it be writeable by any user or only the admin? I think creating a file as admin at the minimum has to ask what permissions to use in that case where you are impersonating as admin. If you run the tool as admin, I think you are free to do whatever you want.

Btw when I checked ST, you are not able to create a file as admin by saving it to a location that needs admin privileges. So they seem to follow a similar model.

@kamil-kielczewski
Copy link

kamil-kielczewski commented Dec 12, 2017

@bpasero I think the editor should behave exactly as operating system (e.g. linux) - so when I create file (or similar folder) by editor (an need admin permissions) I should get the same result as execute

sudo touch some_name

If this behaviour is not implemented, i need to open terminal, do this by hand, and go back do editor again.

When file is readonly situation is similar, I would like to get the same "access level" as using

sudo echo -n "foo" >> some_readonly_file.txt

command. However as I remember, many editors in windows ask USER: "This file is read-only - do you realy want to save changes" and if user agree then they save changes but not change any permissions (read-only flag also not changes). So In read-only case editor can always ask user "do he know what he do" (usually vscode users are programmers so the know what they do :) )

My thinking is right?

@bpasero
Copy link
Member

bpasero commented Dec 14, 2017

I pushed some changes to allow to save a) readonly files and b) hidden files. For a) you will see an explicit action that explains what is going on:

image

For b) we just truncate the file first before writing and then using r+ mode to write. We already do this trick for regular saving when we see that saving fails on Windows.

I still plan to look into making the experience on Windows a bit better. The prompt that shows up is a bit scary if you look at the details. Ideally we could elevate a child process of VS Code itself to admin rights so that the prompt shows the VSCode icon to the user.

@kamil-kielczewski
Copy link

I really appreciate that you fix this issue. When new VSC update will be available I will test it and give feedback.

This fix will be helpful for many developers.

Thank you :)

@bpasero
Copy link
Member

bpasero commented Dec 15, 2017

@kamil-kielczewski it should be available in todays insider release: You can give our preview releases a try from: https://code.visualstudio.com/insiders/

@kamil-kielczewski
Copy link

Ok @bpasero I install and test you fixes:

Main functionality which I want:
a) ability to save /etc/hosts by type admin password - works :)
b) save read-only files (by ask user do he really want it) - works :)
c) save hidden files - works :) (hovewer i was able to edit hidden files only by drag and drop them from mac-finder to vscode)

Minor functionalities like:
a) create file in protected (need admin privilidges) e.g. create file in /etc folder - doesn't work
b) see hidden folders and files - doesn't work (i think this option should be default set to fasle - but user should have ability to change it)

So to this point I am satisfied (however It will be good to have also minor functionalities; implement nice modal windows for user dialogs will be also nice feature)

Tank you :)

@bpasero
Copy link
Member

bpasero commented Dec 17, 2017

@kamil-kielczewski I suggest to open new issues for new feature requests. I am not sure I understand the issue with not seeing hidden folders and files. I see hidden files in the VS Code explorer.

@kamil-kielczewski
Copy link

hm strange - may be it was my mistake - now I see hidden files. So everything ok :)
Later I create new issues.

@bpasero bpasero mentioned this issue Dec 20, 2017
3 tasks
@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality on-testplan workbench-run-as-admin Issues concerning running as administrator
Projects
None yet
Development

Successfully merging a pull request may close this issue.