Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Open generated Go files in readonly mode #1295

Closed
dolmen opened this issue Oct 20, 2017 · 9 comments
Closed

Open generated Go files in readonly mode #1295

dolmen opened this issue Oct 20, 2017 · 9 comments

Comments

@dolmen
Copy link

dolmen commented Oct 20, 2017

The Go community has a standard for files marked as machine-generated: https://golang.org/s/generatedcode
It is expected that the generator may ran again and that any changes made by a human would be overwritten. So those files should not be modified by humans. It would be helpful if VSCode could help to enforce this.

So I propose that when a .go file is opened and it contains the ^// Code generated .* DO NOT EDIT\.$pattern the readonly mode would be enabled.

@roederw
Copy link

roederw commented Oct 21, 2017

I don't think it's a good idea to force read only. What do you think about a notification like feature that notifies the user that this is a generated file? That way they understand that their changes will, probably, be overwritten with future generations.

@dolmen
Copy link
Author

dolmen commented Oct 25, 2017

A notification at file open is too intrusive.
As a developer I may want to be able to open the generated file to read the code or because I want to trace it with the debugger. A notification every time I enter the file with the debugger would be too intrusive.
This is generated code, so if I want to modify it I should either use the generator again, or if I want just to make a discardable change (for example for debugging), disable the readonly mode to make my change. Requiring to disable the readonly mode makes the action painful, and this is the point of this feature request.

However once I have the file open and I try to modify the content, it would be helpful to have a notification of why the file is in readonly mode (and how to disable that).

@ramya-rao-a
Copy link
Contributor

If a file is being opened programatically say via a command registered by an extension, then the extension can use custom uri and register a content provider for the same which will result in the file being opened in a read only mode.

But we are talking about files that are opened normally, from the file explorer or quick open etc. There are no apis that the extension can use to let the core know that the file needs to be opened in read only mode.

One thing we (the Go extension) could do is to listen to the change event on the document. When this event occurs, check the first line for ^// Code generated .* DO NOT EDIT\.$ and provide a notification.

This is pretty easy to do and PRs are welcome.
I can help if anyone wants to give this a try.

Hint: https://github.com/Microsoft/vscode-go/blob/0.6.67/src/goMain.ts#L135 removes test coverage when there is a file change

cheesedosa added a commit to cheesedosa/vscode-go that referenced this issue Dec 25, 2017
.

    Show a warning notification if the first line of the file matches the regexp: `^// Code generated .* DO NOT EDIT\.$`.
ramya-rao-a pushed a commit that referenced this issue Dec 31, 2017
* Check and notify if the file seems to be generated. Fixes #1295.

    Show a warning notification if the first line of the file matches the regexp: `^// Code generated .* DO NOT EDIT\.$`.

* Fix lint errors.

* Skip the first line check on non go files.
@ramya-rao-a
Copy link
Contributor

This is now out in the latest update to the Go extension (0.6.71)

@gregmarr
Copy link

@ramya-rao-a Is there any way to make this notification go away when I close the file, or automatically time out? Right now I have to explicitly dismiss it, which is a bit of a pain. I've also seen this appear when I haven't tried to edit the file, but I have been unable to track down a reproduction scenario so far.

@ramya-rao-a
Copy link
Contributor

@gregmarr

Is there any way to make this notification go away when I close the file, or automatically time out?

You can always press the ESC key to make any notification in VS Code go away. There is no clear way to set timeouts on notification, but there is a feature request for the same in VS Code. See microsoft/vscode#23684

I've also seen this appear when I haven't tried to edit the file

That's worrisome, its only supposed to show up on any change to the document. Please report an issue if you are able to get a repro consistently

@gregmarr
Copy link

gregmarr commented Feb 2, 2018

@ramya-rao-a It happens if the file is regenerated (deleted and recreated) while it is open in VSCode. I guess in some fashion that results in a "file edited" trigger.

@ramya-rao-a
Copy link
Contributor

@gregmarr Does that happen often?

@gregmarr
Copy link

gregmarr commented Feb 5, 2018

In main my project, I'm working with a Swagger library that generates 4 different sets of code based on 3 different swagger files. I'm currently working on the services, so it's fairly often, but I don't expect it to be something that lots of people run into often. I also suspect that it would be difficult to differentiate between "delete, and create new" from "edit".

@vscodebot vscodebot bot locked and limited conversation to collaborators Feb 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants