Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .dagger/modules/e2e/clients.dang
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type ClientChecks {
# from this name by hand.
let selfPath: String! = ".dagger/modules/self-app"
let selfSourcePath: String! = "src/main/java/io/dagger/modules/selfapp/SelfApp.java"
# The client package path in workspaceClientPath is derived from the module
# name of workspaceDepPath by hand.
let workspaceDepPath: String! = ".dagger/modules/ws-dep"
let workspaceAppPath: String! = ".dagger/modules/ws-app"
let workspaceRuntimePath: String! = ".dagger/modules/ws-runtime"
let workspaceClientPath: String! = workspaceAppPath + "/clients/ws-dep/src/main/java/io/dagger/client/modules/wsdep/WsDep.java"

let assert(condition: Boolean!, message: String!): Void {
if (condition == false) { raise message }
Expand Down Expand Up @@ -298,6 +304,86 @@ type ClientChecks {
null
}

"""
A workspace opened from a Git ref generates the files its checkout
generates. There the engine resolves a client declared by its path in the
workspace as a Git source at the workspace commit. Generation still binds
it by path: a module on the Dang entrypoint loads it by its path from the
workspace root, and a module on the runtime records it as a dependency
relative to its manifest. A commit of the generated files then stays clean
when generated from that commit, with the ref's selector and without one.
"""
pub gitWorkspaceGenerateCheck(ws: Workspace!): Void @check {
let config = ws.file("/.dagger/modules/engine-e2e/workspace.toml").contents +
workspaceScope(workspaceAppPath, "ws-app", "") +
workspaceScope(workspaceRuntimePath, "ws-runtime", "settings.dangEntrypoint = false\n")
let generated = cli(
playground(ws)
.withNewFile("dagger.toml", config)
.withNewFile(workspaceDepPath + "/dagger-module.toml", "name = \"ws-dep\"\nengineVersion = \"v1.0.0-0\"\n\n[runtime]\n source = \"dang\"\n")
.withNewFile(workspaceDepPath + "/main.dang", "type WsDep {\n pub greeting: String! { \"hello from ws-dep\" }\n}\n"),
["--auto-apply", "generate"],
)
let client = generated.file(workspaceClientPath).contents
assert(
client.contains("ModuleTarget.local(\"/" + workspaceDepPath + "\")"),
"the checkout does not bind the client by its path:\n" + client,
)
let manifest = generated.file(workspaceRuntimePath + "/dagger-module.toml").contents
assert(
manifest.contains("source = \"../ws-dep\""),
"the checkout does not record the dependency by its path:\n" + manifest,
)
assertGeneratesNothing(generated, "", "generating from the checkout")

let server = container
.from("alpine:3.22")
.withoutEntrypoint
.withExec(["apk", "add", "--no-cache", "git", "git-daemon"])
.withDirectory("/srv/repo", generated.directory(playgroundRoot), exclude: [".git"])
.withWorkdir("/srv/repo")
.withExec(["git", "init", "-q", "-b", "main"])
.withExec(["git", "add", "-A"])
.withExec(["git", "-c", "user.name=e2e", "-c", "user.email=e2e@example.com", "commit", "-q", "-m", "generated scopes"])
.withExec(["git", "clone", "-q", "--bare", ".", "/srv/repo.git"])
.withExposedPort(9418)
.withDefaultArgs(["git", "daemon", "--export-all", "--base-path=/srv", "/srv"])
.asService
# The engine takes a Git ref whose host has no dot for a Go import path
# and refuses it, so the ref names the server by its full name, in the
# session domain that resolv.conf searches last.
let repo = "git://" + server.hostname + ".$(awk '/^search/ {print $NF}' /etc/resolv.conf)/repo.git"
let served = generated.withServiceBinding(server.hostname, server)
assertGeneratesNothing(served, "-W \"" + repo + "@main\"", "generating from the Git ref at main")
assertGeneratesNothing(served, "-W \"" + repo + "\"", "generating from the Git ref without a selector")
null
}

let workspaceScope(path: String!, name: String!, settings: String!): String! {
"\n[sdks.java.scopes.\"" + path + "\"]\nis-module = true\nname = \"" + name + "\"\n" +
"clients = [\"./" + workspaceDepPath + "\"]\n" + settings
}

"""
The CLI reports an empty changeset in a log line, which plain progress
prints. It prints a changeset it does not apply as a summary near the end
of its output, so the tail is enough to tell what changed.
"""
let assertGeneratesNothing(ctr: Container!, flags: String!, what: String!): Void {
let attempt = ctr.withExec(
["sh", "-c", "dagger --progress=plain " + flags + " generate --require-load --no-apply"],
experimentalPrivilegedNesting: true,
expect: ReturnType.ANY,
)
let lines = attempt.stderr.split("\n")
let skipped = if (lines.length > 80) { lines.length - 80 } else { 0 }
assert(
attempt.exitCode == 0 and attempt.stderr.contains("no changes to apply") and
attempt.stderr.contains("Generated changes were not applied") == false,
what + " changed the files generated from the checkout:\n" + lines.dropFirst(skipped).join("\n"),
)
}

let callerSource: String! {
"package io.dagger.modules.hopa;\n"
+ "\n"
Expand Down
37 changes: 37 additions & 0 deletions .dagger/modules/e2e/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,43 @@ type E2e {
null
}

"""
A Git client is a module of the Git workspace that generates it when it is
in the workspace's repository, at its commit, and at its selector or in a
workspace that names none. The cases follow the Go SDK's TestWorkspaceModule
and TestWorkspaceModuleExplicitGitURL (dagger/go-sdk#73, #78 and #79).
"""
workspaceRefMatchCheck: Void @check {
let sha = "419c4afea78603265c40e273c455623a0291922e"
let other = "0000000000000000000000000000000000000000"
let clone = "http://h/r.git"
let github = "https://github.com/o/r"
[
["the workspace at the commit", clone + "@" + sha, sha, clone, clone + "/app@" + sha, sha, "true"],
["a workspace without a selector", clone, sha, clone, clone + "/app@main", sha, "true"],
["a workspace in a subdirectory", clone + "/ci@main", sha, clone, clone + "/app@main", sha, "true"],
["another selector at the same commit", clone + "@main", sha, clone, clone + "/app@release", sha, "false"],
["another commit", clone + "@main", other, clone, clone + "/app@main", sha, "false"],
["another repository", clone + "-fork@" + sha, sha, clone, clone + "/app@" + sha, sha, "false"],
["an unknown workspace commit", clone + "@" + sha, "", clone, clone + "/app@" + sha, sha, "false"],
["a module without a pin", clone + "@" + sha, sha, clone, clone + "/app@" + sha, "", "false"],
["a GitHub address without a scheme", "github.com/o/r@" + sha, sha, github, github + "/app@" + sha, sha, "true"],
["another host without a scheme", "gitlab.com/o/r@" + sha, sha, github, github + "/app@" + sha, sha, "false"],
["a repository the clone ref is a prefix of", "github.com/o/r2@" + sha, sha, github, github + "2/app@" + sha, sha, "false"],
["the explicit Git URL form at the commit", clone + "@" + sha, sha, clone, clone + "#" + sha + ":app", sha, "true"],
["the explicit Git URL form on GitHub", "github.com/o/r@" + sha, sha, github, github + "#" + sha + ":app", sha, "true"],
["the explicit Git URL form at the repository root", "github.com/o/r@" + sha, sha, github, github + "#" + sha, sha, "true"],
["the explicit Git URL form at a branch", clone + "@main", sha, clone, clone + "#main:app", sha, "true"],
["the explicit Git URL form at another selector", clone + "@main", sha, clone, clone + "#release:app", sha, "false"],
["the explicit Git URL form in another repository", "github.com/o/r@" + sha, sha, github + "-fork", github + "-fork#" + sha + ":app", sha, "false"],
].each { row =>
let name = row[0] ?? ""
let got = javaSdk.workspaceRefMatches(row[1] ?? "", row[2] ?? "", row[3] ?? "", row[4] ?? "", row[5] ?? "")
assert(got == (row[6] == "true"), name + ": want " + (row[6] ?? ""))
}
null
}

"""
A module that reaches core over the session Dagger.dag() holds and over a
session it names, with a core type in its signature.
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ generated when the engine loads the module**. The module builds with a plain
> [`github.com/dagger/sdk-helpers`](https://github.com/dagger/sdk-helpers), the
> manifest builder that
> [dagger/dagger#13992](https://github.com/dagger/dagger/pull/13992) moved out of
> the engine, pinned at its `v1.0.2` tag.
> the engine, pinned at its `v1.0.5` tag.

## Install

Expand Down Expand Up @@ -234,9 +234,11 @@ String greeting = clientDep().greeting();

A client loads its target with `serveModule` when the module first calls it. A
local target is named by its path from the workspace root, and the engine
resolves that path in the calling module's own tree. A git target is loaded at
the commit its ref resolved to when the client was generated, even after a newer
tag matches the ref; `dagger generate` moves the client to the newer commit.
resolves that path in the calling module's own tree. A client declared by a
path within the workspace keeps that binding when the workspace is opened from
Git. A git target is loaded at the commit its ref resolved to when the client
was generated, even after a newer tag matches the ref; `dagger generate` moves
the client to the newer commit.

A client types each check of its target as the core object `Check`, whatever
type the check's author wrote and whatever engine version the target declares.
Expand Down Expand Up @@ -293,7 +295,8 @@ dependency set instead. Each client is recorded in the manifest the module has
`dagger-module.toml`, or the `dagger.json` of a pre-1.0 module — and its types
are generated into the core package, so module code reaches it through
`core()`, as `core().clientDep()`. A client that is removed is dropped from
both.
both. A client declared by a path within the workspace keeps that binding when
the workspace is opened from Git.

> [!WARNING]
> On the runtime, the client set is the *whole* dependency set. A module that
Expand Down
2 changes: 1 addition & 1 deletion dagger-module.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ engineVersion = "v1.0.0-beta.18"

[[dependencies]]
name = "sdk-helpers"
source = "github.com/dagger/sdk-helpers@v1.0.2"
source = "github.com/dagger/sdk-helpers@v1.0.5"
2 changes: 1 addition & 1 deletion dagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": [
{
"name": "sdk-helpers",
"source": "github.com/dagger/sdk-helpers@v1.0.2"
"source": "github.com/dagger/sdk-helpers@v1.0.5"
}
]
}
2 changes: 2 additions & 0 deletions dagger.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
["","git-latest",["github.com/dagger/alpine"],"refs/heads/main"]
["","git-latest",["github.com/dagger/dang-sdk"],"refs/heads/main"]
["","git-latest",["github.com/dagger/sdk-helpers",["version","v1.0.2"]],"refs/tags/v1.0.2"]
["","git-latest",["github.com/dagger/sdk-helpers",["version","v1.0.5"]],"refs/tags/v1.0.5"]
["","git-sha",["github.com/containernetworking/plugins","refs/tags/v1.9.0"],"9b3772e1a7abf93cbb7c6526a28bc0d27b830e02"]
["","git-sha",["github.com/dagger/alpine","main"],"ff6a5f4c28364912d92c00e682dd0bf19f524ef9"]
["","git-sha",["github.com/dagger/alpine","refs/heads/main"],"ff6a5f4c28364912d92c00e682dd0bf19f524ef9"]
["","git-sha",["github.com/dagger/dagger","refs/tags/v1.0.0-beta.11"],"a4e1e4ff663e5e51c2b96c2c0772f3d2f00cfb94"]
["","git-sha",["github.com/dagger/dang-sdk","refs/heads/main"],"9c8e5c6f1927b9adc325846d47e66e697991797d"]
["","git-sha",["github.com/dagger/sdk-helpers","refs/tags/v1.0.2"],"e5e38a5f0778c5a804931d95e5d0d9953668049a"]
["","git-sha",["github.com/dagger/sdk-helpers","refs/tags/v1.0.5"],"2d593321b19e3519584b1bbd6e77d83b2be9de2f"]
["","git-sha",["github.com/libfuse/sshfs","refs/tags/sshfs-3.7.6"],"7a2d988775446ebe7af9b01c99b3b8e86bddb05a"]
["","git-sha",["github.com/opencontainers/runc","refs/tags/v1.4.2"],"c241c0bb5e60a8e8c1b2e53d4eca8d0068d8d57e"]
["","oci-latest",["docker.io/library/busybox"],"1.38.0"]
Expand Down
44 changes: 44 additions & 0 deletions main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,50 @@ type JavaSdk {
.filter { label => label != "" }
}

"""
Whether a Git client is a module of the Git workspace, at the workspace
commit. Both ref formats are read: the workspace address keeps
<clone>/<path>@<version>, while from engine v1.0.0-beta.20 a module of the
workspace prints <clone>#<version>[:<subpath>]. URL schemes are ignored,
because a GitHub address has none while its clone ref has https://.
Selectors are compared as written, so an alias such as HEAD for main keeps
the Git binding.
"""
workspaceRefMatches(address: String!, commit: String!, cloneRef: String!, ref: String!, pin: String!): Boolean! {
if (cloneRef == "" or pin == "" or pin != commit) {
false
} else {
let workspaceSelector = gitSelector(address, cloneRef)
let moduleSelector = gitSelector(ref, cloneRef)
if (workspaceSelector == null or moduleSelector == null) {
false
} else {
workspaceSelector == "" or workspaceSelector == moduleSelector
}
}
}

"""
The selector of a ref of the repository at cloneRef, empty when the ref
names none, or null for a ref of another repository.
"""
let gitSelector(ref: String!, cloneRef: String!): String {
let repo = withoutURLScheme(cloneRef)
let path = withoutURLScheme(ref)
let rest = path.trimPrefix(repo)
if (path.hasPrefix(repo) == false or (rest != "" and rest.hasPrefix("/") == false and rest.hasPrefix("@") == false and rest.hasPrefix("#") == false)) {
null
} else if (rest.hasPrefix("#")) {
rest.trimPrefix("#").split(":", limit: 2)[0] ?? ""
} else {
rest.split("@", limit: 2)[1] ?? ""
}
}

let withoutURLScheme(ref: String!): String! {
ref.split("://", limit: 2)[1] ?? ref
}

"""
Whether the module's manifest names the entrypoint this SDK generates, at the
path it generates it to. A main.dang there without the generated header is the
Expand Down
44 changes: 44 additions & 0 deletions main.dang.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,50 @@ type JavaSdk {
.filter { label => label != "" }
}

"""
Whether a Git client is a module of the Git workspace, at the workspace
commit. Both ref formats are read: the workspace address keeps
<clone>/<path>@<version>, while from engine v1.0.0-beta.20 a module of the
workspace prints <clone>#<version>[:<subpath>]. URL schemes are ignored,
because a GitHub address has none while its clone ref has https://.
Selectors are compared as written, so an alias such as HEAD for main keeps
the Git binding.
"""
workspaceRefMatches(address: String!, commit: String!, cloneRef: String!, ref: String!, pin: String!): Boolean! {
if (cloneRef == "" or pin == "" or pin != commit) {
false
} else {
let workspaceSelector = gitSelector(address, cloneRef)
let moduleSelector = gitSelector(ref, cloneRef)
if (workspaceSelector == null or moduleSelector == null) {
false
} else {
workspaceSelector == "" or workspaceSelector == moduleSelector
}
}
}

"""
The selector of a ref of the repository at cloneRef, empty when the ref
names none, or null for a ref of another repository.
"""
let gitSelector(ref: String!, cloneRef: String!): String {
let repo = withoutURLScheme(cloneRef)
let path = withoutURLScheme(ref)
let rest = path.trimPrefix(repo)
if (path.hasPrefix(repo) == false or (rest != "" and rest.hasPrefix("/") == false and rest.hasPrefix("@") == false and rest.hasPrefix("#") == false)) {
null
} else if (rest.hasPrefix("#")) {
rest.trimPrefix("#").split(":", limit: 2)[0] ?? ""
} else {
rest.split("@", limit: 2)[1] ?? ""
}
}

let withoutURLScheme(ref: String!): String! {
ref.split("://", limit: 2)[1] ?? ref
}

"""
Whether the module's manifest names the entrypoint this SDK generates, at the
path it generates it to. A main.dang there without the generated header is the
Expand Down
16 changes: 14 additions & 2 deletions mod.dang
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,28 @@ type Mod {
"""
Where a client loads its target from: a git ref with the commit it resolved
to, or a module of the workspace by its path from the workspace root, which
serveModule reads in the calling module's own tree.
serveModule reads in the calling module's own tree, even when the workspace
is loaded from Git.
"""
let clientDescriptor(client: ModuleSource!): String! {
if (client.kind == ModuleSourceKind.GIT_SOURCE) {
if (client.kind == ModuleSourceKind.GIT_SOURCE and inWorkspace(client) == false) {
"git\n" + client.asString + "\n" + client.pin + "\n"
} else {
"local\n" + workspaceRef(client.sourceRootSubpath) + "\n"
}
}

"""
A workspace loaded from Git resolves its own modules as Git sources at its
commit. Bound by that ref, a client would change with every commit and never
match what the checkout generates. An unknown workspace commit matches no
client, so the client keeps its Git binding and its pin.
"""
let inWorkspace(client: ModuleSource!): Boolean! {
let commit = ws.git.head.commit rescue { err: Error => "" }
JavaSdk().workspaceRefMatches(ws.address, commit, client.cloneRef, client.asString, client.pin)
}

"""
The live engine version, without build metadata.

Expand Down