Skip to content

Commit 298d796

Browse files
committed
fix: resolve extension definitions from config
If a matching definition is found, use its metadata directly. Fall back to manual construction only when no match exists.
1 parent 5c29d92 commit 298d796

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

libs/linglong/src/linglong/cli/cli.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,6 @@ int Cli::run(const RunOptions &options)
654654
opts.cdiDevices = std::move(*autoDetectedCdiDevices);
655655
}
656656

657-
// 调整日志输出,打印扩展列表(用逗号拼接)
658-
std::string extStr =
659-
opts.extensionRefs ? linglong::common::strings::join(*opts.extensionRefs, ',') : "null";
660-
LogD("start resolve run context with base {}, runtime {}, extensions {}",
661-
opts.baseRef.value_or("null"),
662-
opts.runtimeRef.value_or("null"),
663-
extStr);
664-
665657
auto res = runContext.resolve(*curAppRef, opts);
666658
if (!res) {
667659
handleCommonError(res.error());

libs/linglong/src/linglong/runtime/run_context.cpp

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ namespace {
2525

2626
constexpr const char *runContextConfigVersion = "1";
2727

28+
bool isExtensionBelongTo(const std::string &defName, const std::string &id)
29+
{
30+
if (defName == id) {
31+
return true;
32+
}
33+
if (common::strings::starts_with(id, defName + ".")) {
34+
return true;
35+
}
36+
return false;
37+
}
38+
2839
void ensureMountSrcType(std::vector<api::types::v1::Mount> &mounts)
2940
{
3041
for (auto &m : mounts) {
@@ -443,13 +454,40 @@ utils::error::Result<void> RunContext::resolve(const api::types::v1::RunContextC
443454
if (extLayer.getCachedItem().info.kind != "extension") {
444455
return LINGLONG_ERR("invalid extension kind in config.extensions");
445456
}
457+
458+
api::types::v1::ExtensionDefine extDef;
459+
const auto &targetInfo = targetLayer->get().getCachedItem().info;
460+
const auto *matchedDef = [&]() -> const api::types::v1::ExtensionDefine * {
461+
if (!targetInfo.extensions) {
462+
return nullptr;
463+
}
464+
for (const auto &def : *targetInfo.extensions) {
465+
std::string name = def.name;
466+
auto ext = extension::ExtensionFactory::makeExtension(name);
467+
if (ext->shouldEnable(name)
468+
&& isExtensionBelongTo(def.name, extLayer.getReference().id)) {
469+
return &def;
470+
}
471+
}
472+
return nullptr;
473+
}();
474+
475+
if (matchedDef) {
476+
extDef = *matchedDef;
477+
} else {
478+
LogW("extension {} not found in target layer {}'s extensions, "
479+
"using manual extension define",
480+
extLayer.getReference().toString(),
481+
targetLayer->get().getReference().toString());
482+
auto manualDefs = makeManualExtensionDefine({ extensionRefStr });
483+
if (!manualDefs) {
484+
return LINGLONG_ERR(manualDefs);
485+
}
486+
extDef = std::move(manualDefs->front());
487+
}
488+
446489
extLayer.setExtensionInfo(RuntimeLayer::ExtensionRuntimeLayerInfo{
447-
.extensionInfo =
448-
api::types::v1::ExtensionDefine{
449-
.directory = "/opt/extensions/" + extLayer.getReference().id,
450-
.name = extLayer.getReference().id,
451-
.version = extLayer.getReference().version.toString(),
452-
},
490+
.extensionInfo = std::move(extDef),
453491
.extensionLayer = std::ref(extLayer),
454492
.forRef = targetRefStr,
455493
});
@@ -545,6 +583,7 @@ utils::error::Result<void> RunContext::resolveLayer(bool depsBinaryOnly,
545583

546584
const auto &extensionOf = ext.getExtensionInfo();
547585
if (!extensionOf) {
586+
LogW("failed getExtensionInfo, skip");
548587
continue;
549588
}
550589

0 commit comments

Comments
 (0)