Skip to content

invalid syntax highlighting when using backslash inside of raw string literals #3811

@ardnew

Description

@ardnew

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.24.2 linux/amd64
      
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.102.0
      cb0c47c0cfaad0757385834bd89d410c78a856c0
      x64
      
  • Check your installed extensions to get the version of the VS Code Go extension
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
    • # Tools Configuration
      
      
      ## Environment
      
      GOBIN: /opt/go/bin
      toolsGopath: 
      gopath: /opt/go
      GOROOT: /opt/go/lib
      PATH: /home/andrew/.local/bin:/opt/yubioath-flutter/bin:/opt/ov/bin:/opt/git-extras/bin:/opt/github-copilot/bin:/opt/mbuffer/bin:/opt/ino/bin:/opt/khadas/bin:/opt/teensy/loader/bin:/opt/packetry/bin:/opt/arduino/cli/bin:/opt/go/bin:/opt/go/lib/bin:/opt/rust/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin:/opt/rust/cargo/bin:/opt/typescript-go/bin:/opt/rr/bin:/opt/perl5/bin:/opt/node/var/node-v23.3.0-linux-x64/bin:/opt/node/bin:/opt/pipx/bin:/opt/kakoune/bin:/opt/neovim/bin:/opt/fzf/bin:/opt/password-store/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
      
      ## Tools
      
      	go:	/opt/go/lib/bin/go: go version go1.24.2 linux/amd64
      
      	gopls:	/opt/go/bin/gopls	(version: v0.19.1 built with go: go1.24.2)
      	gotests:	/opt/go/bin/gotests	(version: v1.6.0 built with go: go1.24.0)
      	impl:	/opt/go/bin/impl	(version: v1.4.0 built with go: go1.24.2)
      	goplay:	/opt/go/bin/goplay	(version: v1.0.0 built with go: go1.24.0)
      	dlv:	/opt/go/bin/dlv	(version: v1.24.0 built with go: go1.24.0)
      
      ## Go env
      
      Workspace Folder (src): /opt/envmux/src
      
      	AR='ar'
      	CC='gcc'
      	CGO_CFLAGS='-O2 -g'
      	CGO_CPPFLAGS=''
      	CGO_CXXFLAGS='-O2 -g'
      	CGO_ENABLED='1'
      	CGO_FFLAGS='-O2 -g'
      	CGO_LDFLAGS='-O2 -g'
      	CXX='g++'
      	GCCGO='gccgo'
      	GO111MODULE=''
      	GOAMD64='v1'
      	GOARCH='amd64'
      	GOAUTH='netrc'
      	GOBIN='/opt/go/bin'
      	GOCACHE='/home/andrew/.cache/go-build'
      	GOCACHEPROG=''
      	GODEBUG=''
      	GOENV='/home/andrew/.config/go/env'
      	GOEXE=''
      	GOEXPERIMENT=''
      	GOFIPS140='off'
      	GOFLAGS=''
      	GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3979143716=/tmp/go-build -gno-record-gcc-switches'
      	GOHOSTARCH='amd64'
      	GOHOSTOS='linux'
      	GOINSECURE=''
      	GOMOD='/opt/envmux/src/go.mod'
      	GOMODCACHE='/opt/go/pkg/mod'
      	GONOPROXY=''
      	GONOSUMDB=''
      	GOOS='linux'
      	GOPATH='/opt/go'
      	GOPRIVATE=''
      	GOPROXY='direct'
      	GOROOT='/opt/go/lib'
      	GOSUMDB='sum.golang.org'
      	GOTELEMETRY='local'
      	GOTELEMETRYDIR='/home/andrew/.config/go/telemetry'
      	GOTMPDIR=''
      	GOTOOLCHAIN='auto'
      	GOTOOLDIR='/opt/go/lib/pkg/tool/linux_amd64'
      	GOVCS=''
      	GOVERSION='go1.24.2'
      	GOWORK=''
      	PKG_CONFIG='pkg-config'
      	
      

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
Share all the settings with the go. or ["go"] or gopls prefixes.

Global settings.json

  // ---------------------------------------------------------------------------
  // === go
  // ---------------------------------------------------------------------------
  "go.buildFlags": [
    "-v",
    "-gcflags=all='-N -l'"
  ],
  "go.coverOnSingleTest": true,
  "go.coverOnSingleTestFile": true,
  "go.coverShowCounts": true,
  "go.delveConfig": {
    // "dlvLoadConfig": {
    //   "followPointers": true,
    //   "maxVariableRecurse": 1,
    //   "maxStringLen": 64,
    //   "maxArrayValues": 64,
    //   "maxStructFields": -1
    // },
    // "dlvLoadConfigFrom": "file",
    "dlvToolPath": "/opt/go/bin/dlv",
    "showGlobalVariables": true,
    "showLocalVariables": true,
    "showRegisters": true,
    "showThreadGroup": true,
    "showUnexportedVariables": true,
    "showUserDefinedTypes": true,
    "showGoroutineID": true,
    "showGoroutineName": true,
    "showLocation": true,
    "showFunctionArguments": true,
    "showFunctionLocals": true,
    "showFunctionVariables": true,
    "showFunctionReturns": true,
    "showBreakpointID": true,
    "showBreakpointName": true,
    "showBreakpointFunction": true,
    "showBreakpointFile": true,
    "showBreakpointLine": true,
    "showBreakpointCondition": true,
    "showBreakpointTracepoint": true,
    "showBreakpointHitCount": true,
    "showBreakpointLogMessage": true,
  },
  "go.diagnostic.vulncheck": "Imports",
  "go.languageServerFlags": [
    //"-gocodecompletion",
    //"-diagnostics",
    //"-godefinition",
    //"-profile.alloc=<path>"
    //"-profile.cpu=<path>"
    //"-profile.mem=<path>"
    //"-profile.trace=<path>"
    //"-rpc.trace",
    //"-v",
    //"-vv",
  ],
  "go.disableConcurrentTests": true,
  "go.editorContextMenuCommands": {
    "addImport": false,
    "addTags": false,
    "benchmarkAtCursor": false,
    "debugTestAtCursor": false,
    "fillStruct": true,
    "generateTestForFile": false,
    "generateTestForFunction": false,
    "generateTestForPackage": false,
    "playground": true,
    "removeTags": true,
    "testAtCursor": false,
    "testCoverage": false,
    "testFile": false,
    "testPackage": false,
    "toggleTestFile": false,
  },
  "go.enableCodeLens": {
    "runtest": true,
  },
  "go.survey.prompt": false,
  "go.terminal.activateEnvironment": false,
  "go.testExplorer.alwaysRunBenchmarks": true,
  "go.testExplorer.enable": false,
  "go.testExplorer.packageDisplayMode": "nested",
  "go.testExplorer.showDynamicSubtestsInEditor": false,
  "go.testExplorer.showOutput": true,
  "go.testFlags": [
    "-failfast",
    "-v",
  ],
  "go.testOnSave": false,
  "go.toolsManagement.autoUpdate": true,
  "go.toolsEnvVars": {
    // Create symlinks if necessary so that we can reuse this config across
    // multiple systems. There isn't a way to define per-system environments.
    "PATH": "/opt/go/bin:/opt/go/lib/bin:/opt/go/lib/pkg/tool/linux_amd64:/usr/bin:/usr/local/bin",
    "GOPATH": "/opt/go",
    "GOROOT": "/opt/go/lib",
  },
  // ---------------------------------------------------------------------------
  // === gopls
  // ---------------------------------------------------------------------------
  "gopls": {
    "build.buildFlags": [],
    "formatting.gofumpt": true,
    "formatting.local": "github.com/ardnew",
    "ui.codelenses": {
      "build": true,
      "gc_details": true,
      "generate": true,
      "regenerate_cgo": true,
      "run_govulncheck": true,
      "vendor": true,
      "test": true,
      "tidy": true,
      "upgrade_dependency": true
    },
    "ui.completion.completeFunctionCalls": true,
    "ui.completion.experimentalPostfixCompletions": true,
    "ui.completion.matcher": "Fuzzy",
    "ui.completion.usePlaceholders": true,
    "ui.diagnostic.analyses": {
      "fieldalignment": false,
      "nilness": true,
      "shadow": true,
      "unusedparams": true,
      "unusedwrite": true,
      "unusedfunc": false,
      "useany": true
    },
    "ui.diagnostic.analysisProgressReporting": true,
    "ui.diagnostic.diagnosticsDelay": "1s",
    "ui.diagnostic.staticcheck": true,
    "ui.documentation.hoverKind": "FullDocumentation",
    "ui.documentation.linksInHover": true,
    "ui.documentation.linkTarget": "pkg.go.dev",
    "ui.navigation.importShortcut": "Both",
    "ui.navigation.symbolMatcher": "FastFuzzy",
    "ui.navigation.symbolScope": "all",
    "ui.navigation.symbolStyle": "Full",
    "ui.semanticTokens": true,
  },

  "[go]": {
    "breadcrumbs.symbolSortOrder": "position",
    "debug.saveBeforeStart": "nonUntitledEditorsInActiveGroup",
    "editor.defaultFormatter": "golang.go",
    "editor.guides.bracketPairs": "active",
    "editor.guides.highlightActiveIndentation": "always",
    "editor.inlayHints.enabled": "offUnlessPressed",
    "editor.language.colorizedBracketPairs": [
      [
        "[",
        "]"
      ],
      [
        "{",
        "}"
      ],
      [
        "(",
        ")"
      ],
      [
        "'",
        "'"
      ],
      [
        "`",
        "`"
      ],
      [
        "\"",
        "\""
      ],
    ],
    "editor.guides.highlightActiveBracketPair": true,
    "editor.bracketPairColorization.independentColorPoolPerBracketType": true,
    "files.insertFinalNewline": false,
  },

