Add terraform file support for blob viewer in repository

What does this MR do?

This merge request resolves Issue #514908, where .tf (Terraform) files are improperly highlighted or shown without syntax highlighting in the blob viewer due to the absence of Terraform support in Highlight.js.


Why was this needed?

  • Highlight.js does not officially support Terraform or HCL.
  • Although a highlightjs-terraform repository exists, it is not maintained or published to npm.
  • GitLab currently attempts to highlight .tf files using Highlight.js, which leads to rendering issues such as misinterpretation of the . operator in expressions like data.terraform_remote_state.backend.outputs.value.

What’s in the patch?

Allows GitLab to fall back to server-side syntax highlighting using Rouge
Adds a Ruby test to validate blob viewer rendering behavior for .tf files


Why Rouge?

GitLab already uses Rouge on the backend for syntax highlighting and it supports:

  • Rouge::Lexers::HCL

This change leverages the existing Rouge lexers for proper and consistent highlighting of Terraform files.


How was it tested?

  • Added a sample test.tf file using common Terraform constructs such as provider, data sources, outputs, and chained expressions.
  • Verified that it renders .tf syntax correctly in the blob viewer.
  • Confirmed blob viewer tests pass with the added Ruby test case.

Screenshot / Visual (if applicable)

image

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #514908

Edited by Munish

Merge request reports

Loading