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
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/common-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ kotlin {
repositories {
mavenCentral()
maven("https://cache-redirector.jetbrains.com/intellij-dependencies")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-js-wrappers")
maven("https://packages.jetbrains.team/maven/p/edu/maven")
maven("https://packages.jetbrains.team/maven/p/edu/educational-ml-library")
maven("https://packages.jetbrains.team/maven/p/grazi/grazie-platform-public")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,43 @@ abstract class ItemContainer : StudyItem() {
}

private var _items = mutableListOf<StudyItem>()

/**
* Entries of this container's `content:` list that could not be resolved during the last load: the child directory or
* its config file was missing, or the config failed to deserialize. They are not part of [items], but [contentNames]
* still writes them back, so saving the container neither drops them from the config file on disk nor has to be
* skipped -- a container that refused to save would silently lose every child added to it later.
*
* Recomputed on every load, so a later complete load clears it again.
*/
@Transient
var unresolvedContent: List<UnresolvedContentEntry> = emptyList()

/**
* The `content:` list of this container's config file: the names of [items] in their order, with each
* [unresolvedContent] entry kept right after the entry it followed on disk.
*/
val contentNames: List<String>
get() {
val names = items.map { it.name }
if (unresolvedContent.isEmpty()) return names

val present = names.toSet()
val (anchored, orphaned) = unresolvedContent
.filter { it.name !in present }
.partition { it.previousName == null || it.previousName in present }
val namesByAnchor = anchored.groupBy({ it.previousName }, { it.name })
return buildList {
addAll(namesByAnchor[null].orEmpty())
for (name in names) {
add(name)
addAll(namesByAnchor[name].orEmpty())
}
// The entry they followed is gone from the container, so there is no position left to keep.
addAll(orphaned.map { it.name })
}
}

override fun init(parentItem: ItemContainer, isRestarted: Boolean) {
parent = parentItem
for ((i, item) in items.withIndex()) {
Expand All @@ -30,11 +67,20 @@ abstract class ItemContainer : StudyItem() {
fun addItem(item: StudyItem) {
_items.add(item)
item.parent = this
forgetUnresolved(item.name)
}

fun addItem(index: Int, item: StudyItem) {
_items.add(index, item)
item.parent = this
forgetUnresolved(item.name)
}

// From now on the item owns this name, so removing it later must not bring the unresolved entry back.
private fun forgetUnresolved(name: String) {
if (unresolvedContent.any { it.name == name }) {
unresolvedContent = unresolvedContent.filter { it.name != name }
}
}

fun replaceItem(existingItem: StudyItem, newItem: StudyItem) {
Expand All @@ -52,3 +98,9 @@ abstract class ItemContainer : StudyItem() {
_items.sortBy { it.index }
}
}

/**
* A `content:` entry named [name] that could not be resolved during load. [previousName] is the closest resolved entry
* before it (`null` if there is none), so [ItemContainer.contentNames] can write it back at the same place.
*/
data class UnresolvedContentEntry(val name: String, val previousName: String?)
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ abstract class StudyItem() {
var id: Int = 0 // id on remote resource (Stepik, Marketplace)
var contentTags: List<String> = listOf()

/**
* `true` when this item's children could not be fully resolved during the last load: a child directory or its config
* file was missing, or a child failed to deserialize. Such an item holds fewer children than the config file on disk
* claims, so writing it back would persist the truncated `content:` list and permanently drop the missing children.
*
* [org.hyperskill.academy.learning.yaml.YamlFormatSynchronizer.saveItem] refuses to write the *structural* config
* while this is set; the remote config keeps being written, as it holds no `content:` list.
* It is recomputed on every load rather than latched, so a later complete load clears it again.
*
* Note this covers children only. A [org.hyperskill.academy.learning.courseFormat.tasks.Task] whose task files
* could not be resolved is deliberately *not* marked here: its `status` lives in the same config file, so blocking
* the save would stop the solved state from being persisted.
*/
@Transient
var isPartiallyLoaded: Boolean = false

@Transient
private var _parent: ItemContainer? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ abstract class CourseYamlMixin {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private lateinit var environment: String

@JsonSerialize(contentConverter = StudyItemConverter::class)
@JsonProperty(CONTENT)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private lateinit var _items: List<StudyItem>
// Not the `items` themselves: the names also keep the entries that could not be resolved on load,
// see `ItemContainer.unresolvedContent`
val contentNames: List<String>
@JsonProperty(CONTENT)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
get() = throw NotImplementedInMixin()

@JsonProperty(SOLUTIONS_HIDDEN)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import org.hyperskill.academy.learning.courseFormat.Lesson
import org.hyperskill.academy.learning.courseFormat.StudyItem
import org.hyperskill.academy.learning.json.mixins.NotImplementedInMixin
import org.hyperskill.academy.learning.yaml.errorHandling.formatError
import org.hyperskill.academy.learning.yaml.errorHandling.unnamedItemAtMessage
import org.hyperskill.academy.learning.yaml.format.YamlMixinNames.CONTENT
Expand All @@ -29,9 +28,11 @@ abstract class LessonYamlMixin {
@JsonInclude(JsonInclude.Include.NON_NULL)
private var customPresentableName: String? = null

@JsonProperty(CONTENT)
@JsonSerialize(contentConverter = StudyItemConverter::class)
private lateinit var _items: List<StudyItem>
// Not the `items` themselves: the names also keep the entries that could not be resolved on load,
// see `ItemContainer.unresolvedContent`
val contentNames: List<String>
@JsonProperty(CONTENT)
get() = throw NotImplementedInMixin()

@JsonProperty(TAGS)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import org.hyperskill.academy.learning.courseFormat.Section
import org.hyperskill.academy.learning.courseFormat.StudyItem
import org.hyperskill.academy.learning.json.mixins.NotImplementedInMixin
import org.hyperskill.academy.learning.yaml.errorHandling.formatError
import org.hyperskill.academy.learning.yaml.errorHandling.unnamedItemAtMessage
import org.hyperskill.academy.learning.yaml.format.YamlMixinNames.CONTENT
Expand All @@ -28,9 +27,11 @@ abstract class SectionYamlMixin {
@JsonInclude(JsonInclude.Include.NON_NULL)
private var customPresentableName: String? = null

@JsonProperty(CONTENT)
@JsonSerialize(contentConverter = StudyItemConverter::class)
private lateinit var _items: List<StudyItem>
// Not the `items` themselves: the names also keep the entries that could not be resolved on load,
// see `ItemContainer.unresolvedContent`
val contentNames: List<String>
@JsonProperty(CONTENT)
get() = throw NotImplementedInMixin()

@JsonProperty(TAGS)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class EduProjectActivity : ProjectActivity {
@RequiresBlockingContext
fun migrateYaml(project: Project, course: Course) {
// `saveAll` rewrites every config file from the in-memory model. If that model resolved only partially
// (see `YamlLoader.deserializeContent` and `YamlDeepLoader.removeNonExistingTaskFiles`, both of which silently
// drop unresolvable children), running it on every project open turns a transient loading problem into permanent
// data loss on disk. Save only when a migration actually ran: the YAML-format migration in
// (see `YamlDeepLoader.removeNonExistingTaskFiles`, which silently drops unresolvable task files), running it on
// every project open turns a transient loading problem into permanent data loss on disk. Save only when a
// migration actually ran: the YAML-format migration in
// `YamlDeepLoader.loadCourse` already has its own `needMigration`-gated `saveAll`.
if (!migrateCanCheckLocallyYaml(project, course)) return
YamlFormatSynchronizer.saveAll(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.hyperskill.academy.learning.yaml
import com.fasterxml.jackson.databind.ObjectMapper
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.fileEditor.FileDocumentManager
Expand Down Expand Up @@ -53,8 +52,6 @@ import javax.swing.JLabel
import javax.swing.JPanel

object YamlFormatSynchronizer {
private val LOG = Logger.getInstance(YamlFormatSynchronizer::class.java)

val LOAD_FROM_CONFIG = Key<Boolean>("Hyperskill.loadItem")
val SAVE_TO_CONFIG = Key<Boolean>("Hyperskill.saveItem")
private val SYNCHRONIZATION_STARTED = Key<Boolean>("Hyperskill.synchronizationStarted")
Expand Down Expand Up @@ -90,18 +87,10 @@ object YamlFormatSynchronizer {
if (!YamlFormatSettings.shouldCreateConfigFiles(project)) {
return
}
// The item holds fewer children than its config file on disk claims, because some of them could not be resolved
// during the last load. Writing it out now would persist the truncated `content:` list and permanently drop the
// missing children from the course. Skipping the save keeps the on-disk structure intact until a load succeeds.
//
// Only the structural config is guarded. `*-remote-info.yaml` carries no `content:` list, so a partially loaded
// item has to keep persisting its remote state (id, update date, submissions) as usual -- otherwise a single
// unresolvable child would silently freeze the remote state of its whole container for the rest of the session.
if (item.isPartiallyLoaded && configName == item.configFileName) {
LOG.warn("Skipping save of ${item.itemType} `${item.name}`: its children were not fully resolved during load")
return
}

// A container whose children were not all resolved during the last load is saved like any other: its `content:`
// keeps the unresolved names (see `ItemContainer.unresolvedContent`), so the save neither drops them nor has to be
// skipped. Skipping it lost every child added later, e.g. each Hyperskill topic opened after one topic folder
// went missing.
item.saveConfig(project, configName, mapper)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,26 @@ object YamlLoader {
}
}

internal inline fun <reified T : StudyItem> StudyItem.deserializeContent(
internal inline fun <reified T : StudyItem> ItemContainer.deserializeContent(
project: Project,
contentList: List<T>,
mapper: ObjectMapper = basicMapper(),
): List<T> {
val content = mutableListOf<T>()
var unresolvedChildren = false
val unresolved = mutableListOf<UnresolvedContentEntry>()
for (titledItem in contentList) {
val configFile: VirtualFile? = getConfigFileForChild(project, titledItem.name)
if (configFile == null) {
unresolvedChildren = true
continue
val deserializeItem = configFile?.let {
deserializeItemProcessingErrors(it, project, mapper = mapper, parentItem = this) as? T
}
val deserializeItem = deserializeItemProcessingErrors(configFile, project, mapper = mapper, parentItem = this) as? T
if (deserializeItem == null) {
unresolvedChildren = true
if (configFile == null || deserializeItem == null) {
LOG.warn("Cannot resolve `${titledItem.name}` listed in the content of $itemType `$name`, keeping it in the config")
unresolved += UnresolvedContentEntry(titledItem.name, content.lastOrNull()?.name)
continue
}
if (this is Lesson && isHyperskillTopicsLesson() && deserializeItem is UnsupportedTask) {
// A deliberate skip, not an unresolved child: topic lessons legitimately contain tasks this IDE cannot
// support, so this must not mark the lesson partially loaded and block saving it.
// support, so the next save drops them from the lesson config.
LOG.warn(
"Skipping unsupported task `${titledItem.name}` while loading Hyperskill topic lesson `${name}` from ${configFile.path}"
)
Expand All @@ -146,7 +145,7 @@ object YamlLoader {
}

// Recomputed on every load rather than latched, so a later complete load clears it again.
isPartiallyLoaded = unresolvedChildren
unresolvedContent = unresolved
return content
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ open class ItemContainerChangeApplier<T : ItemContainer>(val project: Project) :
val existingChildren = existingItem.items
val preservedChildren = mutableListOf<StudyItem>()
val mapper = mapper()
var unresolvedChildren = false
val unresolved = mutableListOf<UnresolvedContentEntry>()
for (titledItem in deserializedItem.items) {
val child = existingChildren.find { it.name == titledItem.name }
if (child != null) {
Expand All @@ -79,14 +79,9 @@ open class ItemContainerChangeApplier<T : ItemContainer>(val project: Project) :
// this code adding new child item if it was added in config and there's a dir
// it is called from `YamlLoader.loadItem`
val configFile = existingItem.getConfigFileForChild(project, titledItem.name)
if (configFile == null) {
unresolvedChildren = true
continue
}

val deserializedChild = deserializeItemProcessingErrors(configFile, project, mapper = mapper)
val deserializedChild = configFile?.let { deserializeItemProcessingErrors(it, project, mapper = mapper) }
if (deserializedChild == null) {
unresolvedChildren = true
unresolved += UnresolvedContentEntry(titledItem.name, preservedChildren.lastOrNull()?.name)
continue
}
deserializedChild.name = titledItem.name
Expand All @@ -98,9 +93,9 @@ open class ItemContainerChangeApplier<T : ItemContainer>(val project: Project) :
}
// update items so as removed items are no longer in the course
existingItem.items = preservedChildren
// A child named in the config could not be resolved, so `preservedChildren` is shorter than what the config file
// on disk claims. Recomputed on every apply rather than latched, so a later complete load clears it again.
existingItem.isPartiallyLoaded = unresolvedChildren
// Children named in the config that could not be resolved stay in it on the next save.
// Recomputed on every apply rather than latched, so a later complete load clears it again.
existingItem.unresolvedContent = unresolved
existingItem.init(existingItem.parent, false)
}
}
Expand Down
Loading
Loading