Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

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.

VSCode setting "Readonly Include": Configure paths or glob patterns to mark as read-only. Glob patterns are always evaluated relative to the path of the workspace folder unless they are absolute paths. You can exclude matching paths via the Files: Readonly Exclude setting. Files from readonly file system providers will always be read-only independent of this setting.

And voila! I can't edit these generated files in VS Code anymore now.

VS Code editor showing a tooltip telling that this file is readonly.

The trick is not to define this setting in your global config but rather in your workspace or project config at your-project/.vscode/settings.json.

{
  "files.readonlyInclude": {
    "src/app/core/generated-models/**": true
  }
}

Now, no VS Code user will edit the generated files. Win win!

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 10 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles