Gleam 如何用 git 依赖或本地路径依赖引用未发布到 Hex 的包?
【免费下载链接】gleam⭐️ A friendly language for building type-safe, scalable systems!项目地址: https://gitcode.com/GitHub_Trending/gl/gleam
当你要依赖的 Gleam 包还没有发布到 Hex(例如内部工具包、正在开发的库、或私有仓库),Gleam 的gleam.toml支持两种替代方式:用git指向仓库地址,或用path指向本地目录。本文给出两种方式的配置写法、更新与校验命令,以及manifest.toml锁定结果的检查方法。
配置 git 依赖
在项目的gleam.toml中,用git和ref两个字段指向仓库和版本(ref 可以是提交哈希、标签或分支名):
[dependencies] gleam_stdlib = { git = "https://github.com/gleam-lang/stdlib.git", ref = "957b83b" }上面的配置直接来自仓库中的示例项目 test/project_git_deps/gleam.toml,它把gleam_stdlib从 git 仓库拉取而不是从 Hex 安装。git 依赖从 v1.9.0 起由构建工具支持,见 changelog/v1.9.md。
配置写好后执行gleam update拉取依赖,再用gleam check做类型检查。对应的完整示例项目在 test/project_git_deps/,其源码 test/project_git_deps/src/git_deps.gleam 中import gleam/io就是经由 git 依赖提供的:
import gleam/io pub fn main() { // gleam/io is provided via a git dependency io.println("Hello, world!") }配置本地路径依赖
依赖就在本地磁盘上(比如同机的另一个项目目录)时,用path字段指向该目录。写法来自 compiler-core/src/requirement.rs 的测试用例:
[dependencies] local = { path = "/path/to/package" }仓库中的 test/project_path_deps/ 是一组用于 CI 的本地路径依赖项目。test/project_path_deps/README.md 描述了四个项目的依赖关系:
_-> project_b -_ / v project_a project_d \ ^ '-> project_c -'以 test/project_path_deps/project_a/gleam.toml 为例,它用相对路径引用同级的project_b和project_c:
[dependencies] project_b = { path = "../project_b" } project_c = { path = "../project_c" }用 path 字段引用 git 仓库中的子目录(monorepo)
当多个 Gleam 包放在同一个 git 仓库(monorepo)的不同子目录时,git 依赖可以再加一个可选的path字段指定子目录。此功能在 v1.18.0 引入,见 changelog/v1.18.md:
[dependencies] my_package = { git = "https://github.com/example/monorepo", ref = "main", path = "packages/my_package", }仓库中 test/project_git_deps_path/test.sh 用file://本地仓库完整测试了这一场景,包括:
- 同一仓库中引用两个包(
path = "package_a"与path = "package_b"); - 用分支名
ref = "main"代替提交哈希; - 分支上新增提交后再次
gleam update,会刷新传递依赖的包内容。
更新、锁定与校验
修改gleam.toml中的依赖声明后,执行:
gleam updategleam update会解析 git 依赖并生成/更新manifest.toml锁文件。manifest.toml由 Gleam 自动生成,通常不需要手动编辑。git 依赖锁定后会记录解析到的具体提交,参考 test/project_git_deps/manifest.toml:
packages = [ { name = "gleam_stdlib", version = "0.54.0", build_tools = ["gleam"], requirements = [], source = "git", repo = "https://github.com/gleam-lang/stdlib.git", commit = "957b83bbb6103aa0d96c148ce7409243681cf1ab" }, ]锁文件同时保留了requirements一节,内容与gleam.toml中的声明一致。带path字段的 git 依赖会额外在包条目中带上path字段(见 test/project_git_deps_path/test.sh 中对manifest.toml的断言,形如source = "git", repo = "file://...", commit = "...", path = "package_b")。
配置正确后,用gleam check验证整个项目能通过类型检查:
gleam checktest/project_git_deps_path/test.sh 中每次修改gleam.toml依赖声明后都按gleam update→gleam check的顺序验证。
配置限制与失败现象
以下约束直接来自 compiler-core/src/requirement.rs 的解析校验及对应快照文件:
git 依赖的
path不能为空字符串,否则解析报错:git dependency path must not be empty见 compiler-core/src/snapshots/gleam_core__requirement__tests__read_git_requirement_with_empty_path.snap。
git 依赖的
path不能包含..段(不允许跳出仓库根目录):paths must not contain .. segments见 compiler-core/src/snapshots/gleam_core__requirement__tests__read_git_requirement_with_escaping_path.snap。
ref无法解析时gleam update会失败:test/project_git_deps_path/test.sh 中用ref = "no-such-ref"配置后断言gleam update应当返回失败,而不是静默忽略。
此外,v1.17 修复过一个问题:之前gleam update会错误地去 Hex 检查本地或 git 依赖的新主版本,见 changelog/v1.17.md;升级后可避免这类干扰。
小结
- 未发布到 Hex 的包用
git依赖(git+ref,monorepo 再加path)或本地路径依赖(path)引用。 - 修改
gleam.toml后运行gleam update生成manifest.toml锁定,再用gleam check做类型检查验证。 manifest.toml中source = "git"且commit为完整提交哈希,说明 git 依赖已被正确锁定。- git 依赖的
path不能为空、不能含..;ref无法解析时gleam update会直接报错。
【免费下载链接】gleam⭐️ A friendly language for building type-safe, scalable systems!项目地址: https://gitcode.com/GitHub_Trending/gl/gleam
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考