Search code examples
ruby-on-railscredentialstrailing-newline

How to prevent VScode from adding a trailing newline to specific file extensions


You've edited your master.key or credentials.yml.enc file in VScode, and are running into issues when you saved the file because your editor is configured to ensure files have a trailing newline and rails doesn't strip the content before decrypting it.


Solution

  • I generally only read/write that type of file in Vi or BBEdit, but you can get around this in VScode by configuring those file extensions to use a specific Language Mode and then disable the setting for that Language Mode:

    // .vscode/settings.json
    {
      // Global Settings:
      "editor.tabSize": 2,
      "editor.indentSize": "tabSize",
      "files.insertFinalNewline": true,
      "files.trimTrailingWhitespace": true,
      "files.trimFinalNewlines": true,
      // Define the Language Mode for specific file extensions:
      "files.associations": {
        "*.enc": "shellscript",
        "*.key": "shellscript"
      },
      // Define override settings for specific Language Mode:
      "[shellscript]": {
        "files.insertFinalNewline": false
      }
    }
    

    Note: The settings documentation leaves something to be desired, but you effectively need to use an identifier recognized as a VScode valid Language Mode (i.e., the feature that VScode uses to determine which syntax highlighting to use for your file). I am using shellscript here, which is VScode's identifier for the Shell Script Language Mode.