Gleamでローカルのパッケージを参照する

2024/12/31に公開

公式ドキュメントに記載されている方法になります。

gleam.tomlの[dependencies]にディレクトリパスを指定することでローカルのパッケージを参照することができます。

公式ドキュメントより引用
# Local dependencies can be specified with a path
my_other_project = { path = "../my_other_project" }

https://gleam.run/writing-gleam/gleam-toml/

気をつけるべきポイントとして、ローカルのパッケージもgleam buildをしておく必要があることです。
gleam buildをしない場合、エラーや警告の原因になります。

警告が出る例
> gleam build
  Compiling sample_dotenv_gleam
warning: Transitive dependency imported
  ┌─ /Users/home/Project/sandbox/gleam/sample_dotenv_gleam/src/sample_dotenv_gleam.gleam:2:1
  │
2import envoy
  │ ^^^^^^^^^^^^

The module `envoy` is being imported, but `envoy`, the package it belongs
to, is not a direct dependency of your package.
In a future version of Gleam this may become a compile error.

Run this command to add it to your dependencies:

    gleam add envoy


   Compiled in 0.38s

上記の例では、envoyのパッケージを追加するよう警告されていますが、ローカルのパッケージをbuildすることでパッケージを追加せずに警告を回避できました。

Discussion