Workspace .vscode/settings.json

  "go.buildFlags": [
    "-v",
    "-tags=pprof",
    "-gcflags=github.com/ardnew/envmux/...=-N -l"
  ],

  "go.diagnostic.vulncheck": "Imports",

  "go.lintTool": "golangci-lint",
  "go.lintOnSave": "workspace",
  "go.lintFlags": [
    "--config=.golangci.yml"
  ],
  "gopls": {
    "build.buildFlags": [
      "-tags=pprof",
      "-gcflags=github.com/ardnew/envmux/...=-N -l"
    ],
    "formatting.local": "github.com/ardnew/envmux",
    "ui.codelenses": {
      "build": true,
      "generate": true,
      "run_govulncheck": true,
      "test": true,
      "tidy": true,
      "upgrade_dependency": true,
    },
    "ui.diagnostic.analyses": {
      "fieldalignment": false,
      "nilness": true,
      "shadow": true,
      "staticcheck": true,
      "structtag": false,
      "unreachable": true,
      "unsafemarshal": true,
      "unsafemath": true,
      "unsafeprop": true,
      "unsafeptr": true,
      "unsafeslice": true,
      "unsafetype": true,
      "unusedfunc": true,
      "unusedparams": true,
      "unusedwrite": true,
      "useany": true,
    },
  },

Describe the bug

Using a backslash character \ inside of a raw string literal `...` causes the syntax highlighter to highlight all following code as a string literal.

I would expect the syntax highlighter to stop using string highlighting at the end of the raw string literal — i.e., the terminating backtick `.

Steps to reproduce the behavior:

  1. Create a new Go source file parse/src.go with the following code:
package parse

func normalHighlighting() {
}

const rawStringLiteral = `\`

func mySyntaxHighlightingIsBroken() {
	// You can trick the highlighter to end the string highlighting
	// by inserting a single backtick anywhere in a comment, like this:
	// `
	const raw = "but it still thinks the following brace '}' is unbalanced"	
}

  1. Observe that lines 7-11 are highlighted as if they are the content of rawStringLiteral

Screenshots or recordings

See the following screenshot:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions