Skip to content
Merged
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
16 changes: 16 additions & 0 deletions .dagger/modules/e2e/main.dang
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,25 @@ type E2e {
let called = cli(playground(ws), ["-m", runtimeModulePath, "call", "greeting"]).stdout
assertContains(called, runtimeGreeting, "the module did not run on this repository's runtime")

let sourcePath = "/" + runtimeModulePath + "/src/runtime_app/__init__.py"
let edited = ws.withNewFile(sourcePath, ws.file(sourcePath).contents + "\n")
assert(stdlibCompiledAt(edited) == stdlibCompiledAt(ws), "an edit of the module's code compiled the standard library again")

null
}

"""
When the runtime's build compiled the standard library, with checked-hash
bytecode. The generated entrypoint compiles it once per source state instead.
"""
let stdlibCompiledAt(ws: Workspace!): String! {
pythonSdkRuntime
.moduleRuntime(modSource: ws.moduleSource("/" + runtimeModulePath), introspectionJson: null)
.withExec(["python", "-c", "import json, os; b = open(json.__cached__, 'rb').read(8); assert int.from_bytes(b[4:8], 'little') == 3, b; print(os.stat(json.__cached__).st_mtime_ns)"])
.stdout
.trimSpace
}

"""
The runtime refuses a module whose generated files are missing instead of
regenerating them: the no-codegen-at-load contract, which the engine's
Expand Down
9 changes: 9 additions & 0 deletions runtime/build.dang
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ type PythonModuleBuild {

let ctr = container
.from(baseImage)
# Most base images ship the standard library without its bytecode, and a
# call's writes do not last, so each call compiled what it imports.
# Best effort, for `python` on PATH only: a custom image without it or
# with a read-only standard library must still build.
.withExec(["python", "-c",
"import compileall, py_compile, re, sysconfig\n" +
"compileall.compile_dir(sysconfig.get_path('stdlib'), quiet=2, force=True, rx=re.compile('/site-packages/'),\n" +
" invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH)\n",
], expect: ReturnType.ANY)
# Informational only, in case a module finds them useful.
.withEnvVariable("DAGGER_BASE_IMAGE", baseImage)
.withEnvVariable("PYTHONUNBUFFERED", "1")
Expand Down