Trouble Shooting

Git CRLF

oxdjww 2023. 10. 22. 16:09
728x90
반응형

Overview

Git clone시 OS간 차이에서 발생하는 빌드 문제를 해결하는 방법을 다룬다.

Issue

평범하게 자바 소스코드를 Git clone 받았다.

그리고, 정상적으로 실행이 되는지

$ ./gradlew clean test

를 진행했다.
하지만 아래와 같은 오류를 만날 수 있었다.

zsh: ./gradlew: bad interpreter: /bin/sh^M: no such file or directory

처음에는 단순한 오류인줄 알고, 늘 그렇듯이 로그를 바탕으로 구글링을 했다.

정보를 찾아본 결과, OS간 개행문자를 처리하는 방식이 달라서 gradlew 파일이 git clone시 변화됐음을 확인할 수 있었다.

remote 파일

#!/bin/sh

#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
#
#   Gradle start up script for POSIX generated by Gradle.
#
#   Important for running:
#
#   (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
#       noncompliant, but you have some other compliant shell such as ksh or
#       bash, then to run this script, type that shell name before the whole
#       command line, like:
#
#           ksh Gradle
#
#       Busybox and similar reduced shells will NOT work, because this script
#       requires all of these POSIX shell features:
#         * functions;
#         * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
#           «${var#prefix}», «${var%suffix}», and «$( cmd )»;
#         * compound commands having a testable exit status, especially «case»;
#         * various built-in commands including «command», «set», and «ulimit».
#
#   Important for patching:
#
#   (2) This script targets any POSIX shell, so it avoids extensions provided
#       by Bash, Ksh, etc; in particular arrays are avoided.
#
#       The "traditional" practice of packing multiple parameters into a
#       space-separated string is a well documented source of bugs and security
#       problems, so this is (mostly) avoided, by progressively accumulating
#       options in "$@", and eventually passing that to Java.
#
#       Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
#       and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
#       see the in-line comments for details.
#
#       There are tweaks for specific operating systems such as AIX, CygWin,
#       Darwin, MinGW, and NonStop.

...

clone 받은 local 파일

그러므로, 크로스 플랫폼에서 발생하는 이런 문제를 해결하기 위해 더 검색을 해보았다.

윈도우에서 작성한 스크립트 파일을 unix 기반인 Mac OS에서 다르게 받아들인 것이다.
unix 기반 OS에서는 개행문자를 처리하는 방식이 달라서 ^M으로 처리하기 때문에, 문제가 발생한 스크립트에서 ^M을 모두 지우면 해결된다는 것이다.

Solution1

그렇기에, brew를 활용하여 dos2unix 라이브러리를 설치하고 해당 파일을 실행해주면 된다.

$ ./dos2unix gradlew

그 후에는 정상적으로 $ ./gradlew clean test를 실행할 수 있었다.

Solution2

하지만 이 방법보다 보다 근본적인 방법을 찾을 수 있었다.
왜냐하면, 같은 Mac OS(arm64)를 사용하는 다른 친구는 문제가 없었기 때문이다.

git clone 시 개행문자 처리에 대해 다룬 레퍼런스를 찾아보았다.

해당 포스팅에서 git config 수정을 통해 크로스 플랫폼시 개행문자를 처리하는 옵션을 설정하는 방법에 대해 알 수 있었다.

$ git config –global core.autocrlf {option}

깃(리모트 서버)와 로컬 사이에서 파일을 주고 받을 때, 개행 문자를 처리하는 방식을 바꿀 수 있었다.

기존에 true값으로 되어있던 설정을 input으로 바꿔주어 windows 환경에서의 개행문자 처리 방식인 CRLF를 Unix기반 처리 방식인 LF로 바꾸어줄 수 있었다.

그렇게 해결할 수 있었다.

감사합니다.

728x90
반응형