Skip to content

Commit

Permalink
Merge pull request #729 from modelix/fix-resolving-project-modules-fo…
Browse files Browse the repository at this point in the history
…r-non-existing-projects

fix(mps-model-adapters): return null when resolving project module in an unresolvable project
  • Loading branch information
odzhychko authored May 28, 2024
2 parents 4c234f5 + 8b8c920 commit 7a9dc07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.modelix.model.mpsadapters

import org.modelix.model.api.BuiltinLanguages
import org.modelix.model.api.INode

/*
* Copyright (c) 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

class MPSAreaTest : MpsAdaptersTestBase("SimpleProject") {

fun testResolveModuleInNonExistingProject() {
val repositoryNode: INode = MPSRepositoryAsNode(mpsProject.repository)
val area = repositoryNode.getArea()
readAction {
val nonExistingProject = MPSProjectReference("nonExistingProject")
val module = repositoryNode.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Repository.modules)
.single { it.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) == "Solution1" }
val projectModuleReference = MPSProjectModuleReference((module.reference as MPSModuleReference).moduleReference, nonExistingProject)

val resolutionResult = area.resolveNode(projectModuleReference)

assertNull(resolutionResult)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,24 @@ data class MPSArea(val repository: SRepository) : IArea, IAreaReference {
val projectRef = if (ref is MPSProjectModuleReference) {
ref.projectRef
} else {
// XXX Prefix of `projectRef` is not checked.
// `projectRef` might actually be a ref to anything.
// This might trigger unexpected resolution results and undefined behavior.
// Similar missing checks exist for other references in `MPSArea`.
// See https://issues.modelix.org/issue/MODELIX-923
val projectRef = serialized.substringAfter(MPSProjectModuleReference.SEPARATOR)
NodeReference(projectRef)
}

val resolvedNodeForProject = resolveNode(projectRef) ?: return null
check(resolvedNodeForProject is MPSProjectAsNode) {
"Resolved node `$resolvedNodeForProject` does not represent a project."
}
val resolvedProject = resolvedNodeForProject.project

return moduleRef.resolve(repository)?.let {
MPSProjectModuleAsNode(
project = (resolveNode(projectRef) as MPSProjectAsNode).project,
project = resolvedProject,
module = it,
)
}
Expand Down

0 comments on commit 7a9dc07

Please sign in to comment.