firecow/gitlab-ci-local

backslash then double slash within double quotes throws syntax error

tan-wei-xin-alez opened this issue · 2 comments

Minimal .gitlab-ci.yml illustrating the issue

---

stages:
  - test

test-job:
  stage: test
  image: public.ecr.aws/docker/library/python:latest
  tags:
    - <runner_tag>
  rules:
    - if: $TEST_URL =~ $REGEX_VAR
  variables:
    TEST_URL: "test/url"
    REGEX_VAR: "/test\//"
  script:
    - echo 'test'

The above throws the following syntax error when trying to use --preview or running the job itself

➜  ~ rm -rf <project_dir>/.gitlab-ci-local && gitlab-ci-local --cwd <project_dir> --preview
Using fallback git commit data
Unable to retrieve default remote branch, falling back to `main`.
Using fallback git remote data
SyntaxError: Unexpected token ')'
    at Function.evaluateRuleIf (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:221:29)
    at Function.getRulesResult (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:185:24)
    at new Job (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/job.ts:216:38)
    at /home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:176:29
    at Function.forEachRealJob (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:54:13)
    at Parser.init (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:162:15)
    at Function.create (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:60:9)
    at handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/handler.ts:45:18)
    at Object.handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/index.ts:37:21)

➜  ~ rm -rf <project_dir>/.gitlab-ci-local && gitlab-ci-local --cwd <project_dir> test-job
Using fallback git commit data
Unable to retrieve default remote branch, falling back to `main`.
Using fallback git remote data
SyntaxError: Unexpected token ')'
    at Function.evaluateRuleIf (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:221:29)
    at Function.getRulesResult (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:185:24)
    at new Job (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/job.ts:216:38)
    at /home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:176:29
    at Function.forEachRealJob (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:54:13)
    at Parser.init (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:162:15)
    at Function.create (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:60:9)
    at handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/handler.ts:78:18)
    at Object.handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/index.ts:37:21)

Expected behavior
image

Host information

➜  ~ fastfetch
                            ....               
              .',:clooo:  .:looooo:.           ------------------------
           .;looooooooc  .oooooooooo'          OS: Ubuntu focal 20.04 x86_64
        .;looooool:,''.  :ooooooooooc          Host: Precision 5570
       ;looool;.         'oooooooooo,          Kernel: 5.15.0-88-generic
      ;clool'             .cooooooc.  ,,       Uptime: 26 days, 23 hours, 27 mins
         ...                ......  .:oo,      Packages: 2267 (dpkg), 15 (snap), 206 (brew)
  .;clol:,.                        .loooo'     Shell: zsh 5.9
 :ooooooooo,                        'ooool     Display (LG HDR 4K): 3840x2160 @ 60Hz *
'ooooooooooo.                        loooo.    Display (SHP1515): 1920x1200 @ 60Hz
'ooooooooool                         coooo.    Display (ARZOPA): 1920x1080 @ 30Hz
 ,loooooooc.                        .loooo.    Display (SyncMaster): 1920x1200 @ 60Hz
   .,;;;'.                          ;ooooc     DE: Gnome 3.36.9
       ...                         ,ooool.     WM: Mutter (X11)
    .cooooc.              ..',,'.  .cooo.      WM Theme: Yaru
      ;ooooo:.           ;oooooooc.  :l.       Theme: Yaru [GTK2/3/4]
       .coooooc,..      coooooooooo.           Icons: Yaru [GTK2/3/4]
         .:ooooooolc:. .ooooooooooo'           Font: Ubuntu (11pt) [GTK2/3/4]
           .':loooooo;  ,oooooooooc            Cursor: Yaru (24px)
               ..';::c'  .;loooo:'             Terminal: tmux 3.4
                             .                 CPU: 12th Gen Intel(R) Core(TM) i9-12900H (20) @ 5.00 GHz
                                               GPU 1: NVIDIA Device 25BA (3D)
                                               GPU 2: Intel Device 46A6 (VGA compatible)
                                               Memory: 23.41 GiB / 62.47 GiB (37%)
                                               Swap: 1.98 GiB / 2.00 GiB (99%)
                                               Disk (/): 358.88 GiB / 929.04 GiB (39%) - ext4
                                               Local IP (enx349971e7b9cf): 192.168.178.48/24 *
                                               Battery: 100% [AC Connected]
                                               Locale: en_US.UTF-8

➜  ~ gitlab-ci-local --version
4.53.0

Containerd binary

➜  ~ docker version
Client: Docker Engine - Community
 Version:           27.2.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        9e34c9b
 Built:             Fri Sep  6 12:08:15 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.2.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       8b539b8
  Built:            Fri Sep  6 12:08:15 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.21
  GitCommit:        472731909fa34bd7bc9c087e4c27943f9835f111
 nvidia:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Additional context
Changing REGEX_VAR: "/test\//" to REGEX_VAR: '/test\//' seems to get around the issue although it seems strange that the backslash would not be interpreted literally in that case, also works with REGEX_VAR: "/test\/" but I believe that's not the same thing?

Using REGEX_VAR: '/test\/', on the other hand, throws a different syntax error

➜  ~ rm -rf <project_dir>/.gitlab-ci-local && gitlab-ci-local --cwd <project_dir> test-job
Using fallback git commit data
Unable to retrieve default remote branch, falling back to `main`.
Using fallback git remote data
SyntaxError: Invalid regular expression: missing /
    at Function.evaluateRuleIf (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:221:29)
    at Function.getRulesResult (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:185:24)
    at new Job (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/job.ts:216:38)
    at /home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:176:29
    at Function.forEachRealJob (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/utils.ts:54:13)
    at Parser.init (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:162:15)
    at Function.create (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/parser.ts:60:9)
    at handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/handler.ts:45:18)
    at Object.handler (/home/linuxbrew/.linuxbrew/Cellar/gitlab-ci-local/4.53.0/libexec/lib/node_modules/gitlab-ci-local/src/index.ts:37:21)

I don't know if the above is specific to gitlab's kubernetes executor but if it is, feel free to close this issue and link it to #1064

I don't know if the above is specific to gitlab's [kubernetes executor]

nope, doesnt seemed like it

For now, change to either of the following should work similar in gitlab.com and gcl:

variables:
    REGEX_VAR: /test\//
    REGEX_VAR: '/test\//'
    
     # NOTE: we are using double the backslash `\\`!  this is due to how the yaml parsers parses double quoted string and the edge case that's not handled on our side
    REGEX_VAR: "/test\\//"