Readonly files in VSCode
- Published at
- Updated at
- Reading time
- 2min
Here's a quick tip from the excellent vscode.email newsletter: VS Code allows you to mark files as readonly.
When would you use it?
Many applications include generated files, which could be data models or schemas. These files are often checked into version control and only updated periodically. People know they shouldn't touch these files because they start with a big warning — do not edit this file manually.
Here's a file from my current project.
/**
* ...
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
Do people read all the comments? No. Does this prevent me from changing the file manually? Also, no.
I love the idea of solving this problem at the editor level.
The "Readonly include" option accepts files or glob patterns.
And voila! I can't edit these generated files in VS Code anymore now.
The trick is not to define this setting in your global config but rather in your workspace or project config at your-project/
.
{
"files.readonlyInclude": {
"src/app/core/generated-models/**": true
}
}
Now, no VS Code user will edit the generated files. Win win!
Join 5.5k readers and learn something new every week with Web Weekly.