Skip to content

Commit d9aff5c

Browse files
authored
Fix determine position for value in directive without quotes (#2141)
* Update using_directives to 1.1.0 * Fix determine position for value in directive without quotes
1 parent e060eb2 commit d9aff5c

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

modules/build/src/test/scala/scala/build/tests/ActionableDiagnosticTests.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ class ActionableDiagnosticTests extends munit.FunSuite {
5151
}
5252
}
5353

54+
test("actionable diagnostic report correct position") {
55+
val dependencyOsLib = "com.lihaoyi::os-lib:0.7.8"
56+
val dependencyPprintLib = "com.lihaoyi::pprint:0.6.6"
57+
val testInputs = TestInputs(
58+
os.rel / "Foo.scala" ->
59+
s"""//> using dep $dependencyOsLib
60+
|//> using dep "$dependencyPprintLib"
61+
|
62+
|object Hello extends App {
63+
| println("Hello")
64+
|}
65+
|""".stripMargin
66+
)
67+
testInputs.withBuild(baseOptions, buildThreads, None, actionableDiagnostics = true) {
68+
(root, _, maybeBuild) =>
69+
val build = maybeBuild.orThrow
70+
val updateDiagnostics =
71+
ActionablePreprocessor.generateActionableDiagnostics(build.options).orThrow
72+
73+
val actionableDiagnostics = updateDiagnostics.collect {
74+
case diagnostic: ActionableDependencyUpdateDiagnostic => diagnostic
75+
}
76+
77+
val osLib = actionableDiagnostics.find(_.suggestion.startsWith("com.lihaoyi::os-lib")).get
78+
val pprintLib =
79+
actionableDiagnostics.find(_.suggestion.startsWith("com.lihaoyi::pprint")).get
80+
81+
expect(osLib.positions == Seq(File(Right(root / "Foo.scala"), (0, 14), (0, 39))))
82+
expect(pprintLib.positions == Seq(File(Right(root / "Foo.scala"), (1, 15), (1, 40))))
83+
}
84+
}
85+
5486
test("using outdated dependencies with --suppress-outdated-dependency-warning") {
5587
val dependencyOsLib = "com.lihaoyi::os-lib:0.7.8"
5688
val dependencyPprintLib = "com.lihaoyi::pprint:0.6.6"

modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object DirectiveValueParser {
110110
}
111111

112112
def position(path: Either[String, os.Path]): Position =
113-
DirectiveUtil.position(value, path, skipQuotes = isString)
113+
DirectiveUtil.position(value, path)
114114
}
115115

116116
given DirectiveValueParser[Boolean] = { (key, values, scopePath, path) =>

modules/directives/src/main/scala/scala/build/preprocessing/directives/DirectiveUtil.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scala.build.preprocessing.directives
22

33
import com.virtuslab.using_directives.custom.model.{BooleanValue, EmptyValue, StringValue, Value}
4+
import com.virtuslab.using_directives.custom.utils.ast.StringLiteral
45
import dependency.AnyDependency
56
import dependency.parser.DependencyParser
67

@@ -14,9 +15,16 @@ object DirectiveUtil {
1415

1516
def position(
1617
v: Value[_],
17-
path: Either[String, os.Path],
18-
skipQuotes: Boolean = false
18+
path: Either[String, os.Path]
1919
): Position.File = {
20+
val skipQuotes: Boolean = v match {
21+
case stringValue: StringValue =>
22+
stringValue.getRelatedASTNode match {
23+
case literal: StringLiteral => literal.getIsWrappedDoubleQuotes()
24+
case _ => false
25+
}
26+
case _ => false
27+
}
2028
val line = v.getRelatedASTNode.getPosition.getLine
2129
val column = v.getRelatedASTNode.getPosition.getColumn + (if (skipQuotes) 1 else 0)
2230
val endLinePos = column + v.toString.length
@@ -31,13 +39,13 @@ object DirectiveUtil {
3139
): Seq[Positioned[String]] =
3240
scopedDirective.directive.values.map {
3341
case v: StringValue =>
34-
val pos = position(v, scopedDirective.maybePath, skipQuotes = true)
42+
val pos = position(v, scopedDirective.maybePath)
3543
Positioned(pos, v.get)
3644
case v: BooleanValue =>
37-
val pos = position(v, scopedDirective.maybePath, skipQuotes = false)
45+
val pos = position(v, scopedDirective.maybePath)
3846
Positioned(pos, v.get.toString)
3947
case v: EmptyValue =>
40-
val pos = position(v, scopedDirective.maybePath, skipQuotes = false)
48+
val pos = position(v, scopedDirective.maybePath)
4149
Positioned(pos, v.get)
4250
}
4351

project/deps.sc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ object Deps {
178178
val typelevelToolkitVersion = "0.0.11"
179179
def typelevelToolkit = ivy"org.typelevel:toolkit:$typelevelToolkitVersion"
180180
def typelevelToolkitTest = ivy"org.typelevel:toolkit-test:$typelevelToolkitVersion"
181-
def usingDirectives = ivy"org.virtuslab:using_directives:1.0.0"
181+
def usingDirectives = ivy"org.virtuslab:using_directives:1.1.0"
182182
// Lives at https://github.com/scala-cli/no-crc32-zip-input-stream, see #865
183183
// This provides a ZipInputStream that doesn't verify CRC32 checksums, that users
184184
// can enable by setting SCALA_CLI_VENDORED_ZIS=true in the environment, to workaround

0 commit comments

Comments
 (0)