E222: Encoded Package Name — 패키지 이름이 인코딩되면 곤란해요

E222: Encoded Package Name — 패키지 이름이 인코딩되면 곤란해요

패키지 이름에 클래스패스에서 다르게 인코딩되는 문자가 포함되면 이 경고가 발생해요. 공백, 하이픈, 특수 기호처럼 인코딩이 필요한 문자가 패키지 이름에 들어 있으면, 파일 시스템에 쓸 때 변환이 일어나요. 예를 들어 p-qp$minusq가 됩니다. 그러면 디렉터리 이름이 패키지 이름과 달라지면서 도구들이 제대로 동작하지 못할 수 있어요.

참고: 아래 예시들은 package 문이 필요해서 Scaladoc의 스니펫 컴파일러로는 컴파일할 수 없어요.

출처: Scala 3 Reference

본문

Example

package `with spaces` {
  class Foo
}

Error

-- [E222] Syntax Warning: example.scala:1:8 ------------------------------------
1 |package `with spaces` {
  |        ^^^^^^^^^^^^^
  |The package name `with spaces` will be encoded on the classpath, and can lead to undefined behaviour.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Tools may not handle directories whose names differ from their corresponding package names.
  | For example, `p-q` is encoded as `p$minusq` when written to the file system.
  |
  | Package objects derive their names from the file names, so files such as `myfile.test.scala`
  | or `myfile-test.scala` can produce encoded names for the generated package objects.
  |
  | In this case, the name `with spaces` is encoded as `with$u0020spaces`.
   -----------------------------------------------------------------------------

Solution

인코딩이 필요 없는 패키지 이름을 사용하세요. 알파벳·숫자·언더스코어만 쓰는 겁니다.

package with_spaces {
  class Foo
}

아니면 특수 문자를 아예 피하는 명명 규칙을 쓰는 것도 돼요.

package withspaces {
  class Foo
}

파일 이름에 특수 문자가 들어 있는 경우(예: myfile-test.scala), 생성되는 패키지 객체가 인코딩된 이름을 갖게 된다는 점을 기억하세요. 이런 문제를 피하려면 파일 이름을 바꾸는 걸 고려해 보세요.