GitLab CI Python 黑色格式化程序说:会重新格式化,而运行黑色则不会重新格式化

a.t*_*.t. 10 python formatting continuous-integration gitlab python-black

当我在此提交上运行 GitLab CI 时

\n

有了这个gitlab-ci.yml:

\n
stages:\n    - format\n    - test\n\nblack_formatting:\n  image: python:3.6\n  stage: format\n  \n  before_script:\n    # Perform an update to make sure the system is up to date.\n    - sudo apt-get update --fix-missing\n    # Download miniconda.\n    - wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;\n    # Ensure the (mini) conda environment can be activated.\n    - export PATH="$HOME/miniconda/bin:$PATH"\n    # (Re)create the environment.yml file for the repository.\n    - conda env create -q -f environment.yml -n checkstyle-for-bash --force\n    # Activate the environment of the repository.\n    - source activate checkstyle-for-bash\n\n  script:\n    # Verify the Python code is black formatting compliant.\n    - black . --check --exclude \'\\.venv/|\\.local/|\\.cache/|\\.git/\'\n    # Verify the Python code is flake8 formatting compliant.\n    - flake8 .\n  allow_failure: false\n\n\ntest:pytest:36:\n  stage: test\n  image: python:3.6\n  script:\n    # Ensure the (mini) conda environment can be activated.\n    - export PATH="$HOME/miniconda/bin:$PATH"\n    # Activate the environment of the repository.\n    - source activate checkstyle-for-bash\n    # Run the python tests.\n    - python -m pytest\n
Run Code Online (Sandbox Code Playgroud)\n

它输出:

\n
Running with gitlab-runner 14.8.0 (565b6c0b)\n  on trucolrunner DS42qHSq\nPreparing the "shell" executor\n00:00\nUsing Shell executor...\nPreparing environment\n00:00\nRunning on pcname...\nGetting source from Git repository\n00:02\nFetching changes with git depth set to 20...\nReinitialized existing Git repository in /home/gitlab-runner/builds/DS42qHSq/0/root/checkstyle-for-bash/.git/\nChecking out 001577c3 as main...\nRemoving miniconda.sh\nSkipping Git submodules setup\nExecuting "step_script" stage of the job script\n02:55\n$ sudo apt-get update --fix-missing\nHit:1 http://nl.archive.ubuntu.com/ubuntu impish InRelease\nGet:2 http://security.ubuntu.com/ubuntu impish-security InRelease [110 kB]\nGet:3 http://nl.archive.ubuntu.com/ubuntu impish-updates InRelease [115 kB]\nHit:4 https://repo.nordvpn.com/deb/nordvpn/debian stable InRelease\nGet:5 http://nl.archive.ubuntu.com/ubuntu impish-backports InRelease [101 kB]\nHit:6 https://brave-browser-apt-release.s3.brave.com stable InRelease\nGet:7 http://security.ubuntu.com/ubuntu impish-security/main amd64 DEP-11 Metadata [20,3 kB]\nGet:8 http://security.ubuntu.com/ubuntu impish-security/universe amd64 DEP-11 Metadata [3.624 B]\nGet:9 http://nl.archive.ubuntu.com/ubuntu impish-updates/main amd64 DEP-11 Metadata [25,8 kB]\nGet:10 http://nl.archive.ubuntu.com/ubuntu impish-updates/universe amd64 DEP-11 Metadata [35,4 kB]\nGet:11 http://nl.archive.ubuntu.com/ubuntu impish-updates/multiverse amd64 DEP-11 Metadata [940 B]\nGet:12 http://nl.archive.ubuntu.com/ubuntu impish-backports/universe amd64 DEP-11 Metadata [16,4 kB]\nFetched 428 kB in 2s (235 kB/s)\nReading package lists...\n$ wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;\nPREFIX=/home/gitlab-runner/miniconda\nUnpacking payload ...\nCollecting package metadata (current_repodata.json): ...working... done\nSolving environment: ...working... done\n# All requested packages already installed.\ninstallation finished.\n$ export PATH="$HOME/miniconda/bin:$PATH"\n$ conda env create -q -f environment.yml -n checkstyle-for-bash --force\nCollecting package metadata (repodata.json): ...working... done\nSolving environment: ...working... done\nPreparing transaction: ...working... done\nVerifying transaction: ...working... done\nExecuting transaction: ...working... done\nInstalling pip dependencies: ...working... done\n$ source activate checkstyle-for-bash\n$ black . --check --exclude \'\\.venv/|\\.local/|\\.cache/|\\.git/\'\nwould reformat src/arg_parser.py\nwould reformat src/helper_text_parsing.py\nOh no!   \n2 files would be reformatted, 10 files would be left unchanged.\nERROR: Job failed: exit status 1\n
Run Code Online (Sandbox Code Playgroud)\n

