Skip to content
Open
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
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.27"

[[dependencies]]
name = "sdk-helpers"
source = "github.com/dagger/sdk-helpers@v1.0.9"
source = "github.com/dagger/sdk-helpers@v1.0.11"
4 changes: 2 additions & 2 deletions dagger.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[["version","2"]]
["","git-latest",["github.com/dagger/sdk-helpers",["version","v1.0.9"]],"refs/tags/v1.0.9"]
["","git-sha",["github.com/dagger/sdk-helpers","refs/tags/v1.0.9"],"daffbc01a1b3f3df7b1c417a7d2c83cd06d1bd33"]
["","git-latest",["github.com/dagger/sdk-helpers",["version","v1.0.11"]],"refs/tags/v1.0.11"]
["","git-sha",["github.com/dagger/sdk-helpers","refs/tags/v1.0.11"],"3be7456c40d9206b648ac44e418f30aa79bc6190"]
["","git-sha",["https://github.com/dagger/dagger","refs/tags/v1.0.0-beta.11"],"a4e1e4ff663e5e51c2b96c2c0772f3d2f00cfb94"]
["","oci-sha",["docker.io/library/golang:1.25-alpine"],"sha256:1ae0735f00daffa3aaf1363a5184c0d2dc55c78e3db4ec70241cdac97bf84b59"]
30 changes: 24 additions & 6 deletions sdk/bin/dagger-module
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Dagger\Dagger;
use Dagger\Module\CallDispatcher;
use Dagger\Module\DangEntrypointRenderer;
use Dagger\Module\Description;
use Dagger\Module\FunctionError;
use Dagger\Module\InterfaceClients;
use Dagger\Module\ValueCodec;
use GraphQL\Exception\QueryError;
Expand Down Expand Up @@ -46,6 +47,16 @@ require $autoload;
$mode = $argv[1] ?? '';
$options = options(array_slice($argv, 2));

// Made before the function runs, which may change the working directory.
$functionError = FunctionError::forModule(dirname(__DIR__), (string) getcwd());
$functionErrorPath = getenv(FunctionError::PATH_ENV);
$functionErrorPath = $functionErrorPath === false ? null : $functionErrorPath;
if ($mode === 'call') {
// A fatal error, such as exhausted memory or a class that can't be
// declared, ends the script with no Throwable to catch below.
$functionError->reportFatalErrors($functionErrorPath, STDERR);
}

try {
switch ($mode) {
case 'generate':
Expand All @@ -72,12 +83,19 @@ try {
default:
fail(USAGE, 2);
}
} catch (QueryError $e) {
$extensions = $e->getErrorDetails()['extensions'] ?? [];
} catch (Throwable $t) {
if ($mode === 'call') {
// A failed call exits 2, so that its exec isn't cached, and reports
// the function's error to the engine; see FunctionError.
$functionError->report($t, $functionErrorPath, STDERR, STDOUT);
exit(2);
}
if (!$t instanceof QueryError) {
fwrite(STDERR, sprintf("dagger-module: %s: %s\n%s\n", $t::class, $t->getMessage(), $t->getTraceAsString()));
exit(1);
}
$extensions = $t->getErrorDetails()['extensions'] ?? [];
fwrite(STDOUT, $extensions['stdout'] ?? '');
fwrite(STDERR, $e->getMessage() . "\n" . ($extensions['stderr'] ?? ''));
fwrite(STDERR, $t->getMessage() . "\n" . ($extensions['stderr'] ?? ''));
exit($extensions['exitCode'] ?? 1);
} catch (Throwable $t) {
fwrite(STDERR, sprintf("dagger-module: %s: %s\n%s\n", $t::class, $t->getMessage(), $t->getTraceAsString()));
exit(1);
}
60 changes: 52 additions & 8 deletions sdk/src/Module/DangEntrypointRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ private static function wrap(string $call, array $body): array
ModuleTypes().all
}

# The module with its Composer dependencies installed, which every call
# runs: the engine builds it ahead of a call, as the module's build, so
# a failed install shows as the build's, with Composer's output.
pub prebuild: File! {
runtime.file("vendor/autoload.php")
}

pub call(
receiverType: String!,
receiverValue: JSON,
Expand All @@ -302,7 +309,13 @@ private static function wrap(string $call, array $body): array
fnName: fnName,
fnArgs: fnArgs,
}})
let result = runtime
# A failed install stays out of the rescue below, so it fails the call
# with its own error. An engine that builds prebuild first fails the
# call before it gets here.
let installed = runtime.sync rescue {
err: GraphQLError => raiseWithStderr(err)
}
let result = installed
.withEnvVariable("DAGGER_CLIENT_VERSION", %clientVersion%)
# The exec is cached on its own recipe, which holds neither the call's
# arguments nor its cache policy; the engine's digest of the call does
Expand All @@ -311,17 +324,48 @@ private static function wrap(string $call, array $body): array
.withExec(["php", "sdk/bin/dagger-module", "call", "--output", "/dagger/result.json"], stdin: request, experimentalPrivilegedNesting: true)
.file("/dagger/result.json")
.contents rescue {
# From v1.0.0-beta.27 an exec error's message carries only the exit code.
# Its extensions hold the stderr, capped by the engine at 100 KiB.
err: GraphQLError => {
let stderr = (JSON.decode(err.extensions) :: {{stderr: String}}).stderr ?? ""
if (stderr == "") { raise err }
raise stderr
}
err: GraphQLError => raiseFunctionError(err)
}
(result :: JSON!)
}

# An engine that gives the dispatcher a file for the function's error
# (DAGGER_FUNCTION_ERROR_PATH) puts the error the dispatcher wrote there
# in its exec's error, and fails the call with it, values and all: the
# exec's error is raised as it is. Otherwise, as with an older engine or
# a file the dispatcher couldn't write, the exec error carries the end of
# the dispatcher's stderr, where its last marker line holds the
# function's error; without one, the exec failed before the function
# returned, as when it exited by itself, and its own error is raised
# again, or, on an engine that marks no module process, its stderr.
let raiseFunctionError(err: GraphQLError!): String! {
let reported = ((JSON.decode(err.extensions) :: {{functionError: {{message: String, id: String}}}}).functionError != null) rescue false
if (reported) {
raise err
}
let marker = "::dagger-function-error::"
let stderr = ((JSON.decode(err.extensions) :: {{stderr: String}}).stderr ?? "") rescue ""
let lines = stderr.split("\n").filter { line => line.hasPrefix(marker) }
if (lines.isEmpty) {
raiseWithStderr(err)
}
raise ((JSON.decode(lines.takeLast(1).join("").trimPrefix(marker)) :: String!) rescue err.message)
}

# Raise a failed exec's error. An engine that marks the module process
# (the moduleProcess extension) shows an exec's logs with its error, so
# the error is raised as it is; an older engine shows only the exit
# code, so the end of the exec's stderr, which the engine caps at
# 100 KiB, is raised instead.
let raiseWithStderr(err: GraphQLError!): Container! {
let marked = ((JSON.decode(err.extensions) :: {{moduleProcess: Boolean}}).moduleProcess ?? false) rescue false
let stderr = ((JSON.decode(err.extensions) :: {{stderr: String}}).stderr ?? "") rescue ""
if (marked or stderr == "") {
raise err
}
raise stderr
}

let runtime: Container! {
PhpModuleBuild(contextDir: currentModule.contextDirectory, subPath: currentModule.sourceSubpath).installed
}
Expand Down
Loading