但是,如果我black src/**在 GitHub 存储库上运行,则会返回黑色:

\n
~/git/checkstyle-for-bash$ git pull\nAlready up to date.\n(base) some@name:~/git/checkstyle-for-bash$ black src/**\nAll done! \xe2\x9c\xa8  \xe2\x9c\xa8\n8 files left unchanged.\n
Run Code Online (Sandbox Code Playgroud)\n

src/arg_parser.py以防万一我没有克隆正确的存储库,我还手动将文件内容从 GitLab复制粘贴到~/git/checkstyle-for-bash/src/arg_parser.py并再次运行black。然而,输出是相同的,它不会改变任何东西。

\n

为了完整起见,这是文件的内容src/arg_parser.py:

\n
# This is the main code of this project nr, and it manages running the code and\n# outputting the results to LaTex.\nimport argparse\n\n\ndef parse_cli_args():\n    # Instantiate the parser\n    parser = argparse.ArgumentParser(description="Optional app description")\n\n    # Include argument parsing for default code.\n    # Allow user to load a graph from file.\n    parser.add_argument(\n        "--ggl",\n        dest="google_style_guide",\n        action="store_true",\n        help=(\n            "boolean flag, determines whether the Google Style Guide for "\n            "Bash rules are followed."\n        ),\n    )\n\n    # Allow user to specify an infile.\n    parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))\n\n    # Specify default argument values for the parser.\n    parser.set_defaults(google_style_guide=True,)\n\n    # Load the arguments that are given.\n    args = parser.parse_args()\n    return args\n\n
Run Code Online (Sandbox Code Playgroud)\n

问题

\n

是什么导致 GitLab CI 说文件将被重新格式化为黑色(即使文件在其上运行黑色时不会重新格式化(在同一设备上(在不同的 conda 环境中)))?

\n

设置

\n

我在测试conda黑色命令的同一设备上运行自己的 GitLab CI。GitLab CI 一次复制一个存储库的 GitHub 提交,在其上运行 CI,然后将结果报告回 GitHub。

\n

我目前无法在 Clearnet 上公开我的 GitLab 服务器,因为我位于目前无法控制的网关后面。

\n

疑虑

\n

我相当确定这是我的一个“愚蠢”错误,但我还无法弄清楚它是什么。特别是因为我手动复制粘贴了该src/arg_parser.py文件的 GitLab 文件内容两次,并运行了两次 black 来验证 black 确实没有更改“那个”文件。另外,为了确保它不是尾随换行符或其他任何内容,我使用鼠标的复制按钮,而不是手动选择:\n在此输入图像描述

\n

此外,该文件符合 flake8 标准。我目前的猜测是,不知何故 CI 并未在该存储库的最新提交上运行。但是,为了验证我是否单击了失败提交的 GitLab,它确实重定向到了 GitLab 中的“05e85fd54f93ccfc427023b21f9cdb0c0cd6db2e”提交(副本):\n在此输入图像描述正是从这次提交中,我将src/arg_parser.py文件复制粘贴了两次。

\n

另一个猜测是gitlab-ci.yml加载一个 miniconda 环境,而我的本地版本的 black 使用完整的 conda 环境。也许它们有不同的换行符,这会导致格式差异。(尽管我怀疑情况会是这样)。

\n

问题

\n

我再次运行 CI,同时black . --diff在gitlab-ci.yml脚本中包含该命令,区别在于:

\n
\'\'\' Return\n
Run Code Online (Sandbox Code Playgroud)\n

和:

\n
\'\'\'Return\n
Run Code Online (Sandbox Code Playgroud)\n

如随附输出中所示:

\n
$ black . --diff --exclude \'\\.venv/|\\.local/|\\.cache/|\\.git/\'\n--- src/arg_parser.py   2022-04-03 10:13:10.751289 +0000\n+++ src/arg_parser.py   2022-04-03 11:11:26.297995 +0000\n@@ -24,10 +24,12 @@\n \n     # Allow user to specify an infile.\n     parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))\n \n     # Specify default argument values for the parser.\n-    parser.set_defaults(google_style_guide=True,)\n+    parser.set_defaults(\n+        google_style_guide=True,\n+    )\n \n     # Load the arguments that are given.\n     args = parser.parse_args()\n     return args\nwould reformat src/arg_parser.py\n--- src/helper_text_parsing.py  2022-04-02 19:35:45.142619 +0000\n+++ src/helper_text_parsing.py  2022-04-03 11:11:26.342908 +0000\n@@ -5,11 +5,11 @@\n def add_two(x):\n     return x + 2\n \n \n def get_function_line_nrs(filecontent, rules):\n-    """ Returns two lists containing the starting and ending line numbers of\n+    """Returns two lists containing the starting and ending line numbers of\n     the functions respectively.\n \n     :param filecontent: The content of the bash file that is being analysed.\n     :param rules: The Bash formatting rules that are chosen by the user.\n     """\nwould reformat src/helper_text_parsing.py\nAll done! \xe2\x9c\xa8  \xe2\x9c\xa8\n2 files would be reformatted, 10 files would be left unchanged.\n
Run Code Online (Sandbox Code Playgroud)\n

我不太清楚为什么会发生这种情况,因为我认为 python black 总是会收敛到给定有效 python 文件的完全相同的格式。我认为这是因为同一设备上的 miniconda 环境和 anaconda 环境之间使用了两个不同的黑色版本。

\n

为了测试这个假设,我black --version在gitlab-ci.yml.

\n

a.t*_*.t. 10

GitLab CI中使用的miniconda环境python black版本:

black, 22.3.0 (compiled: yes)
Run Code Online (Sandbox Code Playgroud)

而本地环境使用的python black版本:

black, version 19.10b0
Run Code Online (Sandbox Code Playgroud)

更新本地black版本,根据最新python black版本推送格式化代码,并在该 GitHub 提交上运行 GitLab CI,从而成功运行 GitLab CI。